loop_var value not defined error

18 views
Skip to first unread message

Mayank Goyal

unread,
Jan 11, 2021, 1:28:35 AM1/11/21
to Ansible Project
Hi All,
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.

Abhijeet Kasurde

unread,
Jan 11, 2021, 1:48:20 AM1/11/21
to ansible...@googlegroups.com
Hi Mayank,

loop_var variable has a lifetime for a given task. It won't be accessible after the task is complete.

Either you can use import_tasks to club all related functionality or use `set_fact` like

- set_fact:
repo: "{{ repos[0] }}"

- debug: msg="./{{ item.0 }}/{{ item.1 }}/{{ item.2 }}/{{ item.3 }}"
with_nested:
- "{{ repo.os_list }}"
- "{{ repo.major_distribution_list }}"
- "{{ repo.name }}"
- "{{ repo.archs }}"
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/17f6f1df-5c4f-43da-a6d7-7e28f0485cabn%40googlegroups.com.


--
Thanks,
Abhijeet Kasurde
Reply all
Reply to author
Forward
0 new messages