Ansible变量插值
Ansible variable interpolation
显然我在这里遗漏了一些东西(很明显?):-(
字典在调试中被正确解析为 var,但我无法将其放入消息 (msg) 中。在这种情况下如何正确触发插值? (实际用例不是调试,但这是我可以构建的最简单的示例)。
---
- name: Setup based on subnet
hosts:
- localhost
vars:
siteVars:
10.128.0.0:
ServerName: "AServ"
10.0.0.0:
ServerName: "BServ"
tasks:
- name: Do we get the right Network
debug:
msg: "{{ ansible_default_ipv4.network }}"
- name: Var works
debug:
var: siteVars[ {{ 'ansible_default_ipv4.network' }} ].ServerName
- name: Msg does not work interpolate
debug:
msg: "siteVars[ {{ 'ansible_default_ipv4.network' }} ].ServerName"
给出:
TASK [Do we get the right Network] ******************************************************************************************ok: [localhost] => {
"msg": "10.0.0.0"
}
TASK [Var works] ******************************************************************************************ok: [localhost] => {
"siteVars[ ansible_default_ipv4.network ].ServerName": "BServ"
}
TASK [Msg does not work interpolate] ******************************************************************************************ok: [localhost] => {
"msg": "siteVars[ ansible_default_ipv4.network ].ServerName"
}
How do I trigger interpolation in this case correctly?
由于 siteVars
和 ServerName
也是数据结构(变量)的一部分,因此花括号也需要将它们括起来。
- name: Msg work interpolate too
debug:
msg: "{{ siteVars[ansible_default_ipv4.network].ServerName }}"
导致输出
TASK [Msg work interpolate too] ***
ok: [localhost] =>
msg: BServ
其他相关问题在这里
- How can I use Ansible nested variable?
- Accessing nested variable variables in Ansible
- Ansible
set_fact
nested variables in playboook
文档
显然我在这里遗漏了一些东西(很明显?):-( 字典在调试中被正确解析为 var,但我无法将其放入消息 (msg) 中。在这种情况下如何正确触发插值? (实际用例不是调试,但这是我可以构建的最简单的示例)。
---
- name: Setup based on subnet
hosts:
- localhost
vars:
siteVars:
10.128.0.0:
ServerName: "AServ"
10.0.0.0:
ServerName: "BServ"
tasks:
- name: Do we get the right Network
debug:
msg: "{{ ansible_default_ipv4.network }}"
- name: Var works
debug:
var: siteVars[ {{ 'ansible_default_ipv4.network' }} ].ServerName
- name: Msg does not work interpolate
debug:
msg: "siteVars[ {{ 'ansible_default_ipv4.network' }} ].ServerName"
给出:
TASK [Do we get the right Network] ******************************************************************************************ok: [localhost] => {
"msg": "10.0.0.0"
}
TASK [Var works] ******************************************************************************************ok: [localhost] => {
"siteVars[ ansible_default_ipv4.network ].ServerName": "BServ"
}
TASK [Msg does not work interpolate] ******************************************************************************************ok: [localhost] => {
"msg": "siteVars[ ansible_default_ipv4.network ].ServerName"
}
How do I trigger interpolation in this case correctly?
由于 siteVars
和 ServerName
也是数据结构(变量)的一部分,因此花括号也需要将它们括起来。
- name: Msg work interpolate too
debug:
msg: "{{ siteVars[ansible_default_ipv4.network].ServerName }}"
导致输出
TASK [Msg work interpolate too] ***
ok: [localhost] =>
msg: BServ
其他相关问题在这里
- How can I use Ansible nested variable?
- Accessing nested variable variables in Ansible
- Ansible
set_fact
nested variables in playboook
文档