I have noticed this several time in Ansible, when there are similar IPs like "10.142.175.153" and "10.142.175.15" when condition like below example take both IPs and trying to execute. is this the correct way of using when condition, inventory_hostname and group or if not, what would be the correct way of doing this.
I have inventory file like this:
[m_nodes]
10.142.175.42 hostname=mss-mdpha2-mn1
10.142.175.77 hostname=mss-mdpha2-mn2
10.142.175.183 hostname=mss-mdpha2-mn3
10.142.175.57 hostname=mss-mdpha2-mn4
10.142.175.98 hostname=mss-mdpha2-mn5
[u_nodes]
10.142.175.153 hostname=mss-mdpha2-un1
10.142.175.104 hostname=mss-mdpha2-un2
[e_nodes]
10.142.175.51 hostname=mss-mdpha2-en1
10.142.175.85 hostname=mss-mdpha2-en2
[r_nodes]
10.142.175.15 hostname=mss-mdpha2-rn1
10.142.175.108 hostname=mss-mdpha2-rn2
My Playbook as below
---
- hosts: all
gather_facts: yes
tasks:
- name: Here we are checking ansible_hostname
debug:
msg: "hostname is {{ ansible_hostname }}"
when: inventory_hostname in groups['u_nodes'][0]
- name: Here we are checking inventory_hostname
debug:
msg: "hostname is {{ inventory_hostname }}"
when: inventory_hostname in groups['u_nodes'][0]
Output will be as below
TASK [Here we are checking ansible_hostname] *********************************************************************************************************************************************************************************************************************************
skipping: [10.142.175.42]
skipping: [10.142.175.77]
skipping: [10.142.175.98]
skipping: [10.142.175.183]
skipping: [10.142.175.57]
ok: [10.142.175.153] => {
"msg": "hostname is mss-mdpha2-un1"
}
skipping: [10.142.175.104]
skipping: [10.142.175.51]
ok: [10.142.175.15] => {
"msg": "hostname is mss-mdpha2-rn1"
}
skipping: [10.142.175.85]
skipping: [10.142.175.108]
TASK [Here we are checking inventory_hostname] *******************************************************************************************************************************************************************************************************************************
skipping: [10.142.175.42]
skipping: [10.142.175.77]
skipping: [10.142.175.183]
skipping: [10.142.175.57]
skipping: [10.142.175.98]
skipping: [10.142.175.104]
ok: [10.142.175.153] => {
"msg": "hostname is 10.142.175.153"
}
skipping: [10.142.175.51]
skipping: [10.142.175.85]
skipping: [10.142.175.108]
ok: [10.142.175.15] => {
"msg": "hostname is 10.142.175.15"
}
The problem is that you're using an in
comparision instead of an ==
comparision. The value of groups['u_nodes'][0]
is a string (10.142.175.153
). When you ask if stringA in stringB
, you are asking if stringB
contains stringA
anywhere in its value. For example, if stringB
is a variable that contains the phrase this is a test
, the following are all true
:
"test" in stringB
"this" in stringB
"s a t" in stringB
So when you have a string 10.142.175.153
and you ask if "10.142.175.15" in "10.142.175.153"
, the answer is yes, it is.
You want an equality check instead:
when: inventory_hostname == groups['u_nodes'][0]
This will only be true
when the two strings are exactly equal.
If all the tasks in the play should only run on the one host, a better solution is to just target that host in the play:
- hosts: u_nodes[0]
tasks:
...