Dang! @Karl Auer beat me to this example. :)
Depending on your needs and comfort level, here are two examples with sample output.
The first "userlistA" is closer to your original example and has each list with a name that is not consistent with the server_type variable. That leads to the first three "when" clauses to set the userlist variable based on the server_type provided.
The second section using "userlistB" uses the server_type variable to select the correct user list from the dictionary - it's a single task in the playbook as opposed to three or more for the first method.
Here is my example playbook named "dynvar.yml":
---
- hosts: localhost
gather_facts: false
vars:
server_type: standard_server
userlistsA:
list1:
users: 'user1,user2,user3'
list2:
users: 'user4,user5,user6'
list3:
users: 'user7,user8,user1'
userlistsB:
db_server:
users: 'user1,user2,user3'
web_server:
users: 'user4,user5,user6'
standard_server:
users: 'user7,user8,user1'
tasks:
- name: "Users for db_server - option A"
set_fact:
userlist: '{{ userlistsA.list1 }}'
when: server_type=="db_server"
- name: "Users for web_server - option A"
set_fact:
userlist: '{{ userlistsA["list2"] }}'
when: server_type=="web_server"
- name: "Users for standard_server - option A"
set_fact:
userlist: '{{ userlistsA.list3 }}'
when: server_type=="standard_server"
- debug:
msg: "Option A - Users setup for {{ server_type }} - {{ userlist }}"
- name: "Users for {{ server_type }} - option B"
set_fact:
userlist: '{{ userlistsB[server_type] }}'
- debug:
msg: "Option B - Users setup for {{ server_type }} - {{ userlist }}"
Running this with `ansible-playbook ./dynvar.yml` produces this output:Output:
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost
does not match 'all'
PLAY [localhost] ******************************************************************************************
TASK [Users for db_server - option A] *********************************************************************
skipping: [localhost]
TASK [Users for web_server - option A] ********************************************************************
skipping: [localhost]
TASK [Users for standard_server - option A] ***************************************************************
ok: [localhost]
TASK [debug] **********************************************************************************************
ok: [localhost] => {
"msg": "Option A - Users setup for standard_server - {'users': 'user7,user8,user1'}"
}
TASK [Users for standard_server - option B] ***************************************************************
ok: [localhost]
TASK [debug] **********************************************************************************************
ok: [localhost] => {
"msg": "Option B - Users setup for standard_server - {'users': 'user7,user8,user1'}"
}
PLAY RECAP ************************************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0