Ansible带空格 [英] Ansible strip white space

查看:449
本文介绍了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天全站免登陆