我制作了一本Ansible游戏书,它循环遍历.yaml文件,并为界面添加说明。问题是在每个设置命令Ansible提交之后,等待提交然后配置下一个接口。如果我在拥有10个成员的VC上运行它,它将永远需要。任何人都知道如何在完成所有更改后才能提交剧本吗?
---
- name: Change configuration using junos_config module
hosts: test
connection: local
gather_facts: no
tasks:
- include_vars: /home/ansible/interfaces.yaml
- name: Change description on interfaces based on a list of variable
junos_config:
lines:
- "set interfaces {{ item.name }} description \"{{ item.description }}\""
comment: "Update description of interface {{ item.name }}"
with_items: "{{ interfaces }}"
register: result
- debug: var=result
我改变了现在看起来像这样的剧本:
---
- name: Change configuration using junos_config module
hosts: test
connection: local
gather_facts: no
tasks:
- include_vars: /home/ansible/playbook-core/interfaces.yaml
- name: Change description on interfaces based on a list of variable
junos_config:
lines:
- "{{ lines_template }}"
- "set interfaces {{ item.name }} description \"{{ item.description }}\""
comment: "Update description of port"
vars:
lines_template: "[ {% for interface in interfaces %}'set interfaces {{ item.name }} description \"{{ item.description }}\"',{% endfor %} ]"
with_items: "{{ interfaces }}"
register: result
- debug: var=result
当我运行它时,我收到一个错误:
An exception occurred during task execution. To see the full traceback, use -
vvv. The error was: TypeError: sequence item 0: expected string, list found
failed: [10.63.255.71] (item={u'name': u'ge-0/0/1', u'description': u'Set by
ansible'}) => {"changed": false, "item": {"description": "Set by ansible",
"name": "ge-0/0/1"}, "module_stderr": "Traceback (most recent ca
ll last):\n File \"/tmp/ansible_wVNymo/ansible_module_junos_config.py\",
line 402, in <module>\n main()\n File
\"/tmp/ansible_wVNymo/ansible_module_junos_config.py\", line 371, in main\n
diff = configure_device(module, warnings, candidate)\n File
\"/tmp/ansible_wVNymo/ansible_module_junos_config.py\", line 293, in
configure_device\nreturn load_config(module, candidate, warnings, **kwargs)\n
File \"/tmp/ansible_wVNy
mo/ansible_modlib.zip/ansible/module_utils/junos.py\", line 199, in
load_config\nTypeError: sequence item 0: expected string, list found\n",
"module_stdout": "", "msg": "MODULE FAILURE", "rc": 0}
答案 0 :(得分:1)
将循环逻辑移动到Jinja2模板:
- name: Change description on interfaces based on a list of variable
junos_config:
lines: "{{ lines_template }}"
- "set interfaces {{ item.name }} description \"{{ item.description }}\""
comment: "Update description of interfaces"
vars:
lines_template: "[ {% for interface in interfaces %}'set interfaces {{ interface.name }} description \"{{ interface.description }}\"',{% endfor %} ]"
可以跳过最后,
,但似乎Ansible弥补了这一点。
以上代码未经过Juniper测试,但它应该能够生成您想要的内容。