Reading an ansible variable with "(" in its name

Viewed 278

Ansible var file var.yml:

---
OS: test123
OStype(gb): test
disk size(gb): test

main.yml

---
- name: read varibale-
  hosts: localhost
  gather_facts: no
  tasks:
  - include_vars: var.yml
  - debug:
      msg: "{{ OStype(gb) }}"

Error:

"msg": "The task includes an option with an undefined variable. The error was: 'OStype' is undefined\n\

2 Answers

For those cases where the variables name contains special characters, you can use the vars lookup.

Given the playbook:

- hosts: all
  gather_facts: no
      
  tasks:
    - include_vars: vars.yml
    - debug:
        msg: "{{ lookup('vars', 'OStype(gb)') }}"

And the file vars.yml:

OS: test123
OStype(gb): test
disk size(gb): test

This yields:

PLAY [all] *******************************************************************************************************

TASK [include_vars] **********************************************************************************************
ok: [localhost]

TASK [debug] *****************************************************************************************************
ok: [localhost] => {
    "msg": "test"
}

PLAY RECAP *******************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
Related