# Comparing command line variable in import\_playbook

**URL:** <https://forum.ansible.com/t/comparing-command-line-variable-in-import-playbook/37631>\
**Category:** Ansible Project\
**Created:** [February 7, 2023, 6:47pm UTC](https://forum.ansible.com/t/comparing-command-line-variable-in-import-playbook/37631 "2023-02-07T18:47:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [February 7, 2023, 6:47pm UTC](https://forum.ansible.com/t/comparing-command-line-variable-in-import-playbook/37631/1 "2023-02-07T18:47:08Z")

</div>

I again, thank you all for your help, I’m really learning a lot.

I have a new query. I am passing via “extra-vars” a variable called wc2env and I need to compare it with a string (stage or prod).

Here is my playbook:

hosts: all  
gather\_facts: no  
vars:  
wc2env: ‘{{ wc2env }}’  
tasks:

- name: “Validando si debo deployar dominio en PROD”  
ansible.builtin.import\_playbook: add\_carrier\_prod.yaml  
when: “{{ wc2env == ‘prod’ }}”

- name: “Validando si debo deployar el dominio en STAGE”  
ansible.builtin.import\_playbook: add\_carrier\_stage.yaml  
when: “{{ wc2env == ‘stage’ }}”

I run the playbook this way and for some reason the condition returns “false”:

ansible-playbook -i hosts --extra-vars “wc2env=stage” alta\_carrier/playbook/tasks\_target.yaml

Here the debug:

PLAYBOOK: tasks\_target.yaml \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Positional arguments: alta\_carrier/playbook/tasks\_target.yaml  
verbosity: 8  
connection: smart  
timeout: 10  
become\_method: sudo  
tags: (‘all’,)  
inventory: (‘/etc/ansible/webcheckin/hosts’,)  
extra\_vars: (‘wc2env=stage’,)  
forks: 50  
step: True  
4 plays in alta\_carrier/playbook/tasks\_target.yaml

[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ wc2env == ‘prod’ }}  
skipping: [wc2-stage] =\> {  
“changed”: false,  
“skip\_reason”: “Conditional result was False”  
}

I’m doing something wrong and I don’t realize they could help me.

Regards,

---

<div class="post-metadata">

