Ansible在键中带点变量

问题描述 投票:0回答:1

我希望Ansible读出内核中的用户命名空间是否被激活(CentOS)。我运行时可以看到相应的值

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

这给了我输出:

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

但是,我没有成功直接查询该子项:

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

Ansible将.enable解释为另一个子项:The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'user_namespace'

我怎样才能访问密钥"user_namespace.enable"

ansible
1个回答
0
投票

自己解决了:

要使用点来处理键,请使用带单引号的数组符号而不是点符号,即:

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

这将返回值。

cf。:Ansible FAQ

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.