Ad
Ansible Variable Interpolation
Clearly I am missing something (obvious?) here :-( Dictionary is correctly resolved as var in debug, but I can't get it into a message (msg). How do I trigger interpolation in this case correctly? (The actual usecase is not debug, but this is a most simple example I could construct).
---
- 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"
Gives:
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"
}
Ad
Answer
How do I trigger interpolation in this case correctly?
Since siteVars
and ServerName
are part of the data structure (variable) too, the curly braces needs to surround them also.
- name: Msg work interpolate too
debug:
msg: "{{ siteVars[ansible_default_ipv4.network].ServerName }}"
Resulting into an output of
TASK [Msg work interpolate too] ***
ok: [localhost] =>
msg: BServ
Other related questions here
- How can I use Ansible nested variable?
- Accessing nested variable variables in Ansible
- Ansible
set_fact
nested variables in playboook
Documentation
Ad
source: stackoverflow.com
Related Questions
- → How would I create bootstrap rows out of this? (React JS)
- → React-leaflet bounds
- → How to covert a custom class into json array or json dictionary in objective c?
- → Add element to List that is inside a Map (ImmutableJS)
- → How to iterate (keys, values) in JavaScript?
- → React: Looping through an array within a state
- → my geolocation(javascript) does not work and I don't know why
- → Python - Extract specific items from nested dictionary
- → Converting javascript array of objects to array of other objects using map method
- → How to retrieve and store multiple values from a python Data Frame?
- → How to remove select characters from xml parse in python / django?
- → Scope of dynamic view objects in Python 3.x
- → Organizing of Dynamic Lists of Lists
Ad