2

I want to iterate over array and pass each array element value to role from playbook but it is not working in ansible, Can some one help


---
#play book
- name: create config for instance
  hosts: all
  vars:
    LIST: [Asia, Americas, Artic, Antartic ,Oceania,Europe,Africa]
  connection: local
  roles:
    - role: create_config
      debug:
        msg : "{{ item }}"
      vars:
        VENUE: "{{ item }}"

      with_items:
        - "{{ LIST }}"


## Role
- name: create directory structure
  file:
    path: "{{item}}"
    state: directory
    mode: 0755
  with_items:
    - "{{dest_folder}}/{{instance_name}}/{{VENUE}}"




I am getting below error

ansible-playbook  -i inventory/AlgoTest_SP  create_pkg_1.yml 

PLAY [create config for instance] **************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************
ok: [localhost]

TASK [create_config : create directory structure] *******************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "{{ item }}: 'item' is undefined"}

PLAY RECAP ****************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
guido
  • 18,864
  • 6
  • 70
  • 95
Sachin
  • 51
  • 4
  • I am not ansible expert, but you are trying to using roles calling a task and using the module debug. – c4f4t0r Sep 03 '21 at 12:51

1 Answers1

1

To be able to loop over roles, you need the include_role task, as in:

- name: create config for instance
  hosts: localhost
  vars:
    LIST: [Asia, Americas, Artic, Antartic ,Oceania,Europe,Africa]
  tasks:
    - include_role: 
        name: create_config
      vars:
        VENUE: "{{ item }}"
      with_items:
        - "{{LIST}}"

cat roles/create_config/tasks/main.yml 
- debug:
    msg: "{{VENUE}}"

- name: create directory structure
  file:
    path: "{{item}}"
    state: directory
    mode: 0755
  with_items:
    - "{{inventory_hostname}}/{{VENUE}}"

Resulting in:

$ ansible-playbook  69045121.yml 
PLAY [create config for instance] ********************************************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [include_role : create_config] ******************************************************************************************************************************************************************************************************************************************************************************************

TASK [create_config : debug] *************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "Asia"
}

TASK [create_config : create directory structure] ****************************************************************************************************************************************************************************************************************************************************************************
changed: [localhost] => (item=localhost/Asia)

TASK [create_config : debug] *************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "Americas"
}

[...] and so on

As a final note, you could, and should, likely handle this differently. Also, you have clash in the item variable name from the outer (playbook) and inner (role) with_items, you can use loop_var to set a different looping varname.

guido
  • 18,864
  • 6
  • 70
  • 95
  • Thanks It works for me, I am getting below warning though TASK [creat_config : create directory structure] ******************************************************************************************************************* [WARNING]: The loop variable 'item' is already in use. You should set the `loop_var` value in the `loop_control` option for the task to something else to avoid variable collisions and unexpected behavior. – Sachin Sep 05 '21 at 04:51
  • Thanks Guido, I resolved that warning by adding loop_var now https://stackoverflow.com/questions/62272678/ansible-the-loop-variable-item-is-already-in-use – Sachin Sep 05 '21 at 05:07