在Ansible中,可以在变量中使用变量,称为嵌套变量。以下是一个示例:
假设我们有两个变量:
foo: "hello"
bar: "{{ foo }} world"
这里bar
变量使用了foo
变量作为值,通过{{ foo }}
来嵌套使用。
在playbook中,可以使用这些变量:
---
- hosts: localhost
vars:
foo: "hello"
bar: "{{ foo }} world"
tasks:
- debug:
var: bar
运行以上playbook,输出将会是:
TASK [debug] *************************************************************************
ok: [localhost] => {
"bar": "hello world"
}
可以看到,bar
变量的值是hello world
,而不是{{ foo }} world
。
注意:在Ansible中,变量嵌套的深度是有限制的,不能无限嵌套。