Ansible - 模板字符串时列出比较问题:预期的令牌":",'}'



我正在编写一个审计剧本,将运行配置与黄金配置进行比较。Ansible允许你比较vars列表和stdout_lines列表吗?

---
- name: Logging
connection: network_cli
gather_facts: no
vars: 
golden: ["logging userinfo","logging buffered 65536"]
- name: Grab Logging Config
cisco.ios.ios_command:
commands:
- show run | i logging
register: logging_config
- ansible.builtin.debug:
var: logging_config.stdout_lines

- ansible.builtin.debug:
msg: "Items on switch but not in Standard {{ golden | difference({{ logging_config.stdout_lines }}) }}"

我得到以下错误信息:

TASK [ansible.builtin.debug] ******************************************************************************************************************************************************************************************************************

fatal: [atl01-sw-lab01]: FAILED! => {"msg": "template error while templating string: expected token ':', got '}'. String: Items on switch but not in Standard {{ golden | difference({{ logging_config.stdout_lines }}) }}. expected token ':', got '}'"}

这是logging_config_stdout_lines的输出:

"logging_config.stdout_lines": [
[
"no logging buffered",
"no logging reload",
"no logging console",
"no device-tracking logging theft"
]

我期待的是一份有差异的清单。

msg语法错误。大括号不能嵌套

{{ golden | difference({{ logging_config.stdout_lines }}) }}
正确的语法是
{{ golden | difference( logging_config.stdout_lines) }}

给定以下测试数据

logging_config:
stdout_lines:
- ["no logging buffered",
"no logging reload",
"no logging console",
"no device-tracking logging theft"]
- ["stdout line1 of cmd 2",
"stdout line2 of cmd 2",
"stdout line3 of cmd 2"]
golden:
- ["no logging buffered",
"no logging reload",
"no logging console",
"no device-tracking logging theft"]
- ["stdout line1 of cmd 2"]

声明变量以获取交换机上的项,但不是标准的">

golden_diff: "{{ logging_config.stdout_lines|difference(golden) }}"

golden_diff:
- - stdout line1 of cmd 2
- stdout line2 of cmd 2
- stdout line3 of cmd 2

有两个问题。从这个列表中,您无法分辨哪个命令不同,以及不同的细节是什么。为了解决这个问题,比较列表中的项目。声明变量

golden_diff_str: |
{% for i,j in logging_config.stdout_lines|zip(golden) %}
- {{ i|difference(j) }}
{% endfor %}
golden_diff: "{{ golden_diff_str|from_yaml }}"

golden_diff:
- []
- - stdout line2 of cmd 2
- stdout line3 of cmd 2

这个看起来更好。您可以看到在第一个命令中没有区别。在第二个命令中,有两行不符合标准。


用于测试的完整剧本示例

- hosts: localhost
vars:
logging_config:
stdout_lines:
- ["no logging buffered",
"no logging reload",
"no logging console",
"no device-tracking logging theft"]
- ["stdout line1 of cmd 2",
"stdout line2 of cmd 2",
"stdout line3 of cmd 2"]
golden:
- ["no logging buffered",
"no logging reload",
"no logging console",
"no device-tracking logging theft"]
- ["stdout line1 of cmd 2"]
golden_diff_str: |
{% for i,j in logging_config.stdout_lines|zip(golden) %}
- {{ i|difference(j) }}
{% endfor %}
golden_diff: "{{ golden_diff_str|from_yaml }}"
tasks:
- debug:
var: golden_diff
vars:
golden_diff: "{{ logging_config.stdout_lines|difference(golden) }}"
- debug:
var: golden_diff
- debug:
msg: |
Items on switch but not in Standard:
{{ golden_diff|to_nice_yaml }}

相关内容

  • 没有找到相关文章

最新更新