Getting started with Ansible

 Eric Williams | 16 June, 2014
ansible devops 

Setting up an Ansible test environment with LXC

These examples are made using Ubuntu 12.04 LTS and LXC.

To make this as easy as possible, it's a good idea to be using a local dnsmasqd to handle DHCP, DNS, and dynamic DNS for container instances.

We're going to make a small environment that simulates a few webservers, a database server, and an HTTP load-balancer. This will make 6 LXC containers, including the Ansible workstation that we'll be using for the ansible commands.

First, create a few machines using the lxc-create command:

sudo lxc-create -t ubuntu -n ansible-workstation -- -r precise --auth-file ~/.ssh/
sudo lxc-start -n ansible-workstation -d
for i in {1..3}; 
do sudo lxc-create -t ubuntu -n ansible-web-${i} -- -r precise --auth-key ~/.ssh/
   sudo lxc-start -n ansible-web-${i} -d

SSH into the Ansible workstation and setup Ansible:

ssh -l ubuntu ansible-workstation
sudo locale-gen en_{US,GB}{.UTF-8,}
sudo apt-get install -y python-software-properties
sudo apt-add-repository ppa:rquillo/ansible
sudo apt-get update
sudo apt-get install -y ansible

Ansible should be installed now. We're going to setup the ansible clients (ansible-web-{1,2,3}), add our ssh key now, and update the sudoers file to make things easy. Ansible keeps its list of hosts under /etc/ansible/hosts:

$ cat /etc/ansible/hosts

Create the sudoers file:

cat 10_ubuntu

Now see if your connection to the clients is working:

$ ansible -m ping all | success >> {
    "changed": false,
    "ping": "pong"
} | success >> {
    "changed": false,
    "ping": "pong"
} | success >> {
    "changed": false,
    "ping": "pong"

Copy our ssh key and sudoers file over to all those machines:

ansible all -m copy -a "src=~/.ssh/ dest=~/.ssh/authorized_keys mode=0600" -k
ansible all -m copy -a "src=10_ubuntu dest=/etc/sudoers.d/10_ubuntu mode=0440 \
   owner=root group=root" --sudo --ask-sudo-pass

At this point, we should be able to run commands via sudo on these machines.

Using Playbooks to Install Packages

These machines are all going to be web servers, so we're going to do the most basic thing: Install Apache.

We'll create a simple playbook, apache.yml:

- hosts: webservers
  sudo: yes
      - name: Installs apache web server
      apt: pkg=apache2 state=installed update_cache=true

Apply it using the ansible-playbook command:

$ ansible-playbook apache.yml

PLAY [webservers] *************************************************************

GATHERING FACTS ***************************************************************
ok: []
ok: []
ok: []
ok: []
ok: []

TASK: [Installs apache web server] ********************************************
failed: [] => {"failed": true}
msg: Could not import python modules: apt, apt_pkg. Please install python-apt package.
FATAL: all hosts have already failed -- aborting

Doh! We need to install python-apt on these machines.

ansible  -m shell -a 'sudo apt-get update && sudo apt-get install -y python-apt' webservers
ansible-playbook apache2.yml

You can run it again to see that everything is as expected:

$ ansible-playbook apache2.yml
PLAY RECAP ******************************************************************** : ok=2    changed=0    unreachable=0    failed=0 : ok=2    changed=0    unreachable=0    failed=0 : ok=2    changed=0    unreachable=0    failed=0 : ok=2    changed=0    unreachable=0    failed=0 : ok=2    changed=0    unreachable=0    failed=0 : ok=2    changed=0    unreachable=0    failed=0


  • Setup ansible
  • Setup LXC containers as ansible clients
  • Transfer files
  • Install packages via Ad Hoc commands
  • Run a simple playbook