'Ansible: Get Variable with inventory_hostname

I have the following passwords file vault.yml:

---
server1: "pass1"
server2: "pass2"
server3: "pass3"

I am loading these values in a variable called passwords:

- name: Get Secrets
  set_fact:
    passwords: "{{ lookup('template', './vault.yml')|from_yaml }}"
  delegate_to: localhost
- name: debug it
  debug:
    var: passwords.{{ inventory_hostname }}

The result of the debugging task shows me the result I want to get: The password for the specific host.

But if I set the following in a variables file:

---
ansible_user: root
ansible_password: passwords.{{ inventory_hostname }}

This will not give me the desired result. The ansible_password takes "passwords" literally and not as a variable.

How can I achieve the same result I got when debugging the passwords.{{ inventory_hostname }}?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source