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

5,870 views
Skip to first unread message

Marcin Prączko

unread,
Apr 2, 2014, 8:04:11 AM4/2/14
to ansible...@googlegroups.com
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
---
module:
  name: "apache"
  version: "1.0.4"
  status: "Installed"


And this is a task which writes this variables to local facts on remote machine:

- name: Write variable to json file
  copy: content="{{ module }}" dest=/etc/ansible/facts.d/{{ module.name }}.fact
        owner=root group=root mode=0644


And json is written to localfacts - which is working well.

Now I am trying to do something like that in main playbook:

  roles:
    - { role: test_roleversion,
        when: "module.version > ansible_local.apache.version" }


This is working well if local facts already exists with 'version' variable. Hower when file is not there ansible complain about not defined variable:

fatal: [IP-HERE] => error while evaluating conditional: module.version > ansible_local.apache version

Question:
How to run role only if:
- module.version > ansible_local.apache.version (<- this come from local facts), or
- ansible_local.apache.version is not defined?

Following example does't work :(
  roles:
    - { role: test_roleversion,
        when: "module.version > ansible_local.apache.version or ansible_local.apache.version not defined" }

Thanks for helping in advance.

Best regards,
Marcin Praczko


Michael DeHaan

unread,
Apr 2, 2014, 11:49:17 AM4/2/14
to ansible...@googlegroups.com
Python (and Jinja) practices short circuit evaluation.   So move the "is defined" test ahead of the numeric test, and you should be ok.




--
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/ef032110-df93-47e6-a733-85717dc400d0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Marcin Prączko

unread,
Apr 3, 2014, 9:58:05 AM4/3/14
to ansible...@googlegroups.com
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) 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.

Marcin Prączko

unread,
Apr 3, 2014, 10:13:11 AM4/3/14
to ansible...@googlegroups.com
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

Marcin Prączko

unread,
Apr 3, 2014, 10:29:38 AM4/3/14
to ansible...@googlegroups.com
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.

James Cammarata

unread,
Apr 3, 2014, 10:59:59 AM4/3/14
to ansible...@googlegroups.com
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" }

--
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.

Marcin Prączko

unread,
Apr 3, 2014, 11:01:16 AM4/3/14
to ansible...@googlegroups.com
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

Reply all
Reply to author
Forward
0 new messages