Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible strip white space

Tags:

ansible

jinja2

When I try to run some commands on nxos devices, the output has a white space at the end. I have to compare the output to an existing variable list. The whitespace at the end is causing the comparison to go false. How to make use of .strip() function in a list of strings?

- name: Current TACACS server host before
    nxos_command:
      commands:
        - sh run | include 'tacacs-server host'
register: runconfserafter

- debug:
    var: runconfserafter

The output of this comes up like this:

"stdout_lines": [
        [
            "tacacs-server host 1.1.1.1 key 7 \"HelloWorld\" ",
            "tacacs-server host 2.2.2.2 key 7 \"HelloWorld\""
        ],
     ]

When I compare this line with my desired variables, I can't get it matched because of the white space on the first line at the end.

like image 665
Chrysna Avatar asked Jun 25 '18 01:06

Chrysna


People also ask

How do you split in Ansible?

Split Lines in Ansible You can use the 'split()' function to divide a line into smaller parts. The output will be a list or dictionary. This is a Python function and not a Jinja2 filter. For example, in the below example, I am splitting the variable 'split_value' whenever a space character is seen.


1 Answers

To apply a function to list elements use map filter. To strip whitespace use trim filter.

"{{ runconfserafter.stdout_lines | map('trim') | list }}"
like image 172
techraf Avatar answered Nov 24 '22 05:11

techraf