Variables in "when" statement

924 views
Skip to first unread message

Wojciech Korzenny

unread,
Jan 13, 2015, 3:29:08 AM1/13/15
to ansible...@googlegroups.com
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

Tom Bamford

unread,
Jan 13, 2015, 3:39:10 AM1/13/15
to ansible...@googlegroups.com

Hi Wojtek

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

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

or shorter:

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

See http://docs.ansible.com/YAMLSyntax.html#gotchas and http://docs.ansible.com/playbooks_variables.html#other-useful-filters for more details.

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/4cb1d171-cd47-44dc-ae97-87bc7b2dc5a1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Wojciech Korzenny

unread,
Jan 13, 2015, 11:07:50 AM1/13/15
to ansible...@googlegroups.com
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

Dan Vaida

unread,
Jan 13, 2015, 3:15:29 PM1/13/15
to ansible...@googlegroups.com
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
---
- hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - include: yes.yml tags=yes
      when: answer == "yes"
    - include: no.yml tags=no
      when: answer == "no"


cat yes.yml
---

- name: this is the yes task
  command: pwd


cat no.yml
---

- name: this is the no task
  command: pwd
and eventually run it with 
ansible-playbook -i hosts playbook.yml --extra-vars="answer=yes"

Cheers.

Wojciech Korzenny

unread,
Jan 14, 2015, 10:35:50 AM1/14/15
to ansible...@googlegroups.com
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ł:

Dan Vaida

unread,
Jan 14, 2015, 11:03:52 AM1/14/15
to ansible...@googlegroups.com
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.

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/BdSIRBV2HNg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages