user2023
user2023

Reputation: 478

ansible loop with include_variable

I have a playbook, there are multiple yaml files under include_vars section of the task as you see i am defining them individually one by one. I'm wondering if i can pass them through the loop.

I went through the google and ansible doc link here but i don't find loop example however, i see with_first_found but i don't want that.

My playbook below

    ---
    - name: Creating aws host
      hosts: localhost
      connection: local
      become: yes
    
      tasks:
        - include_vars: awsvariable.yml
          no_log: true
        - include_vars: vaults/mysecrets.yml
          no_log: true
        - include_vars: vaults/mydev.yml
          no_log: true
        - include_vars: requirements.yml

Below is what i am considering

Is this doable as below?

      tasks:
        - include_vars: "{{ item }}"
          loop:
            - awsvariable.yml
            - vaults/mysecrets.yml
            - vaults/mydev.yml
            - requirements.yml

OR

      tasks:
        - include_vars: ['awsvariable.yml', 'vaults/mysecrets.yml', 'vaults/mydev.yml', 'requirements.yml']
          no_log: true
          

Please suggest, if we can better align them some other way around.

ansible version: 2.9 BR.

Upvotes: 1

Views: 4803

Answers (3)

ilias-sp
ilias-sp

Reputation: 6685

Seems the include_vars module can't work with loops. You could use the include_task module to loop over a file that has a single task to include_vars:

---
- hosts: localhost
  gather_facts: false
  vars:
    files_to_load: 
    - file1.yml
    - file2.yml
    - file3.yml

  tasks:

  - include_tasks: include_vars.yml
    loop: "{{ files_to_load }}"
    loop_control:
      loop_var: file_to_load

ant the include_vars.yml file:

- name: include vars {{ file_to_load }}
  include_vars: "{{ file_to_load }}"

UPDATE:

Regarding loop syntax that i tried and didnt work, the below attempts failed:

1st:

  - include_vars: "{{ item }}"
    loop: - "{{ files_to_load }}" 

2nd:

  - include_vars: "{{ item }}"
    loop: 
    - "{{ files_to_load }}"

i am aware that this syntax works:

- name: Include variable files
  include_vars: "{{ item }}"
  loop:
    - "vars/file.yml"
    - "vars/anotherfile.yml"

but personally i dont find convenient the fact that you need to list the loop items on task level.

Upvotes: 3

rpm192
rpm192

Reputation: 2454

Use dir to include all the files in a folder.

- name: Include variable files
  include_vars:
    dir: vars
    extensions:
      - "yml"

Alternatively can use both loop or with_items to loop through the filenames and include them.

- name: Include variable files
  include_vars: "{{ item }}"
  with_items:
    - "vars/file.yml"
    - "vars/anotherfile.yml"

Or using the newer loop.

- name: Include variable files
  include_vars: "{{ item }}"
  loop:
    - "vars/file.yml"
    - "vars/anotherfile.yml"

Upvotes: 3

Karn Kumar
Karn Kumar

Reputation: 8816

I see you are already close to your answer and yes loop should work, try to run your play in the dry run mode and set the no_log: false to see if your variables are getting expanded.

Example:

$ ansible-playbook test_play.yml --check --ask-vault-pass

your code

- include_vars: "{{ item }}"
  loop:
    - 'awsvariable.yml'
    - 'vaults/mysecrets.yml'
    - 'vaults/mydev.yml'
    - 'requirements.yml'
  no_log: true

Upvotes: 3

Related Questions