2

I am trying to use a dict in an Ansible task, which is defined like this:

in vars/main.yml file:

username: user1
userpass: pass1
users:
  "{{ username }}":
    pass: "{{ userpass }}"

This doesn't work in Ansible 2.9: Ansible seems to not interpolate the value for "{{ username }}"

IIRC this worked fine around Ansible 2.5

What has changed? How should I rewrite my roles and playbooks now?

1 Answers1

3

The play

- hosts: localhost
  vars:
    username: user1
    userpass: pass1
    users: "{{ {username: {'pass': userpass}} }}"
  tasks:
    - debug:
        var: users

gives:

"users": {
    "user1": {
        "pass": "pass1"
    }
}

Is this what you're looking for?


Q: expect to get an error ... there is an extra colon on line 5.

A: It's JSON format

{username: {'pass': userpass}}
  • JSON is a YAML subset. Test it in YAML Lint. The YAML equivalent is
username:
  pass: userpass

  • Optionally, it is possible to create the structure in a block
    users: |
      {{ username }}:
        pass: {{ userpass}}

and to use the filter from_yaml to convert it. For example,

    - debug:
        var: users | from_yaml

gives

  users | from_yaml:
    user1:
      pass: pass1
  • Use the yaml callback. See
shell> ansible-doc -t callback yaml