# Run role based on local facts - if variable is not defined.

**URL:** <https://forum.ansible.com/t/run-role-based-on-local-facts-if-variable-is-not-defined/14917>\
**Category:** Ansible Project\
**Created:** [April 2, 2014, 12:04pm UTC](https://forum.ansible.com/t/run-role-based-on-local-facts-if-variable-is-not-defined/14917 "2014-04-02T12:04:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcin\_Praczko](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Marcin\_Praczko](https://forum.ansible.com/u/Marcin_Praczko)\
**Post date:** [April 2, 2014, 12:04pm UTC](https://forum.ansible.com/t/run-role-based-on-local-facts-if-variable-is-not-defined/14917/1 "2014-04-02T12:04:11Z")

</div>

Good morning.

I am trying using local facts for tracking version of the role, and run this role only if version increased (version kept locally within role comparing with version from local fatcs).

So this is how is working:  
Role folder

- vars/main.yml
- tasks/main.yml

So this is example for: vars/main.yml

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 2, 2014, 3:49pm UTC](https://forum.ansible.com/t/run-role-based-on-local-facts-if-variable-is-not-defined/14917/2 "2014-04-02T15:49:17Z")

</div>

Python (and Jinja) practices short circuit evaluation. So move the “is defined” test ahead of the numeric test, and you should be ok.

---

<div class="post-metadata">

**Author:** ![Marcin\_Praczko](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Marcin\_Praczko](https://forum.ansible.com/u/Marcin_Praczko)\
**Post date:** [April 3, 2014, 1:58pm UTC](https://forum.ansible.com/t/run-role-based-on-local-facts-if-variable-is-not-defined/14917/3 "2014-04-03T13:58:05Z")

</div>

Hi,

Can I have a bit of more light of this please?  
I checked what means short circuit evaluation ([http://en.wikipedia.org/wiki/Short-circuit\_evaluation](http://en.wikipedia.org/wiki/Short-circuit_evaluation)) however my evaluation is not working.

roles:

- { role: test\_roleversion,  
when: “ansible\_local.apache.version not defined or module.version \> ansible\_local.apache.version” }

Return error:  
fatal: [IP-HERE] =\> error while evaluating conditional: ansible\_local.apache.version not defined or module.version \> ansible\_local.apache.version

Thanks for help.  
Marcin P.

---

<div class="post-metadata">

**Author:** ![Marcin\_Praczko](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Marcin\_Praczko](https://forum.ansible.com/u/Marcin_Praczko)\
**Post date:** [April 3, 2014, 2:13pm UTC](https://forum.ansible.com/t/run-role-based-on-local-facts-if-variable-is-not-defined/14917/4 "2014-04-03T14:13:11Z")

</div>

Hi,

Seems be working - that to be some TABS instead of spaces on the begining of line:

roles:

- { role: test\_roleversion,  
when: ansible\_local.apache.version is not defined or module.version \> ansible\_local.apache.version}Best regards,

Thanks for help.  
Marcin Praczko

---

<div class="post-metadata">

**Author:** ![Marcin\_Praczko](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Marcin\_Praczko](https://forum.ansible.com/u/Marcin_Praczko)\
**Post date:** [April 3, 2014, 2:29pm UTC](https://forum.ansible.com/t/run-role-based-on-local-facts-if-variable-is-not-defined/14917/5 "2014-04-03T14:29:38Z")

</div>

Hi,

Don’t know why worked only once, no luck with this still.  
Givining up.: ( ☹

This syntax at a least run condition everytime and not reporting error:  
roles:

- { role: test\_roleversion,  
when: { ansible\_local.apache.version is not defined or module.version \> ansible\_local.apache.version } }

Everything else which I tried after running command on remote node: rm -f /etc/ansible/facts/\* failed with error:

error while evaluating conditional: ansible\_local.apache.version is not defined or module.version \> ansible\_local.apache.version

Marcin.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [April 3, 2014, 2:59pm UTC](https://forum.ansible.com/t/run-role-based-on-local-facts-if-variable-is-not-defined/14917/6 "2014-04-03T14:59:59Z")

</div>

The YAML parser may be reading the ‘\>’ as the line continuation character, so you may need to wrap that in quotes:

- { role: test\_roleversion, when: “ansible\_local.apache.version is not defined or module.version \> ansible\_local.apache.version” }

---

<div class="post-metadata">

**Author:** ![Marcin\_Praczko](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Marcin\_Praczko](https://forum.ansible.com/u/Marcin_Praczko)\
**Post date:** [April 3, 2014, 3:01pm UTC](https://forum.ansible.com/t/run-role-based-on-local-facts-if-variable-is-not-defined/14917/7 "2014-04-03T15:01:16Z")

</div>

Hi,

I think problem lies in multi level of variable:  
Checking if ansible\_local is defined working as expected, however checking  
ansible\_local.somevars - here automatically raise and error.

This means that variable must be check from top level to bootom.

If ansible\_local is not defined and ansible\_local.somevar is not define and if ansible\_local.somevar.a is not defined …

Best regards,  
Marcin
