Skupiłem się na wyciągnięciu haseł z vaulta, a nie miejscu, w którym chcesz je wyciągać. Można to ogarnąć tak, że w inventory ustawiasz zmienną, w której jest hasło per host, a w playbooku wyciągasz hasło na podstawie tej zmiennej.
# vagrant@ubuntu-focal:~/ansible$ cat inventory.yaml
all:
hosts:
127.0.0.1:
ansible_connection: local
user_pass_var: 'user1_pass'
127.0.0.2:
ansible_connection: local
user_pass_var: 'user2_pass'
# vagrant@ubuntu-focal:~/ansible$ cat helloWorld.yaml
---
- name: Echo User Password
hosts: all
vars_files:
- secrets.vault
tasks:
- name: Echo the password
debug:
msg: "Hello World! My password is {{ lookup('vars', hostvars[inventory_hostname]['user_pass_var']) }}"
I wynik:
# vagrant@ubuntu-focal:~/ansible$ ansible-playbook -i inventory.yaml helloWorld.yaml --ask-vault-pass
Vault password:
PLAY [Echo User Password] ****************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************************
ok: [127.0.0.1]
ok: [127.0.0.2]
TASK [Echo the password] *****************************************************************************************************************************************************************************************************************************************************************
ok: [127.0.0.1] => {
"msg": "Hello World! My password is password1"
}
ok: [127.0.0.2] => {
"msg": "Hello World! My password is password2"
}
PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************************
127.0.0.1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
127.0.0.2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Per wybrany host:
ansible-playbook -i inventory.yaml helloWorld.yaml --ask-vault-pass -l 127.0.0.2