Tag: Ansible

Ansible - ipaddr filter

Ansible's ipaddr filter is something you will be using a lot when working with network devices and their configurations. This filter provides an interface to netaddr Python package and it allows you to do almost anything that package can do, which is to say, a lot. You need to have netaddr library installed on your machine, Ansible filter won't work without it. I will show a few examples of how this filter could be used. You can refer to Ansible documentation [1] for the full list of available options. Contents: Check if the string is a valid IP address Get...

Continue reading »

While using loops in Ansible, by default, output contains the entire content of the item being processed. This can result in a great amount of verbosity if the item is a dictionary or is otherwise long. For example, when looping through the output of the "show ip bgp sum", we want to use just an IP of the peer as an input in another task. When "Get routes for each of the BGP peers" task executes Ansible will display the entire data structure linked to that peer. Task with the loop: - name: Get routes for...

Continue reading »

Problem The other day one of my playbooks reported an issue with connecting to one of the switches. What seemed odd is that the this playbook ran against this box without any problems ten minutes earlier. I double checked my password and all the permissions, all seemed to be in order. Just as a test, I tried to run this playbook for other devices, worked like a charm. It was time to turn the debug and full verbosity (-vvvv), on. Here's what I found in the logs: 2017-10-08 11:24:01,589 p=30940 u=przemek | PLAYBOOK: arista_test.yml...

Continue reading »

Knowing date and timestamp comes handy in many tasks involving automation. We can use these for logging purposes, include in the names of output files, and directories. We can even decide if the task should execute at all based on the current timestamp.   Ansible "ansible_date_time" fact There are a number of ways in which we can retrieve the values for the current date and timestamp. For example, we can take advantage of the facts that Ansible gathers at the beginning of each playbook. Ansible will record the current date and time in the variable "ansible_...

Continue reading »

In this blog post I'll show how to add items to lists and dictionaries, when using loops, and across tasks. Normally when trying to add a new item to the variable, while in the loop, or between tasks, Ansible will ovewrite the values, keeping the result of the last iteration. For example, let's see what will be the result of running the following playbook: --- - name: Append to list hosts: localhost vars: cisco: - CiscoRouter01 - CiscoRouter02 - CiscoRouter03 - CiscoSwitch01 arista: - AristaSwitch01 - AristaSwitch02 - AristaSwitch03 tasks: - name: Add Cisco and Airsta devices to the list...

Continue reading »