From 161e0be89b6365d9a629cfdda012f88b6dd4384e Mon Sep 17 00:00:00 2001 From: SriVignessh Pss Date: Thu, 14 Nov 2019 13:38:40 -0800 Subject: [PATCH] Fix access variable name programmatically faq docs (#64041) * Add an example of replacing dashes with underscores in interface names for IPv4 --- docs/docsite/rst/reference_appendices/faq.rst | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/docsite/rst/reference_appendices/faq.rst b/docs/docsite/rst/reference_appendices/faq.rst index 33f309d1b25..2358db1d668 100644 --- a/docs/docsite/rst/reference_appendices/faq.rst +++ b/docs/docsite/rst/reference_appendices/faq.rst @@ -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_.