We are using Ansible as a way to setup and manage a k8s cluster.
To deploy application we have a playbook that uses localhost
to call kubectl
to deploy our application.
We are doing some refactoring and I would like a make localhost
a part of a group in Ansible host file.
Currently we have:
localhost ansible_connection=local ansible_user=root
[k8s_masters]
k8s-master01 ansible_host=x
[k8s_nodes]
k8s-node01 ansible_host=x
k8s-node02 ansible_host=x
[k8s_cluster:children]
k8s_masters
k8s_nodes
I would like to add the host localhost
to the group k8s_cluster
.
So that when I run my playbook Ansible wil use k8s_cluster
group_var when executing on localhost
. I know that I can use the group_var/all
but our setup is somewhat complicated. And we want to use this setup to match the right variables depending on the inventory that is used.
Is this at all possible to create a group of multiple hosts that also includes localhost
?
I tried to create a group prod
:
[prod:children]
all
localhost
or
[prod:children]
k8s_cluster
localhost
but that is not allowed.
You may have a look into How to build your inventory and Grouping groups: parent/child group relationships
You can create parent/child relationships among groups. Parent groups are also known as nested groups or groups of groups.
This means you can't put a host into a child group without having em in a group before. Otherwise you would receive an error message like
[WARNING]: * Failed to parse k8s.ini with ini plugin:
k8s.ini:15: Section [prod:children] includes undefined group: localhost
An example inventory k8s.ini
[control_node]
localhost
[k8s_masters]
k8s-master01
[k8s_nodes]
k8s-node01
k8s-node02
[k8s_cluster:children]
k8s_masters
k8s_nodes
[prod:children]
control_node
k8s_cluster
called via
ansible-inventory -i k8s.ini --graph
will result into the requested output of
@all:
|--@prod:
| |--@control_node:
| | |--localhost
| |--@k8s_cluster:
| | |--@k8s_masters:
| | | |--k8s-master01
| | |--@k8s_nodes:
| | | |--k8s-node01
| | | |--k8s-node02
|--@ungrouped:
You may also have a look into the output of
ansible-inventory -i k8s.ini prod --graph
ansible-inventory -i k8s.ini control_node --graph
Furthermore I recommend to use the real hostname, in example test.example.com
instead of localhost
within the inventory and have also a look into the meaning of Default groups.
Further Reading