# iisue when evaluating a complex var in become

**URL:** <https://forum.ansible.com/t/iisue-when-evaluating-a-complex-var-in-become/26280>\
**Category:** Ansible Project\
**Created:** [December 13, 2017, 11:55am UTC](https://forum.ansible.com/t/iisue-when-evaluating-a-complex-var-in-become/26280 "2017-12-13T11:55:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_C1](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@Mike\_C1](https://forum.ansible.com/u/Mike_C1)\
**Post date:** [December 13, 2017, 11:55am UTC](https://forum.ansible.com/t/iisue-when-evaluating-a-complex-var-in-become/26280/1 "2017-12-13T11:55:33Z")

</div>

Hi all,

im trying to use a ‘complex’ expression to evaluate if a task should run as root or not.

Here is what i have defined:

```auto
debian_based_system: ansible_os_family == 'Debian' or ansible_os_family == 'Linuxmint'

```

My tasks look like this

```auto
- debug: var=ansible_os_family
- debug: var={{ debian_based_system }}
- debug: var=debian_based_system

- shell: echo $EUID
args:
executable: /bin/bash
become: "{{ debian_based_system }}"

- shell: echo $EUID
args:
executable: /bin/bash
become: true

- shell: echo $EUID
args:
executable: /bin/bash
become: debian_based_system

```

and when i run them, i get

```auto
TASK [test : debug] *********************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"ansible_os_family": "Linuxmint"
}

TASK [test : debug] *********************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"ansible_os_family == 'Debian' or ansible_os_family == 'Linuxmint'": true
}

TASK [test : debug] *********************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"debian_based_system": "ansible_os_family == 'Debian' or ansible_os_family == 'Linuxmint'"
}

TASK [test : command] *******************************************************************************************************************************************************************************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "echo $EUID", "delta": "0:00:00.003180", "end": "2017-12-13 11:48:16.830302", "rc": 0, "start": "2017-12-13 11:48:16.827122", "stderr": "", "stderr_lines": [], "stdout": "1000", "stdout_lines": ["1000"]}

TASK [test : command] *******************************************************************************************************************************************************************************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "echo $EUID", "delta": "0:00:00.002833", "end": "2017-12-13 11:48:16.988274", "rc": 0, "start": "2017-12-13 11:48:16.985441", "stderr": "", "stderr_lines": [], "stdout": "0", "stdout_lines": ["0"]}

TASK [test : command] *******************************************************************************************************************************************************************************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "echo $EUID", "delta": "0:00:00.002869", "end": "2017-12-13 11:48:17.143074", "rc": 0, "start": "2017-12-13 11:48:17.140205", "stderr": "", "stderr_lines": [], "stdout": "1000", "stdout_lines": ["1000"]}

```

which means that only `become: true` gave my task sudo access (and not any of the other two).

If i change the definition to `debian_based_system: '{{ ansible_os_family == "Debian" or ansible_os_family == "Linuxmint" }}'`, then i get correct results for the first two shell tasks but i also get a warning about using {{ }} in when statements.

```auto
TASK [test : debug] *********************************************************************************************************************************************************************************************************************************************************
[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ ansible_os_family == "Debian" or ansible_os_family == "Linuxmint" }}

ok: [localhost] => {
"ansible_os_family": "Linuxmint"
}

TASK [test : debug] *********************************************************************************************************************************************************************************************************************************************************
[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ ansible_os_family == "Debian" or ansible_os_family == "Linuxmint" }}

ok: [localhost] => {
"true": "VARIABLE IS NOT DEFINED!"
}

TASK [test : debug] *********************************************************************************************************************************************************************************************************************************************************
[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ ansible_os_family == "Debian" or ansible_os_family == "Linuxmint" }}

ok: [localhost] => {
"debian_based_system": true
}

TASK [test : command] *******************************************************************************************************************************************************************************************************************************************************
[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ ansible_os_family == "Debian" or ansible_os_family == "Linuxmint" }}

changed: [localhost] => {"changed": true, "cmd": "echo $EUID", "delta": "0:00:00.002737", "end": "2017-12-13 11:51:29.057079", "rc": 0, "start": "2017-12-13 11:51:29.054342", "stderr": "", "stderr_lines": [], "stdout": "0", "stdout_lines": ["0"]}

TASK [test : command] *******************************************************************************************************************************************************************************************************************************************************
[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ ansible_os_family == "Debian" or ansible_os_family == "Linuxmint" }}

changed: [localhost] => {"changed": true, "cmd": "echo $EUID", "delta": "0:00:00.003077", "end": "2017-12-13 11:51:29.227123", "rc": 0, "start": "2017-12-13 11:51:29.224046", "stderr": "", "stderr_lines": [], "stdout": "0", "stdout_lines": ["0"]}

TASK [test : command] *******************************************************************************************************************************************************************************************************************************************************
[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ ansible_os_family == "Debian" or ansible_os_family == "Linuxmint" }}

changed: [localhost] => {"changed": true, "cmd": "echo $EUID", "delta": "0:00:00.003351", "end": "2017-12-13 11:51:29.386325", "rc": 0, "start": "2017-12-13 11:51:29.382974", "stderr": "", "stderr_lines": [], "stdout": "1000", "stdout_lines": ["1000"]}

```

What is the correct way to define this so that it works and i dont get the warning ?

Cheers,  
Mike

```auto
$ ansible --version
ansible 2.3.1.0
config file =
configured module search path = Default w/o overrides
python version = 2.7.12 (default, Nov 20 2017, 18:23:56) [GCC 5.4.0 20160609]

```

---

<div class="post-metadata">

**Author:** ![Mike\_C1](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@Mike\_C1](https://forum.ansible.com/u/Mike_C1)\
**Post date:** [December 18, 2017, 12:12pm UTC](https://forum.ansible.com/t/iisue-when-evaluating-a-complex-var-in-become/26280/2 "2017-12-18T12:12:53Z")

</div>

bump

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 19, 2017, 6:05am UTC](https://forum.ansible.com/t/iisue-when-evaluating-a-complex-var-in-become/26280/3 "2017-12-19T06:05:38Z")

</div>

You don't say where you define this, but debian\_based\_system is literally equal to the string  
ansible\_os\_family == 'Debian' or ansible\_os\_family == 'Linuxmint'

I thing you are trying to make debian\_based\_system true or false, to do this you need to enclose them in expression syntax {{ }}

ansible\_os\_family == "{{ 'Debian' or ansible\_os\_family == 'Linuxmint' }}"

---

<div class="post-metadata">

**Author:** ![Mike\_C1](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@Mike\_C1](https://forum.ansible.com/u/Mike_C1)\
**Post date:** [December 20, 2017, 11:21am UTC](https://forum.ansible.com/t/iisue-when-evaluating-a-complex-var-in-become/26280/4 "2017-12-20T11:21:21Z")

</div>

> > Hi all,
> > 
> > im trying to use a ‘complex’ expression to evaluate if a task should  
> > run as  
> > root or not.
> > 
> > Here is what i have defined:
> > 
> > ```auto
> > debian_based_system: ansible_os_family == 'Debian' or ansible_os_family
> > ==
> > 'Linuxmint'
> > 
> > ```
> 
> You don’t say where you define this, but debian\_based\_system is  
> literally equal to the string  
> ansible\_os\_family == ‘Debian’ or ansible\_os\_family == ‘Linuxmint’
> 
> I thing you are trying to make debian\_based\_system true or false, to do  
> this you need to enclose them in expression syntax {{ }}
> 
> ansible\_os\_family == “{{ ‘Debian’ or ansible\_os\_family == ‘Linuxmint’  
> }}”
> 
> –  
> Kai Stian Olstad

Thanks for the reply.

> You don’t say where you define this  
> I define this in `defaults/main.yml`. I didnt mention it as im not sure how that could make a difference.

> I thing you are trying to make debian\_based\_system true or false, to do  
> this you need to enclose them in expression syntax {{ }}

> ansible\_os\_family == “{{ ‘Debian’ or ansible\_os\_family == ‘Linuxmint’  
> }}”

im failing to see how what you are suggesting is different to what i’ve mentioned already with:

> If i change the definition to `debian_based_system: '{{ ansible_os_family == "Debian" or ansible_os_family == "Linuxmint" }}'`,

This gives me a warning ‘when statements should not include jinja2 templating delimiters’

Cheers,  
Mike

---

<div class="post-metadata">

**Author:** ![Mike\_C1](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@Mike\_C1](https://forum.ansible.com/u/Mike_C1)\
**Post date:** [December 20, 2017, 12:30pm UTC](https://forum.ansible.com/t/iisue-when-evaluating-a-complex-var-in-become/26280/5 "2017-12-20T12:30:40Z")

</div>

I think this is fixed with [https://github.com/ansible/ansible/pull/25092](https://github.com/ansible/ansible/pull/25092), so ill ignore the warnings for now and then fix them when i update ansible version.
