# simplify always the same conditionals - evaluate contents of a string in conditionals (changed\_when, failed\_when, ...)

**URL:** <https://forum.ansible.com/t/simplify-always-the-same-conditionals-evaluate-contents-of-a-string-in-conditionals-changed-when-failed-when/33132>\
**Category:** Ansible Project\
**Created:** [June 9, 2020, 8:58pm UTC](https://forum.ansible.com/t/simplify-always-the-same-conditionals-evaluate-contents-of-a-string-in-conditionals-changed-when-failed-when/33132 "2020-06-09T20:58:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_K](https://avatars.discourse-cdn.com/v4/letter/m/7993a0/32.png) [@Michael\_K](https://forum.ansible.com/u/Michael_K)\
**Post date:** [June 9, 2020, 8:58pm UTC](https://forum.ansible.com/t/simplify-always-the-same-conditionals-evaluate-contents-of-a-string-in-conditionals-changed-when-failed-when/33132/1 "2020-06-09T20:58:16Z")

</div>

Hi there,

while working with fortinet.fortios I’ve engineered that the modules need some help to track the changed and failed  
status. So I’ve added the following parameters to each module called:

`

- name: “{{ device }}{{ vdom }} configure static route”  
fortios\_router\_static:  
vdom: “{{ vdom }}”  
state: “present”  
router\_static:  
device: “{{ item.device }}”  
dst: “{{ item.dst }}”  
gateway: “{{ item.gateway }}”  
seq\_num: “{{ item.name }}”  
status: “enable”  
with\_items: “{{ router\_static }}”  
register: r  
changed\_when: “‘meta’ in r and r[‘meta’][‘status’] == ‘success’ and r[‘meta’][‘revision\_changed’] == true”  
failed\_when: “‘meta’ not in r or r[‘meta’][‘status’] == ‘error’”

`

As it is kinda hard to read and maintain, I wonder if there is a simpler way without changing the module. Something like this comes to mind:

`

`

- set\_fact  
`my_changed_when: "'meta' in r and r['meta']['status'] == 'success' and r['meta']['revision_changed'] == true"`  
`my_failed_when: "'meta' not in r or r['meta']['status'] == 'error'"`

- name: “{{ device }}{{ vdom }} configure static route”  
fortios\_router\_static:  
vdom: “{{ vdom }}”  
state: “present”  
router\_static:  
device: “{{ item.device }}”  
dst: “{{ item.dst }}”  
gateway: “{{ item.gateway }}”  
seq\_num: “{{ item.name }}”  
status: “enable”  
with\_items: “{{ router\_static }}”  
register: r  
changed\_when: “{{ `my_changed_when }}"`  
failed\_when: ``”{{ `my_failed_when````````}}"`

` `

The problem is, that this is not getting evaluated. Further, an error is thrown that jinja template tags are not allowed in conditionals anymore. Does anyone have another  
idea how to solve this?

Greetings,  
Michael

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [June 10, 2020, 8:39pm UTC](https://forum.ansible.com/t/simplify-always-the-same-conditionals-evaluate-contents-of-a-string-in-conditionals-changed-when-failed-when/33132/2 "2020-06-10T20:39:42Z")

</div>

instead of set\_fact (which forces static evaluation BEFORE the task,  
use `vars:` which is lazy evaluation (on us). Also you are using  
templating in the wrong places.

failed\_when: my\_failed\_when|bool  
vars:

&nbsp;&nbsp;&nbsp;&nbsp;my\_failed\_when: "{{'meta' not in r or r['meta']['status'] == 'error'}}"

---

<div class="post-metadata">

**Author:** ![Michael\_K](https://avatars.discourse-cdn.com/v4/letter/m/7993a0/32.png) [@Michael\_K](https://forum.ansible.com/u/Michael_K)\
**Post date:** [June 11, 2020, 12:24pm UTC](https://forum.ansible.com/t/simplify-always-the-same-conditionals-evaluate-contents-of-a-string-in-conditionals-changed-when-failed-when/33132/3 "2020-06-11T12:24:22Z")

</div>

Hi Brian,

thanks for your advice. I’ve added the variable as host var

`

ansible\_fortios\_default\_changed\_when: “{{ ‘meta’ in r and r[‘meta’][‘status’] == ‘success’ and r[‘meta’][‘revision\_changed’] == true }}”  
ansible\_fortios\_default\_failed\_when: “{{ ‘meta’ not in r or r[‘meta’][‘status’] == ‘error’ }}”

`

and to the play:

`  
…

register: r  
changed\_when: ansible\_fortios\_default\_changed\_when|bool  
failed\_when: ansible\_fortios\_default\_failed\_when|bool

`

That worked very well - thanks! Could you elaborate on your note "Also you are using templating in the wrong places. " a bit more?

Greetings,  
Michael
