# variables not defined when tasks are conditionally includes

**URL:** <https://forum.ansible.com/t/variables-not-defined-when-tasks-are-conditionally-includes/13517>\
**Category:** Ansible Project\
**Created:** [September 20, 2013, 3:09pm UTC](https://forum.ansible.com/t/variables-not-defined-when-tasks-are-conditionally-includes/13517 "2013-09-20T15:09:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bruno\_Bonfils](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@Bruno\_Bonfils](https://forum.ansible.com/u/Bruno_Bonfils)\
**Post date:** [September 20, 2013, 3:09pm UTC](https://forum.ansible.com/t/variables-not-defined-when-tasks-are-conditionally-includes/13517/1 "2013-09-20T15:09:15Z")

</div>

Hello folks,

I’m a bit confuse with conditional includes.

First, let me explained what I’m trying to do :

I want to install bacula as agent on all servers, but few servers have another bacula roles (like director and storage), so my first idea was the following one:

Create a roles/backup/tasks/main.yml which install the agent, create two files {director,storage}.yml in tasks director, and include them with the following code:

- include: director.yml  
when: bacula\_role == ‘director’

- include: storage.yml  
when: bacula\_role == ‘storage’

In director.yml I have something like:

- name: Check something by a script  
shell: something.sh  
register: something
- name: Do another thing if something is not ok  
shell: blah.sh  
when: something.rc \> 0

but when I run the playbook on a node which have bacula\_role == ‘agent’, ansible failed on evaluate something.rc \> 0 (since something is not defined).

So my question are:

- why when conditions are evaluated when the task is skipped?
- why tasks in director.yml are parsed, why not just ignore this file when the when: condition doesn’t return true?

I have another related question, the bacula director role needs a SQL server, it is a good practice to have - { role: mysql-server, when: “bacula\_role == ‘director’” } in meta/main.yml for example?

Thanks you

---

<div class="post-metadata">

**Author:** ![James\_Cammarata1](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@James\_Cammarata1](https://forum.ansible.com/u/James_Cammarata1)\
**Post date:** [September 20, 2013, 3:36pm UTC](https://forum.ansible.com/t/variables-not-defined-when-tasks-are-conditionally-includes/13517/2 "2013-09-20T15:36:03Z")

</div>

The files are always parsed because the when: statement may depend on a variable that is defined later in the execution, such as a host variable or fact. As for the first issue, I’m not quite sure why that would be happening because the task should not be run nor evaluated. I’m wondering if you have some other clause that is causing that to be included anyway?

---

<div class="post-metadata">

**Author:** ![Bruno\_Bonfils](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@Bruno\_Bonfils](https://forum.ansible.com/u/Bruno_Bonfils)\
**Post date:** [September 20, 2013, 4:21pm UTC](https://forum.ansible.com/t/variables-not-defined-when-tasks-are-conditionally-includes/13517/3 "2013-09-20T16:21:13Z")

</div>

No, I don’t have other include clauses.

Is that a bug, or I need to refactor my roles?

---

<div class="post-metadata">

**Author:** ![James\_Cammarata1](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@James\_Cammarata1](https://forum.ansible.com/u/James_Cammarata1)\
**Post date:** [September 20, 2013, 4:36pm UTC](https://forum.ansible.com/t/variables-not-defined-when-tasks-are-conditionally-includes/13517/4 "2013-09-20T16:36:01Z")

</div>

Yes go ahead and open an issue for that, and please include a quick sample to help reproduce that if you can.

Thanks!

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [September 20, 2013, 5:07pm UTC](https://forum.ansible.com/t/variables-not-defined-when-tasks-are-conditionally-includes/13517/5 "2013-09-20T17:07:59Z")

</div>

> - name: Check something by a script  
> &nbsp;&nbsp;shell: something.sh  
> &nbsp;&nbsp;register: something  
> - name: Do another thing if something is not ok  
> &nbsp;&nbsp;shell: blah.sh  
> &nbsp;&nbsp;when: something.rc \> 0

While that that first task is skipped​​, the register option does make it  
register something, which is, If I'm not mistaken:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;something['skipped''] = true

Which makes something['rc'] not defined, yeilding an error.

Change that conditional to:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: something.skipped is not defined and something.rc \> 0

Always take into account a registered task might get skipped.

Serge

---

<div class="post-metadata">

**Author:** ![Bruno\_Bonfils](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@Bruno\_Bonfils](https://forum.ansible.com/u/Bruno_Bonfils)\
**Post date:** [September 20, 2013, 5:09pm UTC](https://forum.ansible.com/t/variables-not-defined-when-tasks-are-conditionally-includes/13517/6 "2013-09-20T17:09:26Z")

</div>

Ok, but why the when is evaluated while the task is skipped?

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [September 20, 2013, 5:16pm UTC](https://forum.ansible.com/t/variables-not-defined-when-tasks-are-conditionally-includes/13517/7 "2013-09-20T17:16:38Z")

</div>

​How can it know when to skip without evaluating the condition?  
Remember, both conditions are appended, and evaluated; seems the  
something.rc is evaluated first.

I vaguely recall an issue / discussion about that ordering​, not sure what  
it was or if something was changed about it.

Serge

---

<div class="post-metadata">

**Author:** ![Bruno\_Bonfils](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@Bruno\_Bonfils](https://forum.ansible.com/u/Bruno_Bonfils)\
**Post date:** [September 20, 2013, 5:37pm UTC](https://forum.ansible.com/t/variables-not-defined-when-tasks-are-conditionally-includes/13517/8 "2013-09-20T17:37:25Z")

</div>

Because these tasks are in a file which is include as following:

- include: file.yaml  
when: condition that returned False

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [September 20, 2013, 5:51pm UTC](https://forum.ansible.com/t/variables-not-defined-when-tasks-are-conditionally-includes/13517/9 "2013-09-20T17:51:55Z")

</div>

> Because these tasks are in a file which is include as following:
> 
> - include: file.yaml  
> &nbsp;&nbsp;when: condition that returned False

again: both conditions are appended, and evaluated​​

---

<div class="post-metadata">

**Author:** ![Bruno\_Bonfils](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@Bruno\_Bonfils](https://forum.ansible.com/u/Bruno_Bonfils)\
**Post date:** [September 20, 2013, 9:17pm UTC](https://forum.ansible.com/t/variables-not-defined-when-tasks-are-conditionally-includes/13517/10 "2013-09-20T21:17:09Z")

</div>

Ok my bad.

Any chance to have a different behavior (with another keyword than include maybe) to include a file that will not even parsed when the when condition return false?

After all, maybe the way I think roles doesn’t match ansible roles.

It’s just an example, but for me, an ansible role “backup” should care of all backup configuration, server, client, etc.

Maybe I miss something, but at this moment I don’t understand how to have a separate tasks file for each backup role.
