snippetMinor
How to access elements of a variable in ansible
Viewed 0 times
elementshowvariableansibleaccess
Problem
I'm trying to access a specific value in a list of data and it fails, can't find the ipa variable.
Here is a short bit of the boulder.yaml vars file, yes, dual homed. Network configurations require it:
And I'm trying to use the following (much shorter) jinja2 template (nodes.j2) to create a file:
I'm basically getting the error that ipa is not found.
Since there are two sections, having it be a loop isn't going to work but if I test just the service section as a loop, it does work. So it's able to find ipa, but my method isn't valid for some reason.
I saw another suggestion of using .0 instead of a bracketed array
And also tried using a quoted zero
But neither work. Searching on the 'net is common enough that my search is not finding anything relevant.
Here is a short bit of the boulder.yaml vars file, yes, dual homed. Network configurations require it:
service:
control:
- name: "bldr0cuomdev01"
ipa: 192.168.101.81
- name: "bldr0cuomdev02"
ipa: 192.168.101.82
management:
control:
- name: "bldr0cuomdev11"
ipa: 10.100.78.81
- name: "bldr0cuomdev12"
ipa: 10.100.78.82And I'm trying to use the following (much shorter) jinja2 template (nodes.j2) to create a file:
{{ service.control.ipa[0] }},{{ management.control.ipa[0] }}I'm basically getting the error that ipa is not found.
Since there are two sections, having it be a loop isn't going to work but if I test just the service section as a loop, it does work. So it's able to find ipa, but my method isn't valid for some reason.
{% for s in service.control %}
{{ s.ipa }}
{% endfor %}I saw another suggestion of using .0 instead of a bracketed array
{{ service.control.ipa.0 }},{{ management.control.ipa.0 }}And also tried using a quoted zero
{{ service.control.ipa['0'] }},{{ management.control.ipa['0'] }}But neither work. Searching on the 'net is common enough that my search is not finding anything relevant.
Solution
For example,
gives
result: "{{ service.control.0.ipa }},{{ management.control.0.ipa }}"
gives
result: 192.168.101.81,10.100.78.81
Context
StackExchange DevOps Q#16608, answer score: 2
Revisions (0)
No revisions yet.