In this blog post, I will give an overview of Ansible by delving into how it works, the installation process, creating a playbook, and running commands.
Ansible is a tool that allows you to automate all the tedious and repetitive tasks that we find in system administrators, such as:
- Setting up the computer for new use
- Installing, configuring, and entering new servers into the domain
- Managing VPN users and deleting those that no longer exist
- Or, for instance, preparing the new 300 VMs for the E-Commerce project, which should be created for tomorrow in Amazon AWS and RackSpace - in addition to the development on the Proxmox VE platform
Impressive, right? Ansible allows you to automate all this.
How Ansible Works
One thing about Ansible that sets it apart from competitors is that it does not require you to install Agents or Servers. Simply install Ansible on your device, and you're ready to start.
Ansible connects to the computers you want to configure using SSH, and sends you the instructions you want to run as well as the settings that should be applied.
All this is parallel, so it does not matter how many computers you have to configure.
The easiest method is using the package manager from your operating system.
# RedHat and Clones
$ yum install ansible
# Debian and Clones
$ apt-get install ansible
# Mac Os X with Homebrew
$ brew install ansible
Ansible works with an Inventory of your platform or servers. This Inventory is a file in which you group and list the servers according to what you like:
The hosts.cfg file contains a server group: dbs which has 3 servers. This is a simple Inventory whereas your company's Inventory can be much more complex.
Creating Recipes (Playbooks)
A playbook is a text file written in YAML where we indicate the commands that we want to execute on the clients. For example:
- hosts: servers
- name: Installs nginx web server
apt: pkg=nginx state=installed update_cache=true
- start nginx
- name: start nginx
service: name=nginx state=started
The tasks will be carried out in all the clients that are a part of the group "servants". In this case, the Nginx package will be installed, making sure that the package list is updated (update_cache = true). The service will only restart if this task is called "handlers".
To run the playbook, assume we have saved it in a playbook.yml file:
You will use the ansible.cfg file parameters.
Running Simple Commands
The first command that we are going to use will allow us to verify that we have access to the clients:
1 $ ansible -i hosts.cfg -m ping all
Another example is where we check the memory available on all servers:
1 $ ansible -i hosts.cfg all -m shell -a 'free -m' -U root
As in the two examples, we indicate the Inventory file that we are going to use: -i hosts.cfg. And in the last one, we indicate the user with which we will connect: -U root. Remember that the connection is made with SSH.
Another example that can be useful is applying the update of Time to all the servers. We can assume that they are of type Red Hat:
1 $ ansible -i hosts.cfg all -m yum -a "name=tzdata state=latest"
From the command it is important to note the following:
- -i hosts.cfg all, indicates that the hosts.cfg inventory should be used and that it will run on all servers: all
- -m yum, indicates which ansible module to use (note that we previously executed commands using the shell module, now that we want to install a package, we will use yum)
- -"name = tzdata state = latest" are the parameters that the module receives
- In the Ansible Documentation, you can find the complete list of available modules and how they are used.
Looking even at the little presented so far, we can see how powerful and useful Ansible is when it comes to managing the configuration and scheduling of servers. In the examples presented in this document, we perform tasks on only one host, but what if we had tens, hundreds, or even thousands of hosts to manage? Ansible helps a lot with those types of tasks. This has just been an introduction to Ansible and doesn't display its full capabilities. There's still much more to explore, such as Vault, which lets you encrypt sensitive information like passwords to be stored in version control. If you're interested in learning more, just stay tuned here!
I’m a passionate QA Engineer, working to achieve the best software quality and deliver great products.