Crackerjam
Crackerjam

Reputation: 71

Dynamically building variable names in Ansible

I have a playbook that will iterate over a set of hosts in different environments, "dev", and "prod". The environment that a host is in will change the other variables that it has. For example, this is in my vars/main.yml file:

---
folder_list_DEV: ["folder-1", "folder-2", "folder-3"]
folder_list_PROD: ["folder-1", "folder-2"]

The intention in my example is to create a series of folders on the target system, depending on which environment it is in. The code that I would like to work, but does not, looks like this:

- name: Create folders
  file:
    path: "/{{ item }}"
    state: present
  with_items: "{{ folder_list_env }}

"env" is set on execution of the playbook (-e "env=DEV").

How can I reference this "folder_list_*" variable based on value of the "env" variable?

Upvotes: 2

Views: 4036

Answers (1)

Crackerjam
Crackerjam

Reputation: 71

"{{ vars['folder_list_' + env] }}"

Upvotes: 3

Related Questions