Pages

Friday, January 29, 2016

Ansible – Playbooks

In our previous articles we have seen how we can use Ansible to perform basic commands on a remote machine. In this article we will see playbooks. Playbooks are a completely different way to use Ansible.

Playbooks are nothing but configuration details that are defined in a text files which tell us what actions needs to be done on the remote machines. For the Ansible to execute on the remote machine we don’t need any additional software other than Python. The tasks that need to be performed on a remote machine are written inside a playbook in YAML format.

Now lets create a Sample playbook for the article purpose. As a Scenario we can create a playbook for executing a Echo command on the remote machine.  In order to run a command on a remote machine we can use

[root@vx111a ansible]# ansible dev -m command -a "/bin/echo hello Sample"
172.16.202.96 | success | rc=0 >>
hello Sample

The ‘dev’ is something that we configure in our inventory file with a list of IP address. Now if we check the above command we had used the module command to run a command on the remote machine. Now we will create a  Sample Playbook for the same above command using the YAML format.

Now lets create a Ansible directory which we will use for our article. The next main step is to tell Ansible about the remote machines that we need to talk. For this we need to create a Ansible hosts file also called as inventory file. This file contains list of Ip address that are defined in groups that we can use them while running Ansible commands. By default the location of this file is /etc/Ansible/hosts. This is a global file that we can use but Ansible lets you create your hosts file and pass that to the Ansible command.

When we run the Ansible command, the command will always check for the Ansible.cfg file in the local directory that it is being run. If the file is found it will override the global configuration with the local values.

Once the directory is created, create a ansible.cfg file with the values as,
[root@vx111a ansible]# cat ansible.cfg
[defaults]
hostfile=hosts

We have defined the hostfile configuration option with the value of the hosts, within the defaults group.

Now lets define the hosts file like this,
[root@vx111a ansible]# cat hosts
[dev]
172.16.202.96 ansible_ssh_user=root

In the host file we have defined a “dev” group with an IP address and the user with which we need to login to that machine and perform actions on.

Now once the configuration of the Ansible.cfg and hosts file is done ,we can test our configuration using the basic command as,

[root@vx111a ansible]# ansible -m ping 'dev'
172.16.202.96 | success >> {
    "changed": false,
    "ping": "pong"
}

It was a Success, what we did was to ping the servers defined in the hosts file as group “dev”. You can compare the IP in the above command output with the one in the hosts file.
Now we will define a play book for the same above command execution.

[root@vx111a ansible]# cat sample-playbook.yml
---
- hosts: dev
  tasks:
    - name: run echo Command
      command: /bin/echo Hello Sample PlayBook

This above is the sample play book that we need to write. The hosts: dev declaration is at the top, which tells Ansible that we are using the dev hosts group. Next is the list of the tasks. In the above example we have one task with the name “run echo Command”. This is simply a description to allow users understand what the task does. Finally the command: /bin/echo Hello Sample PlayBook use the module command for running the command /bin/echo with the arguments “Hello Sample PlayBook”.

Now lets run the playbook as,

[root@vx111a ansible]# ansible-playbook sample-playbook.yml

PLAY [dev] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [172.16.202.96]

TASK: [run echo Command] ******************************************************
changed: [172.16.202.96]

PLAY RECAP ********************************************************************
172.16.202.96              : ok=2    changed=1    unreachable=0    failed=0  

Once the above playbook is completed, we can see the status as ok. The most important thing to notice is that the playbook does not return the output of the module.

Now lets write another playbook example where will add some debug statements for the output that is generated. For the same above playbook we add a register

[root@vx111a ansible]# cat sample-playbook1.yml
---
- hosts: dev
  tasks:
    - name: Echo a Command
      command: /bin/echo Hello
      register: out

    - debug: var="{{ out.stdout }}"
    - debug: var="{{ out.stderr }}"


In the above playbook other than name and command we have a register field. We have registered a variable by the name out for which we assigned the out.stdout (output of the command). Now once we execute the play book using the Ansible we can see something like this,

[root@vx111a ansible]# ansible-playbook sample-playbook1.yml

PLAY [dev] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [172.16.202.96]

TASK: [Echo a Command] ********************************************************
changed: [172.16.202.96]

TASK: [debug var="{{ out.stdout }}"] ******************************************
ok: [172.16.202.96] => {
    "var": {
        "Hello": "Hello"
    }
}

TASK: [debug var="{{ out.stderr }}"] ******************************************
ok: [172.16.202.96] => {
    "var": {
        "": ""
    }
}

PLAY RECAP ********************************************************************
172.16.202.96              : ok=4    changed=1    unreachable=0    failed=0  


We can see the stdout as ”Hello”.

This is the basic introduction of the playbooks in Ansible. Hope this helps. In the next articles we will see advanced examples on Ansible playbooks

1 comment :