You can try the following approaches:
vars: parent: 'satellites' tasks: # functional style - debug: msg="{{hostvars[item].group_names | select('ne', parent) | first}}" with_items: "{{groups[parent]}}" # set style - debug: msg="{{hostvars[item].group_names | difference(parent) | first}}" with_items: "{{groups[parent]}}"
Also select('ne', parent) is interchangeable with reject('equalto', parent) , whichever is more readable to you. Strike>
References:
set theory operator
select and reject filters
Updated response based on comments. inspiration from this topic .
vars: parent: 'satellites' tasks: - name: build a subgroups list set_fact: subgroups="{{ ((subgroups | default([])) + hostvars[item].group_names) | difference(parent) }}" with_items: "{{groups[parent]}}" - debug: var=subgroups
output:
"subgroups": [ "nl", "us" ]
source share