Skip to content

Commit

Permalink
Using "~" instead of "+" for concatination (ansible#74364)
Browse files Browse the repository at this point in the history
Changed FAQ examples to conform with the Jinja documentation:
If both values on either side of a plus/+ are numbers, they will be added whereas using "~" will convert all operands into strings and then concatenate them. Closes ansible#73799.
  • Loading branch information
Hu6li committed Apr 23, 2021
1 parent 7fd989d commit e6a5245
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions docs/docsite/rst/reference_appendices/faq.rst
Expand Up @@ -424,11 +424,11 @@ How do I access a variable name programmatically?
+++++++++++++++++++++++++++++++++++++++++++++++++

An example may come up where we need to get the ipv4 address of an arbitrary interface, where the interface to be used may be supplied
via a role parameter or other input. Variable names can be built by adding strings together, like so:
via a role parameter or other input. Variable names can be built by adding strings together using "~", like so:

.. code-block:: jinja
{{ 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``
is a magic variable that indicates the current host you are looping over in the host loop.
Expand All @@ -437,7 +437,7 @@ In the example above, if your interface names have dashes, you must replace them

.. code-block:: jinja
{{ hostvars[inventory_hostname]['ansible_' + which_interface | replace('_', '-') ]['ipv4']['address'] }}
{{ hostvars[inventory_hostname]['ansible_' ~ which_interface | replace('_', '-') ]['ipv4']['address'] }}
Also see dynamic_variables_.

Expand Down Expand Up @@ -681,13 +681,13 @@ The above DOES NOT WORK as you expect, if you need to use a dynamic variable use

.. code-block:: jinja
{{ hostvars[inventory_hostname]['somevar_' + other_var] }}
{{ hostvars[inventory_hostname]['somevar_' ~ other_var] }}
For 'non host vars' you can use the :ref:`vars lookup<vars_lookup>` plugin:

.. code-block:: jinja
{{ lookup('vars', 'somevar_' + other_var) }}
{{ lookup('vars', 'somevar_' ~ other_var) }}
.. _why_no_wheel:
Expand Down

0 comments on commit e6a5245

Please sign in to comment.