# Question about running a task based on facts

**URL:** <https://forum.ansible.com/t/question-about-running-a-task-based-on-facts/14081>\
**Category:** Ansible Project\
**Created:** [December 5, 2013, 10:27pm UTC](https://forum.ansible.com/t/question-about-running-a-task-based-on-facts/14081 "2013-12-05T22:27:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gonzalo\_Servat](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gonzalo\_Servat](https://forum.ansible.com/u/Gonzalo_Servat)\
**Post date:** [December 5, 2013, 10:27pm UTC](https://forum.ansible.com/t/question-about-running-a-task-based-on-facts/14081/1 "2013-12-05T22:27:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [December 5, 2013, 10:33pm UTC](https://forum.ansible.com/t/question-about-running-a-task-based-on-facts/14081/2 "2013-12-05T22:33:14Z")

</div>

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’

---

<div class="post-metadata">

**Author:** ![Gonzalo\_Servat](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gonzalo\_Servat](https://forum.ansible.com/u/Gonzalo_Servat)\
**Post date:** [December 5, 2013, 11:22pm UTC](https://forum.ansible.com/t/question-about-running-a-task-based-on-facts/14081/3 "2013-12-05T23:22:58Z")

</div>

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’
