【发布时间】:2018-06-24 13:29:59
【问题描述】:
我试图通过我的Ansible playbook 多次运行 Python 脚本,使用 with_items 在每次迭代时采用不同的命令行参数,但即使它遍历循环以为生成的文件采用不同的名称,但是文件内容保持不变:即只显示"show version" NX-OS的命令输出内容。
如何迭代来自上一个任务的{{ output }}?
上下文:Cisco Nexus 3k 交换机上的 NX-OS CLI 命令
tasks/main.yml:
---
- name: Run basic CLI commands on nexus 3k switch
nxos_command:
provider: "{{ nxos_provider }}"
commands: "{{ item.cmd1 }}"
with_items: "{{ commands }}"
register: output
- debug: var=output
- name: Run python script and store command output
command: python /users/aastha/play/script.py {{ item.name1 }} {{ output }}
with_items: "{{ commands }}"
vars/main.yml:
---
nxos_provider:
host: "{{ inventory_hostname }}"
username: "{{ un }}"
password: "{{ pwd }}"
transport: nxapi
timeout: 500
commands:
- cmd1: show version
name1: pre-show-version
- cmd1: show interface brief
name1: pre-interface
script.py:
import json
import sys
arg = sys.argv[2:]
print(arg)
aas='\n'.join(map(str, arg))
print aas
with open(sys.argv[1], 'w') as outfile:
outfile.write(aas)
【问题讨论】:
标签: python ansible yaml command-line-arguments