Getting output from Cisco switches using Ansible and Jinja Replace filter

Hello

Today something fairly simple. My task was to retrieve the location of a switch using Ansible and write it to a file such that only the location is recorded rather than the whole output line with that location.

Unfortunately there is no Genie parser for snmp location, so i had to be a bit creative. I’ve added the following task to my playbook:

- name: show run | i snmp-server location
      block:
        - name: Get location info
          ios_command:
            commands: show run | i snmp-server location
          register: show_location

Now i had my variable that I could use in Jinja, but the were several problems.

Firstly, using that variable name only in a Jinja template will produce a lot of content on top of the whole output, so we need to use the .stdout_lines property of that variable.

Secondly, the variable is stored in a multidimensional array, so we need to extract content from it.

Thirdly, we need to use the Jinja replace filter to get rid of what we don’t want to finally have.

Finally, this is what we’ve produced in our jinja template. It’s ready to be used to generate a report (as a template task in Ansible)

{{ hostvars[host].show_location.stdout_lines[0][0]|replace('snmp-server location', '') }}

Skomentuj

Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. Wyloguj /  Zmień )

Zdjęcie na Google

Komentujesz korzystając z konta Google. Wyloguj /  Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Wyloguj /  Zmień )

Zdjęcie na Facebooku

Komentujesz korzystając z konta Facebook. Wyloguj /  Zmień )

Połączenie z %s