Ansible variables jinja and facts

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

        name[0].username

, or

        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

        - debug:
            msg: name.0.username
gives
        "msg": "name.0.username"

        - debug:
            msg: "{{ name.0.username }}"
gives
        "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

Then correct syntax of the condition is

            - debug:
                msg: username is bob
              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"
  https://yaml.org/spec/1.2/spec.html#id2786942
* YAML Syntax
  https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html

Cheers,

  -vlado