absurd
absurd

Reputation: 1115

Ansible get variable with dots in key

I want Ansible to read out if user namespaces in the kernel are activated (CentOS). The respective value is visible when I run

- debug:
    msg: "{{ ansible_cmdline }}"

which gives me the output:

 "msg": {
    "BOOT_IMAGE": "/vmlinuz-...",
    "LANG": "...",
    "crashkernel": "...",
    "namespace.unpriv_enable": "...",
    "quiet": ...,
    "rd.lvm.lv": "...",
    "rhgb": ...,
    "ro": ...,
    "root": "...",
    "user_namespace.enable": "1"
}

However, I had no success to directly query the subkey:

- debug:
    msg: "{{ ansible_cmdline.user_namespace.enable }}"

Ansible interprets the .enable as a further subkey: The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'user_namespace'

How can I access the key "user_namespace.enable"?

Upvotes: 4

Views: 10020

Answers (1)

absurd
absurd

Reputation: 1115

To address a key with dots, use array notation with single quotes instead of dot notation, i.e.:

- debug:
    msg: "{{ ansible_cmdline['user_namespace.enable'] }}"

This returns the value.

cf.: Ansible FAQ

Upvotes: 16

Related Questions