Looping through Dictionaries in Ansible

Note that this post uses NTC-Ansible. Installation instructions can be found here.

In the Understanding Ansible Output Structure post we saw how to extract a single entry out of a dictionary. We saw that it’s possible to do it using the map() filter too. However, what if we want to extract all entries in the dictionary, how would we go about that? The answer is simple – use with_dict . For example, this Playbook:

Continue reading

Understanding Ansible Output Structure

Note that this post uses NTC-Ansible. Installation instructions can be found here.

In the Ansible –extra-vars post we saw just how out of control outputs can be. The Cleaning Up Ansible Loop Outputs post then demonstrated how we can remove all of the unnecessary information.

This post is aimed at answering the question, “How do we navigate through the output in order to extract the information we need?”. For example, how would we extract the management_ip information from the output below (which was generated as a result of running this Playbook):

Continue reading

Cleaning Up Ansible Loop Outputs

Note that this post uses NTC-Ansible. Installation instructions can be found here.

As we saw in the –extra-vars post, the first Playbook produces a very clean, succinct output. However, after making a minor alteration to the Playbook (I added a single loop and one additional show command), the output went from just 43 lines to over 260 lines!

While there is nothing functionally wrong with the way that Ansible displays its loop output, it is distracting and difficult to find the information you’re looking for. We can resolve this “issue” by using the default()  Jinja2 template, like so:

Continue reading

Ansible –extra-vars

Note that this post uses NTC-Ansible. Installation instructions can be found here.

Often you will see variables defined in a Playbook, a Task and/or a variables file such as group_vars or host_vars, as shown below:

Continue reading

Installing Ansible on Windows

While Ansible is not supported on Windows, it is very easy to get it up and running. The Ansible documentation provides information on how to do it using  Windows Subsystem for Linux (Beta), I’ve run into issues trying to get WSL up and running so instead opted for Cygwin.

For those who are unfamiliar with Cygwin, it is “a large collection of GNU and Open Source tools which provide functionality similar to a Linux distribution on Windows.”

Continue reading

Installing NTC Ansible

I’ve written about NTC-Ansible a couple of times already and thought now would be a good time to run you through the installation process. The github page provides some great information on the process, but I’d like to add a few extra details in this post.

Note that there are two ways in which you can install custom modules. They work as well as one another so it’s up to you which you choose.

Continue reading

Ansible Minimum Viable Playbook (MVP)

Reference: Adam’s Tech Blog

The Minimum Viable Playbook (MVP) is the shortest, most useful Ansible playbook I have. Whenever I need to write some Ansible code and I’m not entirely sure I’m doing it right (which is often), I implement it first in the MVP so I can test it quickly. Here’s my latest iteration:

Continue reading

Ansible search( ) Test

Reference: Ansible

match  requires a complete match in the string, while search  only requires matching a subset of the string.

Example

As per the selectattr( )  page, the Jinja equalto( )  Test, as well as the Ansible match( )  and search( )  Tests all work in a similar fashion.

Using this dictionary:

Continue reading

Ansible match( ) Test

Reference: Ansible

match  requires a complete match in the string, while search  only requires matching a subset of the string.

Example

As per the selectattr( )  page, the Jinja equalto( )  Test, as well as the Ansible match( )  and search( )  Tests all work in a similar fashion.

Using this dictionary:

Continue reading

Jinja2 equalto( ) Test

Reference: Jinja

Check if an object has the same value as another object.

Example

As per the selectattr( )  page, the Jinja equalto( )  Test, as well as the Ansible match( )  and search( )  Tests all work in a similar fashion.

Using this dictionary:

Continue reading