Przemek

Przemek

@ttl255
47 posts

Ansible - loops and verbose output

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 »

Ansible - fixing issues with control socket

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 »

Ansible - Getting date and timestamp

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 »

Ansible - Appending to lists and dictionaries

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 »

Python: Relative and absolute paths

Often we need to read from, or write to, files in directories that are local to our script. For instance, we could use the below code to save to file all possible values that can be stored in one byte: my_bytes = bytearray(list(range(0,256))) with open("output/local.bin", 'wb') as f: f.write(my_bytes) Our script, local_file.py, is located in the following directory: /home/python [wintermute@hive python]$ cd /home/python [wintermute@hive python]$ ls local_file.py output Now, let's say we launch this script from within its location: [wintermute@hive python]...

Continue reading »