with_item and access array to pass separate value each time loop is executed

29 views
Skip to first unread message

marcin_...@hotmail.com

unread,
Dec 19, 2016, 2:01:23 PM12/19/16
to Ansible Project
Hi,

I have following code and trying to figure out how I can pass each value from array access to the eos_config using iteration. I want to run the scripts eos_config only on all access port not routed.

- 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'


I am able to extract info about each port and assigne true value if port is access or false when is routed

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

Now I would like to access this variable when running this part of code but dont know really how to do it.

  - 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'

I am getting following output for last part of the script

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
}


Does any one know how to achieve this goal or what I am missing?

Regards,

Marcin
Reply all
Reply to author
Forward
0 new messages