Set boolean to true based on a fact

2,987 views
Skip to first unread message

Willem Bos

unread,
Dec 28, 2014, 8:20:03 AM12/28/14
to ansible...@googlegroups.com
Hi All,

I would like to be able to set a boolean to true only when two facts have certain values:

---
- hosts: all

  vars
:

    rhel5
: true
   
when:
      ansible_os_family
== "RedHat" and
      ansible_distribution_major_version
== "5"

    rhel6
: true
   
when:
      ansible_os_family
== "RedHat" and
      ansible_distribution_major_version
== "6"

  tasks
:

 
- name: Just debugging
    copy
: src=/etc/hosts dest=/tmp/test
   
when: rhel5 or rhel6


Now, the rhel5 and rhel6 booleans are always true so I guess either the 'when:' option is not valid (the parser does not complain) or I'm going about it the wrong way. Any suggestions are much appreciated.

Regards,
Willem.

Tom Bamford

unread,
Dec 29, 2014, 10:43:58 AM12/29/14
to ansible...@googlegroups.com
Hi Willem

Instead of declaring them as vars in your play, try the set_fact module instead.

Regards
Tom


--
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/dbf1f453-be5f-411b-b028-2c49316a45cb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Tomasz Kontusz

unread,
Jan 1, 2015, 7:56:33 AM1/1/15
to ansible...@googlegroups.com
Try to just set rhel5 to '{{ ansible_os_family == "RedHat" and ansible_distribution_major_version == "5" }}'

Parser didn't complain, because you just defined a variable called "when" :-)

Willem Bos <bos.h....@gmail.com> napisał:
--
Wysłane za pomocą K-9 Mail.

Willem Bos

unread,
Jan 7, 2015, 3:16:56 PM1/7/15
to ansible...@googlegroups.com
Hi Tom/Tomasz,

Thanks for your help. I will definitely look into set_fact but went with Tomasz's advise. I changed the playbook to :

---
- hosts: all

  vars:

    rhel5: '{{ ansible_os_family == "RedHat" and ansible_distribution_major_version == "5" }}'
    rhel6: '{{ ansible_os_family == "RedHat" and ansible_distribution_major_version == "6" }}'
    rhel7: '{{ ansible_os_family == "RedHat" and ansible_distribution_major_version == "7" }}'

  tasks:

  - name: Just debugging
    copy: src=/etc/hosts dest=/tmp/test
    when: rhel5 or rhel6

and that works fine. Thanks again!

Regards,
Willem.
Reply all
Reply to author
Forward
0 new messages