我有一个 Ansible 2.6 playbook,它试图在一系列 Cisco Nexus 交换机上运行几个命令(当我解决问题时想要添加更多)并将输出写入文件(每个交换机一个文件)。该剧本成功运行,但我似乎无法将输出转换为可读格式。在此先感谢您的任何尝试!
示例输出:
Module 1: v1.0\n Module 1: v1.0\n Module 2: v1.0\n Module 7: v2.0\n Module 8: v2.0\n Fabric Power Sequencer Firmware: Module 0: version v3 .0\n 微控制器固件:版本 v1.2.2.0\n QSFP 微控制器固件:\n 模块 1:v1.7.0.0\n 模块 2:v1.7.0.0\n SFP 微控制器固件:\n
- hosts: NX-API
gather_facts: false
connection: network_cli
order: sorted
tasks:
- name: run show version on remote devices
nxos_command:
commands:
- show version
- show interface status
register: pre_check
- name: save output to /home/ansible/PRECHECK/
copy:
content:
with_items:
- "{{ pre_check.stdout[0] }}"
- "{{ pre_check.stdout[1] }}"
dest: "/home/PRECHECK/pre_check_{{ inventory_hostname }}.txt"
你说剧本有效,但我发现了几个问题:
with_item
在副本任务生病放在这里。正如你的剧本中所写的那样,你应该得到一个充满json数据的文件(例如 {"with_items": ["whatever {{ pre_check.stdout[0] }} expands to", "whatever {{ pre_check.stdout[1] }} expands to"]}
;with_items
您将为每个开关执行两次任务。然后将无偿执行 n-1 次迭代,因为最后一次迭代将覆盖不会更改的目标文件的内容;不是有pre_check.stdout_lines
nxos_command 模块定义的变量吗?(Doc 说是)这与 stdout 相同,但作为行列表,而不是整个控制台输出。会喜欢:
- name: save output to /home/ansible/PRECHECK/
copy:
content: "{{ (pre_check.stdout_lines[0] + pre_check.stdout_lines[1]) | join('\n') }}"
dest: "/home/PRECHECK/pre_check_{{ inventory_hostname }}.txt"
工作。假设stdout_lines
是一个列表列表。但我相信:
- name: save output to /home/ansible/PRECHECK/
copy:
content: "{{ (pre_check.stdout[0] + pre_check.stdout[1] }}"
dest: "/home/PRECHECK/pre_check_{{ inventory_hostname }}.txt"
应该也可以,假设最后有一个 final \n
,pre_check.stdout[0]
否则你需要自己添加它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句