Beginning Ansible

May 12, 2021

I wanted to spend some more time learning about Ansible, this post covers the setup I created to do this.

VirtualBox

First of all, I created three VMs in VirtualBox. These were all the latest Debian linux, and all were installed from scratch.

Next, I configured these VMs to have a Bridged Network so that they could all see each other. I then restarted them all.

IP Address

After logging in to each of them, I used the command ip a to get the IP address.

Ansible Controller

I used the first machine as an Ansible controller, using apt-get install ansible to install it.

I created an ssh key locally with ssh-keygen and then pushed this key out to the other two servers:

$ ssh-copy-id tjs@192.168.1.252
$ ssh-copy-id tjs@192.168.1.253

These are the IP addresses of the other two non-controller hosts in VirtualBox.

I then created a file called hosts which had the following contents:

[web]
192.168.1.252
192.168.1.253

Ping

I am now able to ping the two hosts marked as "web" hosts, using

$ ansible -I ./hosts web -m ping

NGINX

The next step was to try and install Nginx on both servers. This mechanism uses the apt Ansible module.

$ ansible -i ./hosts web -b --become-user=root -m apt -a 'name=nginx state=present'

This failed as sudo wasn't installed on the slave machines.

Once sudo had been installed, it failed again with Missing sudo password. This can be prompted for on the command line using the -K option.

$ ansible -i ./hosts web -b --become-user=root -m apt -a 'name=nginx state=present' -K

This once more failed, as my user is not in the sudoers file. This was fixed using this command directly on each of the target machines:

# usermod -aG sudo tjs

The command will now work and install Nginx on both servers.

apt-get upgrade

Likewise, updating of the packages can be done with

$ ansible -i ./hosts web -b --become-user=root -m apt -a 'upgrade=safe' -K

Tags: ansible linux