# Ansible variables jinja and facts

**URL:** <https://forum.ansible.com/t/ansible-variables-jinja-and-facts/31107>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [August 23, 2019, 5:18am UTC](https://forum.ansible.com/t/ansible-variables-jinja-and-facts/31107 "2019-08-23T05:18:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 23, 2019, 5:18am UTC](https://forum.ansible.com/t/ansible-variables-jinja-and-facts/31107/1 "2019-08-23T05:18:37Z")

</div>

> So it’s confusing between all the rules on these three is there a good resource to go over this? Or maybe someone willing to help me this weekend figure out when you use quotes when you don’t and when you’re able to use variables. For example I attempted to do when: name.username == bob I’m having errors. Which I have an vars\_files which contains a var array name: - username: bob.

> I’m having errors  
> name: - username: bob  
> when: name.username == bob

The variable "name" is an array. An index is needed to reference an items in  
an array. Correct syntax to reference "username" in the first element of the  
array is

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name[0].username

, or

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name.0.username

> when you use quotes when you don’t and when you’re able to use variables

The debug tasks below are self-explaining

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: name.0.username  
gives  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"msg": "name.0.username"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ name.0.username }}"  
gives  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"msg": "bob"

The variables in the "when" statement are expanded by default.  
See "The When Statement"  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_conditionals.html#the-when-statement](https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#the-when-statement)

Then correct syntax of the condition is

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: username is bob  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: name.0.username == "bob"

Both single and double-quotes work here.

> is there a good resource to go over this?

See:  
\* "7.3. Flow Scalar Styles"  
&nbsp;&nbsp;[https://yaml.org/spec/1.2/spec.html#id2786942](https://yaml.org/spec/1.2/spec.html#id2786942)  
\* YAML Syntax  
&nbsp;&nbsp;[https://docs.ansible.com/ansible/latest/reference\_appendices/YAMLSyntax.html](https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html)

Cheers,

&nbsp;&nbsp;-vlado
