I have written a role to create users. Now I wanted to modify the role to also give the option to remove users. What looked very simple in the beginning is now giving me constant headaches.
Here is my role:
---
- name: Dictionary playbook example
hosts: localhost
vars:
my_default_values:
users: &def
state: 'present'
force: 'yes'
remove: 'no'
my_values:
users:
- username: bla
<<: *def
state: absent
- username: bla2
<<: *def
tasks:
- debug: var=my_default_values
- debug: var=my_values
Here is the output:
PLAY [Dictionary playbook example] ********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"my_default_values": {
"users": {
"force": "yes",
"remove": "no",
"state": "present"
}
}
}
TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"my_values": {
"users": [
{
"force": "yes",
"remove": "no",
"state": "absent",
"username": "bla"
},
{
"force": "yes",
"remove": "no",
"state": "present",
"username": "bla2"
}
]
}
}
PLAY RECAP ********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0
rescued=0 ignored=0
This is working fine as long as I always define <<: *def below every - username in my role. But this is problematic if let's say I want to have the users variables defined in an external file users.yml. I guess I would need some kind of loop?
In the end I want to use the role with Katello and so the users are defined inside of Katello. This is why I need to get this working in a more dynamic fashion.