1

I am trying to access the contents of a variable, from a variable name. I've used ansible vault to create some passwords, and I am trying to create database users, like so:

postgresql_user:
    name: "{{ item }}"
    db: "{{ item }}"
    login_user: postgres_admin
    login_password: '{{postgres_admin}}'
    login_host: localhost
    login_port: 5433
    password: '{{ "{{item}}" }}'
loop:
   - artifactory
   - bitbucket
   - confluence
   - crowd
   - jira

In this case, I have a database user, which is the variable in the loop. The password for that user, is stored inside the (vault) variable name of that user. In the password: field like I am trying to do a double reference. It doesn't work.

I've tried a few more things, but this is my first run with Ansible and I don't want to go down the wrong path. (I did not include the reference to the vault but it's at the top of the file, and it has been tested with a simpler tasks)

Thanks!

0

1 Answer 1

2

Use lookup plugin vars. For example

    password: "{{ lookup('vars', item) }}"

With the introduction of the collections the documentation of the plugins is not available online anymore. See the documentation from the command-line

shell> ansible-doc -t lookup vars

To make the code both more robust and user-friendly, I'd propose to rename the variables with the passwords. For example "artifactory_passwd" etc. Then, in the lookup, create the name of the variable dynamically

    password: "{{ lookup('vars', item ~ '_passwd') }}"
Sign up to request clarification or add additional context in comments.

1 Comment

This is perfect. Thank you! (I took your excellent suggestion as well)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.