0

I am trying to update a dict in a hostvar, and the name of the key is constructed using a variable (node). For example, if 'node' is 1 then I want to update hostvars['fakehost']['mydict']['localaddress1']. Here's my code:

- name: Read IPv4 of first interface
    add_host:
      name: "fakehost"
      telium: "{{ hostvars['fakehost']['mydict'] | combine ({ 'localaddress{{ node }}' : ansible_all_ipv4_addresses[0] }) }}"

I can't figure out how to construct localaddress{{node}} with ansible complaining about the syntax.

flowerysong
  • 806
  • 3
  • 6
TSG
  • 1,634
  • 6
  • 29
  • 51
  • [mustaches don't stack](https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#when-should-i-use-also-how-to-interpolate-variables-or-dynamic-variable-names) – Zeitounator Sep 12 '21 at 16:06

1 Answers1

0

Don't nest moustaches ({{ }}). Once you're inside an expression, you're already in a Jinja context and should not use additional delimiters when accessing a variable.

      telium: "{{ hostvars['fakehost']['mydict'] | combine ({ 'localaddress' ~ node: ansible_all_ipv4_addresses[0] }) }}
flowerysong
  • 806
  • 3
  • 6