**Author:** ![Alex\_Wanderley](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alex_wanderley/32/3232_2.png) [@Alex\_Wanderley](https://forum.ansible.com/u/Alex_Wanderley)\
**Post date:** [February 7, 2023, 6:57pm UTC](https://forum.ansible.com/t/comparing-command-line-variable-in-import-playbook/37631/2 "2023-02-07T18:57:39Z")

</div>

Hi,

The error says all:  
“[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }}”

So, your “when” statement should look like:  
when: wc2env == ‘stage’

Something else:  
You are already passing on “wc2env” as an extra variable. You don’t need the lines below, declaring it again, in the playbook:  
vars:  
wc2env: ‘{{ wc2env }}’

Alex

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 7, 2023, 7:03pm UTC](https://forum.ansible.com/t/comparing-command-line-variable-in-import-playbook/37631/3 "2023-02-07T19:03:02Z")

</div>

Alex pointed out your first mistake, but you also are using moustaches in ‘when’ when you shouldn’t

when: wc2env == ‘prod’

conditionals are already in an implied template context.

---

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [February 8, 2023, 12:55am UTC](https://forum.ansible.com/t/comparing-command-line-variable-in-import-playbook/37631/4 "2023-02-08T00:55:48Z")

</div>

I’ve tried that way, but it doesn’t work either:

nsible-playbook -i hosts --extra-vars “wc2env=stage carrier=LL” alta\_carrier/playbook/tasks\_targe  
t.yaml

PLAYBOOK: tasks\_target.yaml \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Positional arguments: alta\_carrier/playbook/tasks\_target.yaml  
verbosity: 5  
connection: smart  
timeout: 10  
become\_method: sudo  
tags: (‘all’,)  
inventory: (‘/etc/ansible/webcheckin/hosts’,)  
extra\_vars: (‘wc2env=stage carrier=XX’,)  
forks: 50  
step: True  
4 plays in alta\_carrier/playbook/tasks\_target.yaml

TASK [Creating DataBase] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /etc/ansible/webcheckin/alta\_carrier/playbook/add\_carrier\_prod.yaml:6  
skipping: [wc2-stage] =\> {  
“changed”: false,  
“skip\_reason”: “Conditional result was False”  
}

Here the playbook:

- name: “select target”  
hosts: all  
gather\_facts: no  
tasks:

- name: “deploy prod”  
ansible.builtin.import\_playbook: add\_carrier\_prod.yaml  
when: wc2env == ‘prod’

- name: “deploy stage”  
ansible.builtin.import\_playbook: add\_carrier\_stage.yaml  
when: wc2env == ‘stage’

any sugestions?

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [February 8, 2023, 12:19pm UTC](https://forum.ansible.com/t/comparing-command-line-variable-in-import-playbook/37631/5 "2023-02-08T12:19:08Z")

</div>

We need to see the entire output. This excerpt does not provide enough context. From what I see it is skipping the PROD because your wc2end is ‘stage’. That is expected.

Walter

---

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [February 8, 2023, 12:56pm UTC](https://forum.ansible.com/t/comparing-command-line-variable-in-import-playbook/37631/6 "2023-02-08T12:56:36Z")

</div>

This is the entire output:

ansible-playbook [core 2.12.9]  
config file = /etc/ansible/ansible.cfg  
configured module search path = [‘/home/emanuel/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]  
ansible python module location = /usr/lib/python3/dist-packages/ansible  
ansible collection location = /home/emanuel/.ansible/collections:/usr/share/ansible/collections  
executable location = /usr/bin/ansible-playbook  
python version = 3.8.10 (default, Nov 14 2022, 12:59:47) [GCC 9.4.0]  
jinja version = 2.10.1  
libyaml = True  
Using /etc/ansible/ansible.cfg as config file  
setting up inventory plugins  
host\_list declined parsing /etc/ansible/webcheckin/hosts as it did not pass its verify\_file() method  
script declined parsing /etc/ansible/webcheckin/hosts as it did not pass its verify\_file() method  
auto declined parsing /etc/ansible/webcheckin/hosts as it did not pass its verify\_file() method  
Not replacing invalid character(s) “{‘-’}” in group name (kiucargo-servers)  
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details  
Parsed /etc/ansible/webcheckin/hosts inventory source with yaml plugin  
Loading collection community.mysql from /usr/lib/python3/dist-packages/ansible\_collections/community/mysql  
Loading collection community.general from /usr/lib/python3/dist-packages/ansible\_collections/community/general  
Loading callback plugin default of type stdout, v2.0 from /usr/lib/python3/dist-packages/ansible/plugins/callback/default.py  
Attempting to use ‘default’ callback.  
Skipping callback ‘default’, as we already have a stdout callback.  
Attempting to use ‘junit’ callback.  
Attempting to use ‘minimal’ callback.  
Skipping callback ‘minimal’, as we already have a stdout callback.  
Attempting to use ‘oneline’ callback.  
Skipping callback ‘oneline’, as we already have a stdout callback.  
Attempting to use ‘tree’ callback.

PLAYBOOK: tasks\_target.yaml \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Positional arguments: alta\_carrier/playbook/tasks\_target.yaml  
verbosity: 5  
connection: smart  
timeout: 10  
become\_method: sudo  
tags: (‘all’,)  
inventory: (‘/etc/ansible/webcheckin/hosts’,)  
extra\_vars: (‘wc2env=stage carrier=LL’,)  
forks: 50  
step: True  
4 plays in alta\_carrier/playbook/tasks\_target.yaml

PLAY [Seleccionamos el destino] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
META: ran handlers  
META: ran handlers  
META: ran handlers

PLAY [Creando DNS para nuevo carrier WC2 STAGE] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
META: ran handlers  
Perform task: TASK: Creating DataBase (N)o/(y)es/(c)ontinue: yes

Perform task: TASK: Creating DataBase (N)o/(y)es/(c)ontinue: \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [Creating DataBase] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /etc/ansible/webcheckin/alta\_carrier/playbook/add\_carrier\_prod.yaml:6  
skipping: [wc2-stage] =\> {  
“changed”: false,  
“skip\_reason”: “Conditional result was False”  
}

Does this skipping: [wc2-stage] mean the playbook is skipping the stage server?

This is my hosts file:

kiucargo-servers:  
hosts:  
wc2-stage:  
ansible\_host: 172.x.x.x  
wc2-prod:  
ansible\_host: 172.x.x.x

I understand that the import\_playbook parameter function should not matter the prod playbook, am I right?

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [February 8, 2023, 1:17pm UTC](https://forum.ansible.com/t/comparing-command-line-variable-in-import-playbook/37631/7 "2023-02-08T13:17:54Z")

</div>

You still haven’t shown us a task with the name “Creating DataBase”, which is what’s being skipped. Until you show us the tasks and their conditionals that go with the outputs, we can only guess.

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [February 8, 2023, 1:28pm UTC](https://forum.ansible.com/t/comparing-command-line-variable-in-import-playbook/37631/8 "2023-02-08T13:28:15Z")

</div>

> task path: /etc/ansible/webcheckin/alta\_carrier/playbook/ **add\_carrier\_prod.yaml** :6  
> skipping: [wc2-stage] =\> {  
> “changed”: false,  
> “skip\_reason”: “Conditional result was False”  
> }

Look at the red text above. You have a prod task file. Your wc2env variable has a value of ‘stage’. Your task in the prod file has a condition of when wc2env == ‘prod’. Since wc2env == ‘stage’ this ‘prod’ task is the one being skipped.

The skipping message means the task itself is being skipping because the condition you place on it evaluated to false. The task would only execute if the condition evaluated to true.

You provided these two tasks in a prior message.

> - name: “deploy prod”  
> ansible.builtin.import\_playbook: add\_carrier\_prod.yaml  
> when: wc2env == ‘prod’
> 
> - name: “deploy stage”  
> ansible.builtin.import\_playbook: add\_carrier\_stage.yaml  
> when: wc2env == ‘stage’

You are using ‘include\_playbook’. When you place a condition on ‘import\_playbook’ it does not skip importing the playbook when the condition is false. It effectively places that condition on every task inside the imported playbook. If you want to skip the import itself you should use include vs import. In one of my playbooks I have a series of include\_tasks tasks. The task files included are also ansible yaml files. Some of the include\_tasks tasks in my playbook have when clauses on them. If one of those ‘when’ clauses evaluates to false, the task file is NOT included. In your case if one of your include\_playbook ‘when’ clauses evaluates to false the playbook is still imported and all of the tasks are assigned the added condition from your import\_playbook task.

Walter

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [February 8, 2023, 1:34pm UTC](https://forum.ansible.com/t/comparing-command-line-variable-in-import-playbook/37631/9 "2023-02-08T13:34:41Z")

</div>

Read this article for a more thorough explanation of import vs include and how conditions on them are handled.

[https://www.devopsschool.com/blog/ansible-include-and-import-with-differences-explanined/](https://www.devopsschool.com/blog/ansible-include-and-import-with-differences-explanined/)

Walter
