# Checking if a variable is defined in included tasks

**URL:** <https://forum.ansible.com/t/checking-if-a-variable-is-defined-in-included-tasks/27709>\
**Category:** Ansible Project\
**Created:** [June 5, 2018, 10:18am UTC](https://forum.ansible.com/t/checking-if-a-variable-is-defined-in-included-tasks/27709 "2018-06-05T10:18:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bozho](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bozho/32/10430_2.png) [@bozho](https://forum.ansible.com/u/bozho)\
**Post date:** [June 5, 2018, 10:18am UTC](https://forum.ansible.com/t/checking-if-a-variable-is-defined-in-included-tasks/27709/1 "2018-06-05T10:18:22Z")

</div>

Hi all,

I’ve just started learning Ansible and I have a question about checking for defined variables in included tasks. I’m writing an infrastructure role as a part of my test project configuration. Currently, the role only creates infrastructure users by including a “create infrastructure user” task and looping over a users variable containing a list of usernames and associated SSH public keys:

My **main.yml** tasks for the role:

`

- name: Create infrastructure users  
include\_tasks: user.yml  
vars:  
username: “{{ item.username }}”  
ssh\_key: “{{ item.ssh\_key }}”  
loop: “{{ users }}”

`

My **user.yml** tasks:

`

- name: “Create infrastructure user {{ username }}”  
user:  
name: “{{ username }}”  
state: present  
groups:

- sudo  
append: yes

- name: “Set SSH key for {{ username }}”  
authorized\_key:  
user: “{{ username }}”  
key: “{{ ssh\_key }}”  
when: ssh\_key is defined

`

Now, if a user doesn’t have an **ssh\_key** defined, I would expect the a **uthorized\_key** task not to run. However, I get the following error message:

`  
The conditional check ‘ssh\_key is defined’ failed. The error was: error while evaluating conditional (ssh\_key is defined): ‘dict object’ has no attribute ‘ssh\_key’

The error appears to have been in ‘/home/bozho/projects/vault/roles/infrastructure/tasks/user.yml’: line 9, column 3, but may  
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- name: “Set SSH key for {{ username }}”  
^ here  
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 }}”

`

If I rewrite the **when** condition as:

`  
when: dict.ssh\_key is defined

`

everything works as expected (i.e. the tasks is skipped for users with no **ssh\_key** set.

Is this the expected behaviour? I would expect task parameters to be handled as variables.

Thank you!  
``

---

<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:** [June 6, 2018, 10:08am UTC](https://forum.ansible.com/t/checking-if-a-variable-is-defined-in-included-tasks/27709/2 "2018-06-06T10:08:25Z")

</div>

To me this seams like a bug.

Personally I have never used vars: on include\_tasks: I just use item variable directly and have no problems with that.  
To use item directly you code would look like this:

&nbsp;&nbsp;&nbsp;- name: Create infrastructure users  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include\_tasks: user.yml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ users }}"

user.yml
