children / group structure in hosts file issue
hi folks, hoping someone can tell me where i am going wrong here please, have read the docs and can't see what i am doing wrong.
the goal is to have a structure like follows:
top level group: kube
children groups of kube: kube1, kube2
children groups of kube1 and kube2: kube1_m, kube1_w, kube2_m, kube2_w
lastly, each of those groups have hosts in there for their respective roles (kubernetes master node, worker node etc)
my /etc/ansible/hosts is laid out as follows:
all:
children:
kube1_m:
hosts:
k1.local.domain:
kube1_w:
hosts:
k[2:4].local.domain:
kube2_m:
hosts:
l1.local.domain:
kube2_w:
hosts:
l[2:4].local.domain:
kube1:
children:
kube1_m:
kube1_w:
kube2:
children:
kube2_m:
kube2_w:
kube:
children:
kube1:
kube2:
If i then try and run a sample playbook:
---
- hosts: all
tasks:
- name: test
when:
- ansible_hostname in groups['kube1_m']
- ansible_hostname in groups['kube2_m']
shell: ping -c 1 -w 2 8.8.8.8
ignore_errors: true
Using a syntax such as ansible-playbook -vvvvv -l kube2 test.yml it doesn't run as expected.
From the log it seems to parse the hosts file ok but but then skips running it - based on my config it should run on the host l1 as it is present in the kube2_m group.
Parsed /etc/ansible/hosts inventory source with yaml plugin
PLAYBOOK: test.yml **************************************************************************************************************************************************************
Positional arguments: playbooks/k8s/test.yml
verbosity: 5
remote_user: ansible
connection: smart
timeout: 10
become_method: sudo
tags: ('all',)
inventory: ('/etc/ansible/hosts',)
subset: kube
forks: 5
1 plays in test.yml
skipping: [l2.local.domain] => {
"changed": false,
"skip_reason": "Conditional result was False"
}
skipping: [l1.local.domain] => {
"changed": false,
"skip_reason": "Conditional result was False"
}
skipping: [l3.local.domain] => {
"changed": false,
"skip_reason": "Conditional result was False"
}
skipping: [l4.local.domain] => {
"changed": false,
"skip_reason": "Conditional result was False"
}
is there a more appropriate way for me to be doing this or am i missing something simple here?
thanks!
Posted Jan 1, 2023, 10:42 AM UTC
You are using logical AND in your
when:statement (listing multiple conditions is an implied AND). So the host would need to belong to both groups, or else the task would be skipped.Try this instead:
As u/cluelesssysadmin69 noted, be mindful of difference between
ansible_hostnameandinventory_hostname, the latter is safer if checking inventory/group membership.so I made some changes based on both bits of advice:
no changes to hosts file.
If i then run
ansible-playbook -vv -l kube1 test.ymlor with-l kube2it runs across the four hosts within each group, i.e. i get changed on all four, skipped on none. Because there is only one host in eachkube1_m/kube2_mgroup I expected it to run just on the singular node. Running it on the top levelkubegroup it runs on all 8 hosts. So i have gone from it running on none of the hosts to all of the hosts, when the desire is for it to only run on hosts present in the very bottom level subgroup (master/worker node groups).I need to read up more on the differences between inventory_hostname and ansible_hostname, just writing this comment in the spare 10 minutes i have before a family day out. thanks!
edit: i am a moron, i messed up updating the
whenline. fixing that resolves the issue.If it isn't working the the example given above, I suspect you have a more complicated inventory then what you have actually given us or something.
responding to your other comment @ https://www.reddit.com/r/ansible/comments/100h575/children_group_structure_in_hosts_file_issue/j2lay8d/