Setting Up Ansible To Manage Cluster Of VMs On Mac OS X

Last time we created a cluster of VMs on Mac OS X. In this post you will learn how to setup Ansible to manage the cluster.

Installing Ansible

If your OS X version is below El Capitan, follow instruction from Ansible.

OS X El Capitan introduced System Integrity Protection, which disallowed modification of system-level components. As result, official instruction from Ansible does not work anymore. Fortunately, Marc Weisel has written a nice post on how to Install Ansible on OS X El Capitan.

When Ansible is installed, let's configure inventory for our cluster.

Inventory

Ansible allows to configure and install packages on multiple hosts with a single command. For that it needs to know which hosts it manages. A host can be a physical box, a virtual machine (like in our case) or even a docker container.

A list of hosts that Ansible manages is called inventory and is stored in a file. Default inventory location is /etc/ansible/hosts.

In a simple form, inventory file is just a list of hosts, e.g.

one.example.com  
two.example.com  
three.example.com  

In practice, however, it is more convenient to organise hosts into groups

[frontend]
one.example.com  
two.example.com

[backend]
two.example.com  
three.example.com  
four.example.com  

Every group consists of a name in square brackets and a list of hosts that belong to it. Grouping allows to apply Ansible commands only to hosts belonging to a specific group.

Note that a host (two.example.com in our example) may belong to multiple groups.

Host aliases

When a host is known by its IP address or when there are multiple VMs accessed via different ports on the same host, it could be convenient to use host alias instead. An alias is just a name with specified ansible_host and ansible_port parameters, e.g.

server1 ansible_host=127.0.0.1 ansible_port=3022  

Aliases are exactly what we need for our cluster of VirtualBox VMs.

An alias declared in inventory file can be used in the same way as a host.

Cluster inventory

Now when we know how to create an inventory file and what aliases are, let's create inventory for our cluster. To do that, create default inventory file /etc/ansible/hosts with the following content:

server1 ansible_host=127.0.0.1 ansible_port=3022  
server2 ansible_host=127.0.0.1 ansible_port=4022

[all]
server1  
server2  

Extend the inventory file to include as many servers as VMs you have in your cluster. We created the only group all for all servers, but feel free to create as many groups as you need.

To check that your cluster is correctly configured in Ansible, run ansible all -m ping. You should get a successful response from all your cluster nodes similar to:

server2 | SUCCESS => {  
    "changed": false,
    "ping": "pong"
}
server1 | SUCCESS => {  
    "changed": false,
    "ping": "pong"
}

Now you cluster is ready to play with. Read on to learn how to manage it with Ansible ad-hoc commands.

comments powered by Disqus