Gaurav Ojha
Gaurav Ojha

Reputation: 1187

How can I use a wildcard to include multiple files for using environment variables in Ansible?

I am trying to do a custom install of openedx and I have a bunch of .yml files with environment variables in them inside paths that looks like this

playbooks/roles/<component-name>/defaults/main.yml

Then, while running a playbook that installs all such components, I'm using a command like this

ansible-playbook ./openedx_native.yml -e"@roles/<component-name-1>/defaults/main.yml" -e"@roles/<component-name-2>/defaults/main.yml"

Now I want to be able to use the main.yml files from all components and there are about 20-25 of them, so I'm looking for a way to include them using a wildcard, something like this

ansible-playbook ./openedx_native.yml -e"@roles/*/defaults/main.yml"

This, of course, doesn't work and Ansible throws an error like this

ERROR! the file_name '/var/tmp/configuration/playbooks/roles/*/defaults/main.yml' does not exist, or is not readable

How do I achieve this? Please help!

Upvotes: 0

Views: 3120

Answers (2)

Shubhu
Shubhu

Reputation: 41

If you have flexibility to change & re-arrange environment variables and its values in /group/all.yaml like environments: - { name: ‘development’, profile: 'small' } - { name: ‘staging’, profile: ‘medium’ } - { name: ‘production’, profile: ‘complex’ }

And then you can use this variable for any task say for example you want to create folder with environment name

    - name: create folders for Environment
  file: 
    path: "{{ target }}/{{ item.name }}"
    state: directory
    mode: 0755
  with_items: "{{ environments }}"

Upvotes: 0

Vladimir Botka
Vladimir Botka

Reputation: 68294

An option would be to find the files and include_vars.

  tasks:
    - command: "sh -c 'find {{ playbook_dir }}/roles/*/defaults/main.yml'"
      register: result
    - include_vars:
        file: "{{ item }}"
      loop: "{{ result.stdout_lines }}"

Upvotes: 2

Related Questions