# delegate\_to and when

**URL:** <https://forum.ansible.com/t/delegate-to-and-when/34121>\
**Category:** Ansible Project\
**Created:** [October 27, 2020, 8:42pm UTC](https://forum.ansible.com/t/delegate-to-and-when/34121 "2020-10-27T20:42:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Spiro\_Mitsialis](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@Spiro\_Mitsialis](https://forum.ansible.com/u/Spiro_Mitsialis)\
**Post date:** [October 27, 2020, 8:42pm UTC](https://forum.ansible.com/t/delegate-to-and-when/34121/1 "2020-10-27T20:42:53Z")

</div>

I have a playbook that executes a command on a set of routers (inventory hosts) looking for a specific IP address. The playbook uses the “meta: end\_host” on hosts that do not return a desired result. This works fine. A result is returned to the user via a URI call.  
Now I’d like to return “not found” with the URI call, when none of the routers have the IP address. Unfortunately the meta:end\_host ends the playbook.  
I’ve tried setting a “flag” on localhost as ansible\_fact but the when does not work.

- name: Set default value for ip not\_found flag  
set\_fact:  
not\_found: true  
delegate\_to: localhost  
delegate\_facts: true  
run\_once: yes

…

- name: Set default value for ip not\_found flag  
set\_fact:  
not\_found: true  
delegate\_to: localhost  
delegate\_facts: true  
run\_once: yes

…

- name: NOT FOUND  
uri:  
url: https://{{ cmdb }}/{{ test\_results }}  
method: PUT  
body\_format: json  
body: {  
“jobId”: “{{ tower\_job\_id | default(‘000’)}}”,  
“result”: “Search for IP Address {{ realtime\_ip }}”,  
“details”: “NOT FOUND”  
}  
delegate\_to: localhost  
when: not\_found

The last play does not delegate to localhost so I get an error that not\_found does not exist. What I’m I doing wrong. Any other way I can accomplish this. I dont want “failed” hosts from the playbook.

TASK [NOT FOUND] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/smitsi1/tower-network-playbooks/find\_ip\_mac\_realtime.yml:155  
fatal: [router-lab-srp.gw.mcgill.ca]: FAILED! =\> {  
“msg”: “The conditional check ‘not\_found’ failed. The error was: error while evaluating conditional (not\_found): ‘dict object’ has no attribute ‘not\_found’\n\nThe error appears to be in ‘/home/smitsi1/tower-network-playbooks/find\_ip\_mac\_realtime.yml’: line 155, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n # Check if None of the routers have an ARP then IP not found. Want to return "not found" to NetMan\n - name: NOT FOUND DEBUG\n ^ here\n”  
}

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [October 27, 2020, 8:57pm UTC](https://forum.ansible.com/t/delegate-to-and-when/34121/2 "2020-10-27T20:57:33Z")

</div>

when statements, as well as other templating in a task, uses the context of the target host in the play, not the delegated host.

You could use `hostvars.localhost.not_found` instead in the `when` statement.
