cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1364
Views
2
Helpful
19
Replies

Ansible using Loop with registered variables

Netmart
Level 1
Level 1

 

Hello,

I was wondering, whether it is possible instead of creating three separate tasks, one processing one element of an array, to actually loop through an array.

 

Example:

 

vars:

      ansible_become_pass: "{{ vault_sudo_password }}"

      ansible_python_interpreter: /usr/bin/python3

 

      prefix:

         -  10.0.1.0/25

         -  10.0.3.0/25

         -  10.0.5.0/25

 

 

     - name: Task1- remove prefix

       ios_config:

         commands:

             - no ip prefix-list MTDC-nets permit {{prefix[0]}}

       when: output2.stdout is search ~prefix[0]

 

       become: true

       register: output1

 

 

     - name: Task2- remove prefix

       ios_config:

         commands:

             - no ip prefix-list cisco-nets permit {{prefix[1]}}

       when: output2.stdout is search ~prefix[1]

 

       become: true

       register: output2

 

 

     - name: Task3- remove prefix

       ios_config:

         commands:

             - no ip prefix-list MTDC-nets permit {{prefix[2]}}

       when: output2.stdout is search ~prefix[2]

 

       become: true

       register: output3

 

 

Is it possible to create only one task and looping through the elements of an array:

 

 

  vars:

      ansible_become_pass: "{{ vault_sudo_password }}"

      ansible_python_interpreter: /usr/bin/python3

 

      loop:

         -  10.0.1.0/25

         -  10.0.3.0/25

         -  10.0.5.0/25

register: prefix

 

 

     - name: Task1- remove prefix

       ios_config:

         commands:

             - no ip prefix-list Cisco-nets permit {{prefix}}

       when: output2.stdout is search ~prefix

 

       become: true

       register: output1

 

19 Replies 19

Sorry Torbjoern, this has been already tried without success.

Is there no example available illustrating looping through an array as in my case.

The material I"ve found so far in Ansible is not really matching what I am looking for.

The closest I was able to find has been:

Iterating over a list of hashes

If you have a list of hashes, you can reference subkeys in a loop. For example:

- name: Add several users
  ansible.builtin.user:
    name: "{{ item.name }}"
    state: present
    groups: "{{ item.groups }}"
  loop:
    - { name: 'testuser1', groups: 'wheel' }
    - { name: 'testuser2', groups: 'root' }


https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_loops.html

 

 

Netmart
Level 1
Level 1

Hi Torbjorn,

I am sorry, it is still failing.

I also tried the following:

     - name: Task3- remove prefix

       ios_config:

         commands:

             - "no ip prefix-list {{ item.network }} permit {{ item.prefix }}"

       loop:

               - network:"Test-nets",prefix:"10.0.0.0/25"

               - network:"Test-nets",prefix:"10.0.2.0/25"

               - network:"Test-nets",prefix:"10.0.5.0/25"

However, Ansible is still complaining that has no attribute network and prefix. And following the Ansible documentation, the loop is configured underneath the task.

 

fatal: [switch: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.parsing.yaml.objects.AnsibleUnicode object' has no attribute 'network'. 'ansible.parsing.yaml.objects.AnsibleUnicode object' has no attribute 'network'\n\nThe error appears to be in '/home/cisco/Ansible/library/lib-ios-xe-staging/site-ios-xe-CE-bgp-prefix-list-update-loop2.yml': line 34, column 8, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n     - name: Task3- remove prefix\n       ^ here\n"}

Netmart
Level 1
Level 1

Sorry, still the same.

I also tried the following:

Extract Playbook:

     - name: Task3- remove prefix

       ios_config:

         commands:

             - "no ip prefix-list {{ item.network }} permit {{ item.prefix }}"

       loop:

               - { network:"Test-nets",prefix:"10.0.0.0/25" }

               - { network:"Test-nets",prefix:"10.0.2.0/25" }

               - { network:"Test-nets",prefix:"10.0.5.0/25" }

 

 

fatal: [switch: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.parsing.yaml.objects.AnsibleUnicode object' has no attribute 'network'. 'ansible.parsing.yaml.objects.AnsibleUnicode object' has no attribute 'network'\n\nThe error appears to be in '/home/cisco/Ansible/library/lib-ios-xe-staging/site-ios-xe-CE-bgp-prefix-list-update-loop2.yml': line 34, column 8, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n     - name: Task3- remove prefix\n       ^ here\n"}

 

 

The previous setup follows the example as illustrated in the Ansible documentation. The only difference that I am using the ios_config command to iterate a command with a loop variable.

Iterating over a list of hashes

If you have a list of hashes, you can reference subkeys in a loop. For example:

- name: Add several users
  ansible.builtin.user:
    name: "{{ item.name }}"
    state: present
    groups: "{{ item.groups }}"
  loop:
    - { name: 'testuser1', groups: 'wheel' }
    - { name: 'testuser2', groups: 'root' }
 

 

netmart2
Level 1
Level 1

After reviewing it together with someone from Ansible Community, it turned out that the syntax for loop was not correct:

A space is needed after each ‘:’ and ‘,’ in the dictionary. Tip: use ``` before and after a block of YAML/code to make it easier to read.

 

It is working as followed:

 

 - ios_config:

     commands:

       - "no ip prefix-list {{ item.network }} permit {{ item.prefix }}"

   loop:

     - { network: Test-nets, prefix: "10.0.0.0/25" }

     - { network: Test-nets, prefix: "10.0.2.0/25" }

     - { network: Test-nets, prefix: "10.0.5.0/25" }

netmart2
Level 1
Level 1

The same is illustrated in Ansible documetation:

 

Iterating over a list of hashes

If you have a list of hashes, you can reference subkeys in a loop. For example:

- name: Add several users

  ansible.builtin.user:

    name: "{{ item.name }}"

    state: present

    groups: "{{ item.groups }}"

  loop:

    - { name: 'testuser1', groups: 'wheel' }

    - { name: 'testuser2', groups: 'root' }

 

https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_loops.html