Hi All,
Say I have the following facts for a server (relevant parts):
ansibletest | success >> {
“ansible_facts”: {
“ansible_em1”: {
“module”: “bnx2”,
},
“ansible_em2”: {
“module”: “tg2”,
},
“ansible_em3”: {
“module”: “bnx2”,
}
}
}
Say I only want to run a task once and only once IF at least one interface is running the module “bnx2”. How would you do it?
GS
Here’s one possible way (untested so might have a small typo or something)
- name: assume no bnx2 modules
set_fact: bnx2=no
-
name: flag we have a bnx2 module if we find any
set_fact: bnx2=yes
when: hostvars[inventory_hostname][‘ansible_’ + item][‘module’] == ‘bnx’
with_items: ansible_interfaces
-
name: do the thing
shell: echo thing
when: bnx2 == ‘yes’
Thanks Michael, I knew there would have to be a way Here’s a slightly modified (and tested) version of your suggestion that might help someone one day:
-
hosts: all
tasks:
-
name: assume no bnx2 modules
set_fact: bnx2=no
-
name: flag we have a bnx2 module if we find any
set_fact: bnx2=yes
when: “‘module’ in hostvars[inventory_hostname][‘ansible_’ + item] and hostvars[inventory_hostname][‘ansible_’ + item][‘module’] == ‘bnx2’”
with_items: ansible_interfaces
-
name: bnx2 found
debug: msg=“got one!”
when: bnx2 == ‘yes’