Ansible 条状空白 [英] Ansible strip white space

查看:18
本文介绍了Ansible 条状空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在 nxos 设备上运行一些命令时,输出的末尾有一个空格.我必须将输出与现有的变量列表进行比较.最后的空格导致比较失败.如何在字符串列表中使用 .strip() 函数?

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

输出结果如下:

"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.

推荐答案

要应用函数来列出元素,请使用 map 过滤器.要去除空格,请使用 trim 过滤器.>

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

"{{ runconfserafter.stdout_lines | map('trim') | list }}"

这篇关于Ansible 条状空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