Fix access variable name programmatically faq docs (#64041)
* Add an example of replacing dashes with underscores in interface names for IPv4
This commit is contained in:
parent
a6f45713fc
commit
161e0be89b
1 changed files with 7 additions and 1 deletions
|
@ -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'] }}
|
||||
|
||||
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.
|
||||
|
||||
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_.
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue