I am trying to figure out if there is a way to use Ansible to automate the creation of /etc/hosts files depending on which group the Ansible host belongs to. I am creating multiple test Kubernetes clusters that will use hosts files for DNS resolution.
When I use [all], as Ansible iterates through the hosts file can it search which Group the host belongs to and create an /etc/hosts from the group hostnames and IPs.
For example:
Ansible interacts with: kube-g2-master3
`
inventory_hostname = kube-g2-master3
`
Finds kube-g2-master3 belongs [Group 2]
Creates /etc/hosts on kube-g2-master3 using the inventory_hostname and ansible_hosts from [Group 2]
I am trying to figure out if there is a way to use Ansible to automate the
creation of /etc/hosts files depending on which group the Ansible host
belongs to. I am creating multiple test Kubernetes clusters that will use
hosts files for DNS resolution.
When I use* [all]*, as Ansible iterates through the hosts file can it
search which Group the host belongs to and create an* /etc/hosts* from the
group hostnames and IPs.
Space in group names is not allowed.
For example:
1. Ansible interacts with: *kube-g2-master3*
inventory_hostname = kube-g2-master3
2. Finds *kube-g2-master3* belongs* [Group 2]*
Groups in Ansible is just for grouping variables, and the grouping is lost when Ansible is running.
What you can do is check if a host is in a group, but you would need to check every group.
when: "'kube-g2-master3' in groups['group2']"
And the list of all group is in the special variable "group_names".
3. Creates */etc/hosts* on *kube-g2-master3* using the *inventory_hostname*
and *ansible_hosts* from* [Group 2]*