在Ansible的Playbook中,可以使用vars关键字来设置嵌套变量。
以下是一个示例Playbook,其中包含设置嵌套变量的代码:
- hosts: localhost
vars:
nested_variable:
key1: value1
key2: value2
key3:
subkey1: subvalue1
subkey2: subvalue2
tasks:
- name: Print nested variable
debug:
var: nested_variable
在上述示例中,通过vars关键字设置一个名为nested_variable的嵌套变量。nested_variable是一个字典,包含三个键值对:key1: value1,key2: value2和key3。
key3是一个嵌套字典,包含两个键值对:subkey1: subvalue1和subkey2: subvalue2。
然后,在任务中使用debug模块打印嵌套变量nested_variable。
执行上述Playbook,将会输出如下结果:
TASK [Print nested variable] *****************************************************************
ok: [localhost] => {
"nested_variable": {
"key1": "value1",
"key2": "value2",
"key3": {
"subkey1": "subvalue1",
"subkey2": "subvalue2"
}
}
}
可以看到,嵌套变量nested_variable的值被正确打印出来。