Setting Up Ansible on Windows Subsystem for Linux
...and using it for deploying stuff
I was trying to deploy an app to my new virtual LAMP server using the Chef DevOps tool, but I encountered a problem (maybe a bug) with my SSH keys. I filed an issue in the Chef Workstation GitHub repository.
So, I needed an alternative. I figured I might try out Ansible, but it only runs on UNIX OSes. I was, though, already running Ubuntu 18.04 on my Windows machine using the Windows Subsystem for Linux, so I could use that.
To proceed with the installation, I opened a WSL/Ubuntu console and, according to the instructions found in the sources section, ran the following commands:
sudo apt update
sudo apt-get install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt-get install ansible
I then cd
-ed to the /etc/ansible
directory accessible in the WSL console. I edited the Ansible hosts
file with the following command:
sudo nano ./hosts
...and added my server (mkkekkonen.fi
) to the ungrouped hosts section like this:
# Ex 1: Ungrouped hosts, specify before any group headers.
## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10
mkkekkonen.fi
Next, in the WSL shell, I navigated to an arbitrary folder where I store my code in the Windows filesystem. I created a directory named ansible
there, entered the directory and ran the command nano test.yaml
to create an Ansible playbook. I copy-pasted the following into the playbook file:
---
- name: My playbook
hosts: all
tasks:
- name: Leaving a mark
command: "touch /tmp/ansible_was_here"
This SSHs to all the servers (just one in my case), or hosts, specified in the /etc/ansible/hosts
file, and creates an empty file named ansible_was_here
in the /tmp
directory.
To prepare for actually running the playbook, I started the SSH agent with the following command:
ssh-agent bash
My SSH keys resided in a directory in the Windows filesystem. I copied the correct key from the Windows folder to the ~/.ssh
directory in the WSL shell. Next, the permissions of the key file had to be modified, or ssh-agent
would not accept the key file. So I ran this command in the ~/.ssh
directory:
chmod 600 keyfile.pem
Here, the chmod
command marks the key file as readable and writable but not executable by the owning user, and prevents reading, writing and execution by the owning group and everybody else. More information and clarification about chmod
can be found behind the link in the sources of this article.
I then simply added the file to the SSH agent like this:
ssh-add keyfile.pem
Next, I navigated back to the ansible
directory and ran this command:
ansible-playbook test.yaml
(A detail: the user names in my WSL shell and on my server are the same, so I didn't need to provide a user name in the ansible-playbook
command.)
The output was the following:
PLAY [My playbook] *****************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [mkkekkonen.fi]
TASK [Leaving a mark] **************************************************************************************************
[WARNING]: Consider using the file module with state=touch rather than running 'touch'. If you need to use command
because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message.
changed: [mkkekkonen.fi]
PLAY RECAP *************************************************************************************************************
mkkekkonen.fi : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Now, when I SSHed to my server and checked the /tmp
directory, the ansible_was_here
file indeed was there, so executing the Ansible playbook was successful.
Worth Checking Out:
Ansible Tutorial for Beginners: Playbook & Examples
Sources:
Ansible Documentation: Installing Ansible: Installing Ansible on Ubuntu
Ansible Documentation: Getting Started
Ansible Documentation: Connection Methods and Details: Setting up SSH Keys
AskUbuntu: Understanding chmod Symbolic Notation and use of Octal