Fix access variable name programmatically faq docs (#64041)

* Add an example of replacing dashes with underscores in interface names for IPv4
pull/64877/head
SriVignessh Pss 5 years ago committed by Alicia Cozine
parent a6f45713fc
commit 161e0be89b

@ -379,9 +379,15 @@ via a role parameter or other input. Variable names can be built by adding stri
{{ hostvars[inventory_hostname]['ansible_' + which_interface]['ipv4']['address'] }} {{ hostvars[inventory_hostname]['ansible_' + which_interface]['ipv4']['address'] }}
The trick about going through hostvars is necessary because it's a dictionary of the entire namespace of variables. 'inventory_hostname' The trick about going through hostvars is necessary because it's a dictionary of the entire namespace of variables. ``inventory_hostname``
is a magic variable that indicates the current host you are looping over in the host loop. is a magic variable that indicates the current host you are looping over in the host loop.
In the example above, if your interface names have dashes, you must replace them with underscores:
.. code-block:: jinja
{{ hostvars[inventory_hostname]['ansible_' + which_interface | replace('_', '-') ]['ipv4']['address'] }}
Also see dynamic_variables_. Also see dynamic_variables_.

Loading…
Cancel
Save