Ansible is a configuration management software that allows to manage your servers very easily. It focuses on keeping things simple, making it easy to begin with it.
What do you need to begin with Ansible?
In the Ansible world there are only two things: your computer where Ansible runs and servers to manage.
- The computer only needs the Ansible client installed.
- Servers only need an SSH access and Python installed. Basically all Unix servers match the requirements. OpenStack GNU/Linux instances are known to work out of the box with Ansible.
How does Ansible work?
Ansible uses a standard SSH connection to manage the servers. The client on your computer initiates a connection to all your servers in parallel and uses Python modules to configure them.
Hosts, playbooks, roles and tasks
Ansible encourages you to reuse the configuration you write by defining different elements, mainly: hosts, playbooks, roles and tasks.
Let’s take a real life example that will make that clear. Imagine that you want to deploy a small cluster of web servers.
First you define a list of hosts that will be part of this cluster. This list is called an inventory file.
The next step is defining a role. Each role is a list of tasks and configuration files.
Imagine a role “HTTP server”. It will contain the following tasks:
- Install the HTTP server Nginx
- Upload its configuration
- Start the service
Finally you write a playbook that connects your hosts with the role(s) you want to give them. In our example our hosts will have two roles, the common one and the HTTP server one. Now let’s do that on OpenStack!
Using Ansible on OpenStack
The first thing you need to do is installing Ansible, then you will be able to clone a git repository containing the files mentioned in the previous paragraphs and run the playbook on real servers.
On your local computer install Ansible. As it is just a Python software it can
be installed like any other Python program with pip. As a good practice you
should install it in a virtualenv.
Note, on a fresh Debian-like system you can install the requirements:
apt-get install python-dev python-virtualenv python-pip
Assuming you are using an Unix OS with Python and virtualenv installed it is as easy as:
mkdir ansible && cd ansible virtualenv venv source venv/bin/activate pip install ansible
Congratulations, you are ready to use Ansible.
Clone the git repository
Clone the repository containing the web cluster example:
git clone https://github.com/NicolasLM/ansible-runabove-example.git cd ansible-runabove-example
The repository has the following layout:
├── hosts ├── webserver.yml └── roles ├── common │ ├── files │ │ └── ntp.conf │ ├── handlers │ │ └── main.yml │ └── tasks │ └── main.yml └── nginx ├── files │ └── testsite ├── handlers │ └── main.yml └── tasks └── main.yml
hostsis your inventory of hosts
webserver.ymlis the playbook
rolesis a directory containing two roles: common and nginx
The common role is meant to be shared by all your host on OpenStack. It installs a NTP server and upgrades packages. The nginx role is the one installing the HTTP server.
Launch some OpenStack instances
Launch a few OpenStack instances using Ubuntu 14.04. You can pick the flavor you want, even a Sandbox will be fine. It is important that you launch them with the key of your current computer running Ansible.
Once they are running copy their IP to the
host file of the repository like
[webservers] 220.127.116.11 18.104.22.168 22.214.171.124
Run the playbook
You can simply run the playbook:
ansible-playbook -i hosts webserver.yml
The process can take up to a few minutes, once Ansible has setup all your hosts you can connect to
http://<IP of a instance> and check that Nginx is responding.
You can check more examples in the Github repository
Ansible has a lot of built-in modules and a great community. You can find them on IRC at #ansible.