# Use host var in a task in when clause

**URL:** <https://forum.ansible.com/t/use-host-var-in-a-task-in-when-clause/17172>\
**Category:** Ansible Project\
**Created:** [January 2, 2015, 10:09pm UTC](https://forum.ansible.com/t/use-host-var-in-a-task-in-when-clause/17172 "2015-01-02T22:09:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Liu1](https://avatars.discourse-cdn.com/v4/letter/m/4491bb/32.png) [@Michael\_Liu1](https://forum.ansible.com/u/Michael_Liu1)\
**Post date:** [January 2, 2015, 10:09pm UTC](https://forum.ansible.com/t/use-host-var-in-a-task-in-when-clause/17172/1 "2015-01-02T22:09:12Z")

</div>

Hey All,

I’m trying to force a task to run based on a host variable I’ve set. The goal is to only have the task run (on a host) when the “elasticsearch\_purge\_old\_indices\_node” variable is set to “yes”.

Inventory file:

[elasticsearch]  
elasticsearch1 elasticsearch\_purge\_old\_indices\_node=yes  
elasticsearch2

elasticsearch3

Task:

- name: add a nightly cronjob task to purge node  
lineinfile: dest=/etc/crontab line=“0 1 \* \* \* \* root /path/to/script.sh” state=present  
when: elasticsearch\_purge\_old\_indices\_node == “yes”

Error output:

TASK: [elasticsearch | cron job to purge old log indices] \*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [elasticsearch1] =\> One or more undefined variables: ‘dict object’ has no attribute ‘address’  
fatal: [elasticsearch2] =\> error while evaluating conditional: elasticsearch\_purge\_old\_indices\_node == “yes”  
fatal: [elasticsearch3] =\> error while evaluating conditional: elasticsearch\_purge\_old\_indices\_node == “yes”

How do I force a task to run based on a host variable I’ve set?

Thanks!

---

<div class="post-metadata">

**Author:** ![Tom\_Bamford](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@Tom\_Bamford](https://forum.ansible.com/u/Tom_Bamford)\
**Post date:** [January 3, 2015, 7:09am UTC](https://forum.ansible.com/t/use-host-var-in-a-task-in-when-clause/17172/2 "2015-01-03T07:09:50Z")

</div>

Hi Michael

Ansible considers “yes” strings as True booleans but for comparison you can use the bool filter. Perhaps try something like this:

```auto
- debug: msg="run this task"
  when: elasticsearch_purge_old_indices_node|bool

```

---

<div class="post-metadata">

**Author:** ![Michael\_Liu1](https://avatars.discourse-cdn.com/v4/letter/m/4491bb/32.png) [@Michael\_Liu1](https://forum.ansible.com/u/Michael_Liu1)\
**Post date:** [January 5, 2015, 7:44pm UTC](https://forum.ansible.com/t/use-host-var-in-a-task-in-when-clause/17172/3 "2015-01-05T19:44:57Z")

</div>

Thanks for your help Tom.

So, in this situation there is actually no difference between…

elasticsearch\_purge\_old\_indices\_node|bool  
AND  
elasticsearch\_purge\_old\_indices\_node == “yes”

The error: “One or more undefined variables: ‘dict object’ has no attribute ‘address’” is a misnomer. B/c I was using incorrectly using {{ ansible\_eth1.address }} instead of {{ ansible\_eth1.ipv4.address }} elsewhere in the same task.

To sum this up, so this post is more useful for others. If you are trying to mark a host (i.e give it a host variable) so that it’s the only one that runs a task, here is what you do…

Inventory file:

[elasticsearch]  
elasticsearch1 elasticsearch\_purge\_old\_indices\_node=yes  
elasticsearch2

elasticsearch3

Task:

- name: add a nightly cronjob task to purge node  
lineinfile: dest=/etc/crontab line=“0 1 \* \* \* \* root /path/to/script.sh” state=present  
when: elasticsearch\_purge\_old\_indices\_node is defined

The line “when: elasticsearch\_purge\_old\_indices\_node is defined” does two things.

1. ensures only the host with elasticsearch\_purge\_old\_indices\_node defined runs that task.
2. ensures you don’t get an error like “error while evaluating conditional”, because you haven’t defined elasticsearch\_purge\_old\_indices\_node for your other hosts.

Good day gentlemen!

-Mike
