with_item和access数组在每次执行循环时传递单独的值

时间:2016-12-21 19:57:09

标签: ansible

我有以下代码,我试图找出如何使用迭代将数组访问中的每个值传递给eos_config。我想仅在未路由的所有访问端口上运行脚本eos_config。

- name: change description
  hosts: arista
  connection: local

  vars:
    access: []

  tasks:
    - name: show interface
      eos_command:
        commands: 
          - sh interfaces ethernet {{ item.if }} switchport | json
        provider: "{{ cli }}"
      with_items:
        - { if: '1'}
        - { if: '2'}
        - { if: '3'}
      register: showif

    - name: set access fact
      set_fact:
        access: "{{ access + [item] }}"
      with_items:
        - "{{showif.results[0].stdout[0].switchports.Ethernet1.enabled}}"
        - "{{showif.results[1].stdout[0].switchports.Ethernet2.enabled}}"
        - "{{showif.results[2].stdout[0].switchports.Ethernet3.enabled}}"
      when: '"mode" is defined'

    - debug: var=access

    - name: change description
      eos_config:
        lines:
          - description ansible_test
        parents: interface ethernet {{item.if}}
        provider: "{{ cli }}"
        authorize: yes
      with_items:
        - { if: '1' }
        - { if: '2' }
        - { if: '3' }
      when: 'access == true'

我能够提取有关每个端口的信息,并在端口为access时指定true值,或在routed时指定false。

TASK [debug] *******************************************************************
task path: /etc/ansible/arista.yaml:28
ok: [10.10.10.101] => {
    "access": [
        true, 
        true, 
        false
    ]
}

现在我想在运行这部分代码时访问此变量,但不知道该怎么做。

  - name: change description
      eos_config:
        lines:
          - description ansible_test
        parents: interface ethernet {{item.if}}
        provider: "{{ cli }}"
        authorize: yes
      with_items:
        - { if: '1' }
        - { if: '2' }
        - { if: '3' }
      when: 'access == true'

我正在获取脚本最后一部分的输出:

TASK [change description] ******************************************************
task path: /etc/ansible/arista.yaml:30
skipping: [10.10.10.101] => (item={u'if': u'2'})  => {
    "changed": false, 
    "item": {
        "if": "2"
    }, 
    "skip_reason": "Conditional check failed", 
    "skipped": true
}
skipping: [10.10.10.101] => (item={u'if': u'1'})  => {
    "changed": false, 
    "item": {
        "if": "1"
    }, 
    "skip_reason": "Conditional check failed", 
    "skipped": true
}
skipping: [10.10.10.101] => (item={u'if': u'3'})  => {
    "changed": false, 
    "item": {
        "if": "3"
    }, 
    "skip_reason": "Conditional check failed", 
    "skipped": true
}

有谁知道如何实现这个目标或我缺少什么?

1 个答案:

答案 0 :(得分:0)

我无法测试此代码,因为我无法访问eos设备,但您可以使用with_together尝试以下逻辑同时迭代两个列表:

- name: change description
  hosts: arista
  connection: local

  vars:
    interfaces: ['1','2','3']

  tasks:
    - name: show interface
      eos_command:
        commands: 
          - sh interfaces ethernet {{ item }} switchport | json
        provider: "{{ cli }}"
      with_items: "{{ interfaces }}"
      register: showif

    - name: change description
      eos_config:
        lines:
          - description ansible_test
        parents: "interface ethernet {{ item.0 }}"
        provider: "{{ cli }}"
        authorize: yes
      when: item.1.stdout[0].switchports.Ethernet1.enabled
      with_together:
        - "{{ interfaces }}"
        - "{{ showif.results }}"