0

Clearly I am missing something (obvious?) here :-( Dictionary is correctly resolved as var in debug, but I can't get it into a message (msg). How do I trigger interpolation in this case correctly? (The actual usecase is not debug, but this is a most simple example I could construct).

---
- name: Setup based on subnet
  hosts: 
    - localhost
  vars:
    siteVars:
      10.128.0.0:
        ServerName:  "AServ"
      10.0.0.0:
        ServerName:  "BServ"
 
  tasks:
  - name: Do we get the right Network
    debug: 
      msg: "{{ ansible_default_ipv4.network }}"

  - name: Var works
    debug: 
      var: siteVars[ {{ 'ansible_default_ipv4.network' }} ].ServerName

  - name: Msg does not work interpolate
    debug:
       msg: "siteVars[ {{ 'ansible_default_ipv4.network' }} ].ServerName"   

Gives:

TASK [Do we get the right Network] ******************************************************************************************ok: [localhost] => {
    "msg": "10.0.0.0"
}

TASK [Var works] ******************************************************************************************ok: [localhost] => {
    "siteVars[ ansible_default_ipv4.network ].ServerName": "BServ"
}

TASK [Msg does not work interpolate] ******************************************************************************************ok: [localhost] => {
    "msg": "siteVars[ ansible_default_ipv4.network ].ServerName"
}

1 Answers1

1

How do I trigger interpolation in this case correctly?

Since siteVars and ServerName are part of the data structure (variable) too, the curly braces needs to surround them also.

- name: Msg work interpolate too
  debug:
    msg: "{{ siteVars[ansible_default_ipv4.network].ServerName }}"

Resulting into an output of

TASK [Msg work interpolate too] ***
ok: [localhost] =>
  msg: BServ

Other related questions here

Documentation

U880D
  • 8,601
  • 6
  • 24
  • 40