# Group variables conditional based on OS distribution release

**URL:** <https://forum.ansible.com/t/group-variables-conditional-based-on-os-distribution-release/20693>\
**Category:** Ansible Project\
**Created:** [February 27, 2016, 5:51am UTC](https://forum.ansible.com/t/group-variables-conditional-based-on-os-distribution-release/20693 "2016-02-27T05:51:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Darren\_S](https://avatars.discourse-cdn.com/v4/letter/d/59ef9b/32.png) [@Darren\_S](https://forum.ansible.com/u/Darren_S)\
**Post date:** [February 27, 2016, 5:51am UTC](https://forum.ansible.com/t/group-variables-conditional-based-on-os-distribution-release/20693/1 "2016-02-27T05:51:05Z")

</div>

Hi,

After various docs and some trial and error, I can’t figure out how to handle setting a variable differently when the target operating system version is newer than a certain release. OpenBSD 5.8 and newer have replaced sudo(8) with doas(1), so I’d like to set ansible\_become\_method correctly based on the OS version. The only idea I could come up with was this template logic in /etc/ansible/group\_vars/openbsd, which is clearly incorrect:

{% if {{ ansible\_distribution\_release | version\_compare(‘5.8’, ‘\>=’) }} %}  
ansible\_become\_method: doas  
{% else %}  
ansible\_become\_method: sudo  
{% endif %}

What is the correct/cleanest way to ansible\_become\_method conditionally for a given OS family based on the value of ansible\_distribution\_release?

Ansible manager: ansible 2.0.1.0 on OpenBSD 5.7

Managed nodes: OpenBSD 5.7, OpenBSD 5.8, various Linux

Thanks,

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 27, 2016, 6:10am UTC](https://forum.ansible.com/t/group-variables-conditional-based-on-os-distribution-release/20693/2 "2016-02-27T06:10:36Z")

</div>

​Y​  
ou cannot use jinja2 commands in vars file, the templates are not used in the vars file, they are just assigned as strings that later get recognized as templates and templated. But you can do this:

ansible\_become\_method: “{{ (ansible\_distribution\_release|version\_compare(‘5.8’, ‘\>=’))|ternary(‘doas’, ‘sudo’) }}”

—​

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [February 27, 2016, 2:48pm UTC](https://forum.ansible.com/t/group-variables-conditional-based-on-os-distribution-release/20693/3 "2016-02-27T14:48:15Z")

</div>

We do stuff with inventory variales and dicts, like

&nbsp;&nbsp;become\_method:  
&nbsp;&nbsp;&nbsp;&nbsp;5.7: doas  
&nbsp;&nbsp;&nbsp;&nbsp;5.8: doas  
&nbsp;&nbsp;&nbsp;&nbsp;6.0: sudo

&nbsp;&nbsp;ansible\_become\_method: become\_method[ansible\_distribution\_release]

You can't do the \>= thing this way, you have to enumerate all the options.  
(Or you could have a default and only enumerate the ones that are  
different from the defaults.) You might have to quote the numbers if  
ansible\_distribution\_release is a string, I've gotten tripped up by stuff  
like that once in a while but don't remember if this is one of them. :^)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.

---

<div class="post-metadata">

**Author:** ![Darren\_S](https://avatars.discourse-cdn.com/v4/letter/d/59ef9b/32.png) [@Darren\_S](https://forum.ansible.com/u/Darren_S)\
**Post date:** [February 28, 2016, 5:08am UTC](https://forum.ansible.com/t/group-variables-conditional-based-on-os-distribution-release/20693/4 "2016-02-28T05:08:01Z")

</div>

This looks elegant, thanks. In my environment when I run the playbook using  
this configuration, it fails with the variable showing undefined:

$ ansible-playbook site.yml --limit sinoptik --tags snmpd

PLAY

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [March 1, 2016, 5:29am UTC](https://forum.ansible.com/t/group-variables-conditional-based-on-os-distribution-release/20693/5 "2016-03-01T05:29:49Z")

</div>

yes, you need to gather facts before that variable is available, you might want to use |default filter to avoid the error

---

<div class="post-metadata">

**Author:** ![Darren\_S](https://avatars.discourse-cdn.com/v4/letter/d/59ef9b/32.png) [@Darren\_S](https://forum.ansible.com/u/Darren_S)\
**Post date:** [March 7, 2016, 5:58am UTC](https://forum.ansible.com/t/group-variables-conditional-based-on-os-distribution-release/20693/6 "2016-03-07T05:58:16Z")

</div>

I'm still finding myself stumped on this.

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [March 7, 2016, 6:17pm UTC](https://forum.ansible.com/t/group-variables-conditional-based-on-os-distribution-release/20693/7 "2016-03-07T18:17:58Z")

</div>

Hi Darren,

> I'm still finding myself stumped on this.

See my answer below.

> > yes, you need to gather facts before that variable is available, you might want to use |default filter to avoid the error

> ansible\_become\_method: "{{  
> (ansible\_distribution\_release|version\_compare('5.8',  
> '\>='))|ternary('doas', 'sudo') }}"

I think Brian meant something like this:

ansible\_become\_method: "{{  
(ansible\_distribution\_release|version\_compare('5.8',

> '\>='))|ternary('doas', 'sudo')|default(sudo) }}"

This way, when ansible\_distribution\_release is undefined, the  
default(sudo) strikes and sets a default. Once  
ansible\_distribution\_release is defined, default does nothing anymore.

Johannes
