# Variables in "when" statement

**URL:** <https://forum.ansible.com/t/variables-in-when-statement/17245>\
**Category:** Ansible Project\
**Created:** [January 13, 2015, 8:29am UTC](https://forum.ansible.com/t/variables-in-when-statement/17245 "2015-01-13T08:29:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wojciech\_Korzenny](https://avatars.discourse-cdn.com/v4/letter/w/eada6e/32.png) [@Wojciech\_Korzenny](https://forum.ansible.com/u/Wojciech_Korzenny)\
**Post date:** [January 13, 2015, 8:29am UTC](https://forum.ansible.com/t/variables-in-when-statement/17245/1 "2015-01-13T08:29:08Z")

</div>

Hi,

I want to use variable in when statement, for example:

- include: some\_playbook.yml  
when: “{{ SOME\_VARIABLE }}” == “true”

where:  
SOME\_VARIABLE - is given as false/true via --extra-vars

Unfortunately I get error:

ERROR: Syntax Error while loading YAML script, run\_soak\_test.yaml  
Note: The error may actually appear before this position: line 3, column 26

- include: some\_playbook.yml  
when: “{{ SOME\_VARIABLE }}” == “true”  
^  
We could be wrong, but this one looks like it might be an issue with  
missing quotes. Always quote template expression brackets when they  
start a value. For instance:

with\_items:

- {{ foo }}

Should be written as:

with\_items:

- “{{ foo }}”

Do you know what’s the reason and what’s the workaround?

Thanks in advance,  
Wojtek

---

<div class="post-metadata">

**Author:** ![Tom\_Bamford](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@Tom\_Bamford](https://forum.ansible.com/u/Tom_Bamford)\
**Post date:** [January 13, 2015, 8:38am UTC](https://forum.ansible.com/t/variables-in-when-statement/17245/2 "2015-01-13T08:38:48Z")

</div>

Hi Wojtek

The error you are getting is due to inconsistent quoting. You may also need to cast your variable. Try one of these:

```auto
- include: some_playbook.yml
  when: "{{ some_variable | bool }} == True"

```

or shorter:

```auto
- include: some_playbook.yml
  when: "{{ some_variable | bool }}"

```

See [http://docs.ansible.com/YAMLSyntax.html#gotchas](http://docs.ansible.com/YAMLSyntax.html#gotchas) and [http://docs.ansible.com/playbooks\_variables.html#other-useful-filters](http://docs.ansible.com/playbooks_variables.html#other-useful-filters) for more details.

Regards  
Tom

---

<div class="post-metadata">

**Author:** ![Wojciech\_Korzenny](https://avatars.discourse-cdn.com/v4/letter/w/eada6e/32.png) [@Wojciech\_Korzenny](https://forum.ansible.com/u/Wojciech_Korzenny)\
**Post date:** [January 13, 2015, 4:07pm UTC](https://forum.ansible.com/t/variables-in-when-statement/17245/3 "2015-01-13T16:07:49Z")

</div>

Thanks Tom. It worked pefrect.

But I faced another issue with including tasks.  
I have the file tasks.yml with two tasks, I tagged them as below:

- name: Perform action - FULL  
shell: run\_script\_full.py  
tags:
- full
- name: Perform action - BASIC  
shell: run\_script\_basic.py  
tags:
- basic

In my main playbook I want to inlcude them like below:

- hosts: controller  
user: ansuser  
tasks:
- include: tasks.yml tags=full  
when: “‘{{ LEVEL }}’ == ‘full’”
- include: tasks.yml tags=basic  
when: “‘{{ LEVEL }}’ == ‘basic’”

I run ansible with command:

ansible-playbook main\_playbook.yml --extra-vars=“LEVEL=basic”. My point is to execute only task with tagged as “basic” so I’d like main script to:

1. skip first include statement
2. execute tasks.yml in second include statement but only second one (first is tagged as full so requirenments are not met).

Is it possible to achive this way?

Regards,  
Wojtek

W dniu wtorek, 13 stycznia 2015 09:29:08 UTC+1 użytkownik Wojciech Korzenny napisał:

---

<div class="post-metadata">

**Author:** ![vaida.dan](https://avatars.discourse-cdn.com/v4/letter/v/5daacb/32.png) [@vaida.dan](https://forum.ansible.com/u/vaida.dan)\
**Post date:** [January 13, 2015, 8:15pm UTC](https://forum.ansible.com/t/variables-in-when-statement/17245/4 "2015-01-13T20:15:29Z")

</div>

The inclusion of tagged tasks doesn’t work like you’re expecting in your example. That will simply tag all tasks in the included file. It won’t include the tasks that are tagged like that. You want something like:

`

cat hosts  
[localhost]  
127.0.0.1

cat playbook.yml

---

<div class="post-metadata">

**Author:** ![Wojciech\_Korzenny](https://avatars.discourse-cdn.com/v4/letter/w/eada6e/32.png) [@Wojciech\_Korzenny](https://forum.ansible.com/u/Wojciech_Korzenny)\
**Post date:** [January 14, 2015, 3:35pm UTC](https://forum.ansible.com/t/variables-in-when-statement/17245/5 "2015-01-14T15:35:50Z")

</div>

Cheers Dan for your answer.  
It works but it’s not exactly what I want to achieve. I tried one more configruation using tags but still doesn’t work as I expect.  
I have main playbook (test1.yml) with only one include:

- include: test2.yml tags=“{{ TEST\_LEVEL }}”

Playbook test2.yml looks like below:

- hosts: controller  
user: ansuser  
tasks:

- name: Test task 1  
shell: ‘echo 1’  
tags:

- test1

- hosts: controller  
user: ansuser  
tasks:

- name: Test task 2  
shell: ‘echo 2’  
tags:

- test2

I’d like to run main playbook with extra-vars where I set TEST\_LEVEL, like:  
**ansible-playbook examples/test1.yml --extra-vars=“TEST\_LEVEL=test2”**

and value of TEST\_LEVEL to be a tag for choosing tasks from test2.yml playbook.

For now it executes all tasks (whatever value for TEST\_LEVEL I choose). Is there any typo/wrong quotas etc.in my playbook or I just try to use something wrong way?

Regards,  
Wojtek

W dniu wtorek, 13 stycznia 2015 09:29:08 UTC+1 użytkownik Wojciech Korzenny napisał:

---

<div class="post-metadata">

**Author:** ![vaida.dan](https://avatars.discourse-cdn.com/v4/letter/v/5daacb/32.png) [@vaida.dan](https://forum.ansible.com/u/vaida.dan)\
**Post date:** [January 14, 2015, 4:03pm UTC](https://forum.ansible.com/t/variables-in-when-statement/17245/6 "2015-01-14T16:03:24Z")

</div>

Hi, I got what you said. However, I must repeat myself: passing tags to the include task will tag all tasks in the included file, it will NOT call ONLY the tasks that have that specific tag.
