0

我制作了一个循环播放 .yaml 文件并向接口添加描述的 Ansible 剧本。问题是在 Ansible 提交每个 set 命令后,等待提交然后配置下一个接口。如果我在一个有 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}
4

1 回答 1

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 弥补了这一点。

上面的代码没有用瞻博网络测试,但它应该产生你想要的。

于 2018-02-26T20:27:20.737 回答