Executing Ansible

We have so far seen the installation of Ansible, listed the hosts in the inventory, and set up our Ansible playbook; now, we can run Ansible to configure our VMs.

For this, we will run the Ansible tool with the ansible-playbook command, like this:

ansible-playbook -i inventory playbook.yml

The basic options of this command are as follows:

  • The -i argument with the inventory file path 
  • The path of the playbook file

The following is the execution of this command:

The execution of this command applies the playbook to the hosts in the inventory in several steps:

  1. Gathering facts: Ansible checks that the hosts are reachable.
  2. The tasks' playbook is executed on hosts.
  3. PLAY Recap: This is the status of the changes that were executed on each host; the value of this status can be as follows:

ok

This is the number of playbook tasks that have been correctly applied to the host.

changed

This is the number of changes applied.

unreachable

The host is unreachable.

failed

Execution failed on this host.

 

If we need to upgrade our playbook to add or modify middleware on our VMs, during the second execution of Ansible with this upgraded playbook, it can be noticed that Ansible did not reapply the complete configuration of the VMs, but only applied the differences. 

The following screenshot shows the second execution of Ansible with no changes made to our playbook:

We can see in the recap step that Ansible didn't change anything on the hosts (changed=0).

We can also add some useful options to this command to provide the following:

  • A preview of Ansible changes before applying the changes
  • More logs in the execution output

These options are not only important for the playbook development phase, but also for debugging them in case of errors during their execution.

Now, let's look at how to use these preview options.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.33.87