I have a variable containing details of host machines on my network (called 'hostlist' - I believe you call this a dictionary, but I'm not sure of the terminology. The variable is defined in a file in group_vars/all, so it's available in all playbooks (not sure if this is important).
I have a play which I would like to run only if ansible_hostname is not found in the list of host names in hostlist. The host names in hostlist are one of the attributes of the variable, but again I'm not sure if "attribute" is the right term...
Hostlist is defined as:
hostlist:
- { name: 'host1', ip_addr: '192.168.2.31', hostgrp: 'physical_workstation' }
- { name: 'host2', ip_addr: '192.168.2.32', hostgrp: 'physical_workstation' }
- { name: 'host3', ip_addr: '192.168.2.33', hostgrp: 'virtual_machine' }
The play I'm using to try to get this working is:
- name: Conditional test
debug:
msg: "ansible_hostname not found in hostlist."
when: ansible_hostname not in hostlist.name
I'm not sure of the syntax required in the condition, or if what I'm wanting is achieveable in this manner?