如何在 Ansible 变量中转义点"."字符


vars:
variable: [{
"b.test": "test1",
"host": "host1"
},
{
"b.test": "test2",
"host": "host2"
}]
tasks:
- debug:
msg:  "{{item.host}}"
loop: "{{variable}}"
- debug:
msg:  "{{item.b.test}}"
loop: "{{variable}}"

以上是我目前的行动手册
当我运行上面的剧本时,正如预期的那样,第一次调试返回存储在密钥host中的vlalue

ok: [localhost] => (item={'b.test': 'test1', 'host': 'host1'}) => {
"msg": "host1"
}
ok: [localhost] => (item={'b.test': 'test2', 'host': 'host2'}) => {
"msg": "host2"
}

但我不知道如何访问b.test中的值——我得到了以下错误:

fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'b'nnThe error appears to be in '/home/ansible/cmdb/playbooks/dot.yml': line 25, column 7, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nnn    - debug:n      ^ heren"}

我尝试使用逃离,但没有成功
我还在selectattr中找到了这个页面:Ansible jinja2转义点键但我不知道里面发生了什么,到底发生了什么。

请让我知道如何找到解决方案,我一直在寻找,但到目前为止还没有真正的结果。

请参阅引用键:值字典变量。将钥匙放入括号item['b.test']中。例如,战术手册

- hosts: localhost
vars:
variable:
- b.test: test1
host: host1
- b.test: test2
host: host2
tasks:
- debug:
var:  item['b.test']
loop: "{{ variable }}"

给出(节略(

ok: [localhost] => (item={'b.test': 'test1', 'host': 'host1'}) => 
ansible_loop_var: item
item:
b.test: test1
host: host1
item['b.test']: test1
ok: [localhost] => (item={'b.test': 'test2', 'host': 'host2'}) => 
ansible_loop_var: item
item:
b.test: test2
host: host2
item['b.test']: test2

最新更新