On Mon, 30 Dec 2019 01:31:54 -0800 (PST) Yehuda Pinhas <[email protected]> wrote:
> I still don't understand what these code lines used for in my case:
> tasks:
> - add_host:
> name: "{{ item.1 }}"
> groups: "{{ item.0.group }}"
> loop: "{{ lookup('subelements', my_hosts, 'hosts') }}"
Short answer: These code lines create the groups you want.
Details:
1) Quoting from "add_host"
https://docs.ansible.com/ansible/latest/modules/add_host_module.html
"Use variables to create new hosts and groups in inventory for use in later
plays of the same playbook."
2) For example, with this list of dictionaries
"my_hosts": [
{
"group": "POC_ENV",
"hosts": [
"POC_ENV_01",
"POC_ENV_02",
"POC_ENV_03"
]
},
{
"group": "Nexus",
"hosts": [
"nexus_01",
"nexus_02",
"nexus_03"
]
}
]
3) The debug task
- debug:
msg:
- "name: {{ item.1 }}"
- "groups: {{ item.0.group }}"
loop: "{{ lookup('subelements', my_hosts, 'hosts') }}"
gives output that explains the plugin "subelements"
"msg": [
"name: POC_ENV_01",
"groups: POC_ENV"
]
"msg": [
"name: POC_ENV_02",
"groups: POC_ENV"
]
"msg": [
"name: POC_ENV_03",
"groups: POC_ENV"
]
"msg": [
"name: nexus_01",
"groups: Nexus"
]
"msg": [
"name: nexus_02",
"groups: Nexus"
]
"msg": [
"name: nexus_03",
"groups: Nexus"
]
4) The add_host tasks
- add_host:
name: "{{ item.1 }}"
groups: "{{ item.0.group }}"
loop: "{{ lookup('subelements', my_hosts, 'hosts') }}"
create groups that can be used in the next plays. For example the play
- hosts: POC_ENV
tasks:
- debug:
var: inventory_hostname
gives
ok: [POC_ENV_01] => {
"inventory_hostname": "POC_ENV_01"
}
ok: [POC_ENV_02] => {
"inventory_hostname": "POC_ENV_02"
}
ok: [POC_ENV_03] => {
"inventory_hostname": "POC_ENV_03"
}
HTH,
-vlado
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/20191230114110.2d910243%40gmail.com.
pgpe7t7h0sr12.pgp
Description: OpenPGP digital signature
