Use host var in a task in when clause

165 views
Skip to first unread message

Michael Liu

unread,
Jan 2, 2015, 5:09:12 PM1/2/15
to ansible...@googlegroups.com
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!

Tom Bamford

unread,
Jan 3, 2015, 2:10:14 AM1/3/15
to ansible...@googlegroups.com

Hi Michael

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

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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/63f138f5-9447-4fbd-8cea-c368a7b1deb2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Liu

unread,
Jan 5, 2015, 2:44:57 PM1/5/15
to ansible...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages