# Help me understand 'when:' conditionals. Cause I seem to fumble with them quite frequently.

**URL:** <https://forum.ansible.com/t/help-me-understand-when-conditionals-cause-i-seem-to-fumble-with-them-quite-frequently/17163>\
**Category:** Ansible Project\
**Created:** [December 30, 2014, 8:22pm UTC](https://forum.ansible.com/t/help-me-understand-when-conditionals-cause-i-seem-to-fumble-with-them-quite-frequently/17163 "2014-12-30T20:22:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimmy](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@jimmy](https://forum.ansible.com/u/jimmy)\
**Post date:** [December 30, 2014, 8:22pm UTC](https://forum.ansible.com/t/help-me-understand-when-conditionals-cause-i-seem-to-fumble-with-them-quite-frequently/17163/1 "2014-12-30T20:22:49Z")

</div>

Hello everyone!

Conditionals seem to be very difficult to get right for some reason so I am hoping someone can explain why the following doesn’t seem to work. Or show me the way. 🙂

I am simply trying to check the current timezone of the server and if it doesn’t match CST then run a handler to update it. Here’s the code:

`

- name: Get timezone info  
shell: date  
register: zoneinfo  
`

Then just to make sure I have the right data I use debug to print the zoneinfo var I just registered:  
`

- name: Print zoneinfo for debug  
debug: var=“zoneinfo.stdout”

`

This outputs:

`TASK: [common | Print zoneinfo for debug] ************************************* ok: [10.10.10.10] => { "zoneinfo.stdout": "Tue Dec 30 14:11:03 CST 2014" }`

Referring to the Ansible docs I can use something like when: zoneinfo.stdout.find(‘CST’) != 1 but no matter what I try I can’t get it to work.

I have tried the following:

`

- name: If timezone is not CST then remove /etc/localtime and call set timezone handler  
file: path=/etc/localtime state=absent  
when: zoneinfo.stdout.find(‘CST’) != 1  
notify:
- set timezone  
`

I have tried not true. Is not. == 1 == 0 and several other options but its not working.

Any suggestions?

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:** [December 30, 2014, 8:45pm UTC](https://forum.ansible.com/t/help-me-understand-when-conditionals-cause-i-seem-to-fumble-with-them-quite-frequently/17163/2 "2014-12-30T20:45:21Z")

</div>

Hi

For substring comparisons, I use the ‘in’ keyword like this:

```auto
- name: Get timezone info
  shell: date
  register: zoneinfo
  changed_when: no
- name: If timezone is not CST then remove /etc/localtime and call set timezone handler
  file: path=/etc/localtime state=absent
  when: '"CST" in zoneinfo.stdout'
  notify: set timezone

```

The entire conditional is quoted in order to conform to yaml syntax. You could also add `changed_when: no` to the first task that runs `date`, so that Ansible doesn’t report it as a changed task.

In this case, I would probably use a task rather than a handler for ‘set timezone’ - should your playbook run fail, the handler will not be invoked and the timezone will not get configured.

Regards

Tom

​

---

<div class="post-metadata">

**Author:** ![Scott\_Sturdivant](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@Scott\_Sturdivant](https://forum.ansible.com/u/Scott_Sturdivant)\
**Post date:** [December 30, 2014, 8:51pm UTC](https://forum.ansible.com/t/help-me-understand-when-conditionals-cause-i-seem-to-fumble-with-them-quite-frequently/17163/3 "2014-12-30T20:51:04Z")

</div>

As with Tom, I too use the ‘in’ keyword for substring compares. That said, the reason why your ‘when’ is failing is because .find(str) is returning the index into the string where the match (if found) begins. In your case, it should be returning 20 for a successful match. For no match, it will return -1. It seems like you’ve just omitted the negative sign in your comparison.

---

<div class="post-metadata">

**Author:** ![jimmy](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@jimmy](https://forum.ansible.com/u/jimmy)\
**Post date:** [December 30, 2014, 9:04pm UTC](https://forum.ansible.com/t/help-me-understand-when-conditionals-cause-i-seem-to-fumble-with-them-quite-frequently/17163/4 "2014-12-30T21:04:51Z")

</div>

Thanks Tom and Scott,

Both replies very helpful! Thanks!
