Ansible Basics

Ansible is simple open source IT engine which automates application deployment, intra-service orchestration, cloud provisioning, and many other IT tools. In our particular case, it is used to provision and configure the compute nodes in the cluster. On RHEL and CentOS Ansible is installed via ansible package.

[root@master ~]# yum -y install ansible.noarch
[root@master ~]# ansible
Usage: ansible <host-pattern> [options]
...ERROR! Missing target hosts

When executed, Ansible will attempt to establish a ssh connection with host, unless ansible_connection=local is specified. For instance, the ping module (ping command):

[root@master ~]# ansible localhost -m ping
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

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

will success for localhost but any other unspecified host will fail. For instance:

[root@master ~]# ansible master -m ping
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

 [WARNING]: Could not match supplied host pattern, ignoring: master

In order to include other hosts and more importantly groups of hosts, it is necessary to create or edit the inventory host file.

Creating an inventory

Here the Video Transcript

The default inventory host file is located in /etc/ansible/hosts. However, any custom inventory host path can be specified with the -i flag. For instance, with the following inventory file:

[root@master ~]# cat inventory
[master]
localhost    ansible_connection=local

the command ping for the group [master]

[root@master ~]# ansible master -m ping -i inventory
localhost | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

will succeed. Now, let us create the group [compute] and include all the compute nodes in the cluster:

[root@master ~]# cat inventory
[master]
localhost    ansible_connection=local

[compute]
c01
c02
c03
c04

Then, execute the command ping for the group compute. The correct output should be:

[root@master ~]# ansible compute -m ping -i inventory
c01 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
c02 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
c04 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
c03 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

Note that the ping command is executed concurrently in all the compute nodes. The concurrency can be changed via file config (/etc/ansible/ansible.cfg) by changing the number of forks (200 by default). Or by using the flag -f when executing the command.