subcritical


Getting started with Ansible

 Eric Williams | 16 June, 2014
 Comments
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/id_rsa.pub
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/id_rsa.pub
   sudo lxc-start -n ansible-web-${i} -d
done

SSH into the Ansible workstation and setup Ansible:

ssh -l ubuntu ansible-workstation
sudo locale-gen en_{US,GB}{.UTF-8,}
ssh-keygen
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
[webservers]
ansible-client-[1:3].home.hokeypokeyland.org

Create the sudoers file:

cat 10_ubuntu
ubuntu ALL=(ALL) NOPASSWD:ALL

Now see if your connection to the clients is working:

$ ansible -m ping all
ansible-web-1.home.hokeypokeyland.org | success >> {
    "changed": false,
    "ping": "pong"
}

ansible-web-2.home.hokeypokeyland.org | success >> {
    "changed": false,
    "ping": "pong"
}

ansible-web-3.home.hokeypokeyland.org | success >> {
    "changed": false,
    "ping": "pong"
}

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

ansible all -m copy -a "src=~/.ssh/id_rsa.pub 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
  tasks:
      - 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: [ansible-client-3.home.hokeypokeyland.org]
ok: [ansible-client-5.home.hokeypokeyland.org]
ok: [ansible-client-1.home.hokeypokeyland.org]
ok: [ansible-client-2.home.hokeypokeyland.org]
ok: [ansible-client-4.home.hokeypokeyland.org]

TASK: [Installs apache web server] ********************************************
failed: [ansible-web-1.home.hokeypokeyland.org] => {"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 ********************************************************************
ansible-client-1.home.hokeypokeyland.org : ok=2    changed=0    unreachable=0    failed=0
ansible-client-2.home.hokeypokeyland.org : ok=2    changed=0    unreachable=0    failed=0
ansible-client-3.home.hokeypokeyland.org : ok=2    changed=0    unreachable=0    failed=0
ansible-client-4.home.hokeypokeyland.org : ok=2    changed=0    unreachable=0    failed=0
ansible-client-5.home.hokeypokeyland.org : ok=2    changed=0    unreachable=0    failed=0
ansible-client-6.home.hokeypokeyland.org : ok=2    changed=0    unreachable=0    failed=0

Recap

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