Reputation: 1188
I have a host_vars
file that looks like this:
---
clients:
cl1:
to_install:
- value11
- value21
to_uninstall:
- value31
cl3:
to_install:
- value12
- value22
- value32
to_uninstall:
- value42
I would like the values listed under cl1/to_install
to be installed to cl1
client only, and those under cl3/to_install
to be installed to cl3
only. This should be done by looping the values contained in to_install
. cl1 and cl3 are also defined in the hosts file.
I am trying just to print the values:
- name: test variables...
hosts: localhost
vars_files:
- path/to/hosts_vars
tasks:
- name: Print softwares to install
debug: msg="Softwares installed on {{ item.key }} are: {{ item.value.to_install }}"
with_dict:
- "{{ clients }}"
but this would print to_install
of both clients. I could also refer to the client explicitly (for example with cl1 in place of key) but the number of clients defined in host_vars
will change every time. How could I refer to the values in to_install
of a specific client only? thanks
Upvotes: 0
Views: 1275
Reputation: 23801
I think, you are looking something like this:
---
clients:
cl1:
name: My-First-Machine
to_install:
- value11
- value21
to_uninstall:
- value31
cl3:
name: My-Second-Machine
to_install:
- value12
- value22
- value32
to_uninstall:
- value42
and then in your playbook something like this:
tasks:
- debug:
msg: "Softwares installed on {{ clients[machine].name }} are: {{ clients[machine].to_install }}"
Then you can use your playbook like this for cl1 machine:
ansible-playbook -i hosts test.yml -e "machine=cl1"
will give you the result like this:
ok: [localhost] => {
"msg": "Softwares installed on My-First-Machine are: [u'value11', u'value21']"
}
PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
Also you can use your playbook like this for cl3 machine:
ansible-playbook -i hosts test.yml -e "machine=cl3"
will give you the result like this:
PLAY [local] *******************************************************************
TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": "Softwares installed on My-Second-Machine are: [u'value12', u'value22', u'value32']"
}
PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
Hope that might help you.
Upvotes: 1