# Dot notation versus bracket notation for variables with hyphens

**URL:** <https://forum.ansible.com/t/dot-notation-versus-bracket-notation-for-variables-with-hyphens/42902>\
**Category:** Get Help\
**Tags:** ansible\
**Created:** [May 22, 2025, 11:08am UTC](https://forum.ansible.com/t/dot-notation-versus-bracket-notation-for-variables-with-hyphens/42902 "2025-05-22T11:08:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anoniempje](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/anoniempje/32/8157_2.png) [@anoniempje](https://forum.ansible.com/u/anoniempje)\
**Post date:** [May 22, 2025, 11:08am UTC](https://forum.ansible.com/t/dot-notation-versus-bracket-notation-for-variables-with-hyphens/42902/1 "2025-05-22T11:08:18Z")

</div>

Hi all  
We tend to follow the GPA [Good Practices for Ansible - GPA](https://redhat-cop.github.io/automation-good-practices/#_argument_validation) for our Ansible coding standards. GPA states that bracket notation `foo['bar']` should be used in lieue of dot notation `foo.bar` for variables:

_Use bracket notation instead of dot notation for value retrieval (e.g. `item['key']` vs. `item.key`)_

_Dot notation will fail in some cases (such as when a variable name includes a hyphen) and it’s better to stay consistent than to switch between the two options within a role or playbook. Additionally, some key names collide with attributes and methods of Python dictionaries such as `count`, `copy`, `title`, and others (refer to the [Ansible User Guide](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#referencing-key-value-dictionary-variables) for an extended list)_

Now I saw that someone in our project used a variable with a hyphen in it let’s call it `foo-server` , and he used the following code which works:

```auto
  block:
    - name: blablabla
      when: "'foo-server.bar' in ansible_facts.services"
      ansible.builtin.service:
        name: foo-server
        state: stopped

```

However this does not follow GPA hence I tried the following to improve our coding standards, which oddly and contrary to my expectations does NOT work. And when you quote the entire string in `when` , it ALSO DOES NOT WORK.

```auto
  block:
    - name: blablabla
      when: foo-server['bar'] in ansible_facts['services']
      ansible.builtin.service:
        name: foo-server
        state: stopped

```

\*\*Questions:

1. Why does my version not work?
2. Does this mean that the advice in GPA to use bracket notation rather than dot notation is untrue and that we should definitely reconsider following this guide? Are there other, better best practice guides to improve our Ansible coding standard?\*\*

_- Include relevant logs from the issue_

- Confidential environment, but the task failed due to variables not being interpreted properly when using the bracket notation ending the playbook. For example it will only see the part before the hyphen `-` and think that is the variable name, which it isn’t, and that causes failure.

ansible [core 2.15.13]  
config file = /etc/ansible/ansible.cfg  
python version = 3.9.20 (main, Sep 26 2024, 20:59:47) [GCC 8.5.0 20210514 (Red Hat 8.5.0-22)] (/usr/bin/python3.9)  
jinja version = 3.1.6  
libyaml = True

---

<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:** [May 22, 2025, 12:06pm UTC](https://forum.ansible.com/t/dot-notation-versus-bracket-notation-for-variables-with-hyphens/42902/2 "2025-05-22T12:06:02Z")

</div>

> [@anoniempje](#):
>
> Now I saw that someone in our project used a variable with a hyphen in it let’s call it `foo-server`

I don’t think so. In this expression:

```auto
when: "'foo-server.bar' in ansible_facts.services"

```

`foo-server.bar` is not a variable. It’s a string with both a hyphen and a dot in it. It’s also (possibly) a key in the dict `ansible_facts.services` — or, if you prefer, `ansible_facts['services']`.

---

<div class="post-metadata">

**Author:** ![anoniempje](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/anoniempje/32/8157_2.png) [@anoniempje](https://forum.ansible.com/u/anoniempje)\
**Post date:** [May 22, 2025, 1:00pm UTC](https://forum.ansible.com/t/dot-notation-versus-bracket-notation-for-variables-with-hyphens/42902/3 "2025-05-22T13:00:31Z")

</div>

Would the following work? Intended result is for `foo-server` to be a variable, containing the key `bar` .

```auto
when: "foo-server.bar in ansible_facts.services"

```

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [May 22, 2025, 4:06pm UTC](https://forum.ansible.com/t/dot-notation-versus-bracket-notation-for-variables-with-hyphens/42902/4 "2025-05-22T16:06:17Z")

</div>

See [https://docs.ansible.com/ansible/latest/playbook\_guide/playbooks\_variables.html#creating-valid-variable-names](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html#creating-valid-variable-names).

---

<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:** [May 23, 2025, 7:10pm UTC](https://forum.ansible.com/t/dot-notation-versus-bracket-notation-for-variables-with-hyphens/42902/5 "2025-05-23T19:10:35Z")

</div>

> [@anoniempje](#):
>
> Would the following work? Intended result is for `foo-server` to be a variable […]

No.“`foo-server`” is not only an invalid variable name, in a jinja expression it means to subtract the value of the variable `server` from the value of the variable `foo`. This is not a windmill you want to tilt against.
