skip to Main Content

I am trying to access the variable using loop, but it’s not working for me.

playbook

---
- name: test looping
  hosts: localhost
  gather_facts: False
  vars:
    repos:
      - name: repo1
        os_list:
          - centos
          - rhel
        major_distribution_list:
          - 6
          - 7
          - 8
        archs:
          - noarch
          - x86_64
  tasks:

    - include_tasks: repo-paths.yml
      with_items: "{{ repos }}"
      loop_control:
        loop_var: repo

    - debug: msg="./{{ item.0 }}/{{ item.1 }}/{{ item.2 }}/{{ item.3 }}"
      with_nested:
        - "{{ repo.os_list }}"
        - "{{ repo.major_distribution_list }}"
        - "{{ repo.name }}"
        - "{{ repo.archs }}"

But i am getting error repo variable is undefined.
Original Ouput:-

PLAY [test looping] ***********************************************************************************************************************************************************

TASK [include_tasks] **********************************************************************************************************************************************************

TASK [debug] ******************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "'repo' is undefined"}

PLAY RECAP ********************************************************************************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

Expected output:-

"msg": "./centos/6/repo1/noarch"
"msg": "./centos/6/repo1/x86_64"
"msg": "./centos/7/repo1/noarch"
"msg": "./centos/7/repo1/x86_64"
"msg": "./centos/8/repo1/noarch"
"msg": "./centos/8/repo1/x86_64"
"msg": "./rhel/6/repo1/noarch"
"msg": "./rhel/6/repo1/x86_64"
"msg": "./rhel/7/repo1/noarch"
"msg": "./rhel/7/repo1/x86_64"
"msg": "./rhel/8/repo1/noarch"
"msg": "./rhel/8/repo1/x86_64"

Please help me and please also tell if there is some alternative to do the same.

2

Answers


  1. You need to put this:

        - debug: msg="./{{ item.0 }}/{{ item.1 }}/{{ item.2 }}/{{ item.3 }}"
          with_nested:
            - "{{ repo.os_list }}"
            - "{{ repo.major_distribution_list }}"
            - "{{ repo.name }}"
            - "{{ repo.archs }}"
    

    into a file named repo-paths.yml. This is because with_items: "{{ repos }}" runs loop over single task include_tasks: repo-paths.yml and passes the repo variable to tasks inside the imported file. The loop does not affect consequent tasks in the same file.

    Login or Signup to reply.
  2. The loop_control statement is defined for the first task and relates only to it. It’s not kept thru the rest of the playbook. If you want to use it in the with_nested task, it needs to be stated again.

    The second problem you have is with_nested. Your repos variable is a list containing one element. To support iterating over the list, you should define your debug tasks in it’s own file, and use include_tasks with loop over the list. Something like this

    ---
    - name: test looping
      hosts: localhost
      gather_facts: False
      vars:
        repos:
          - name: repo1
            os_list:
              - centos
              - rhel
            major_distribution_list:
              - 6
              - 7
              - 8
            archs:
              - noarch
              - x86_64
      tasks:
      - include_tasks: my_tasks.yml
        loop: "{{ repos }}"
        loop_control:
          loop_var: repo
    
    --- my_tasks.yml
    
    - debug: msg="./{{ item.0 }}/{{ item.1 }}/{{ item.2 }}"
      with_nested:
        - "{{ repo.os_list }}"
        - "{{ repo.major_distribution_list }}"
        - "{{ repo.archs }}"
    
    --- output 
    
    ok: [localhost] => (item=[u'centos', 6, u'noarch']) => {
        "msg": "./centos/6/noarch"
    }
    ok: [localhost] => (item=[u'centos', 6, u'x86_64']) => {
        "msg": "./centos/6/x86_64"
    }
    ok: [localhost] => (item=[u'centos', 7, u'noarch']) => {
        "msg": "./centos/7/noarch"
    }
    ..
    ..
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search