Hi!
I would like to use one vault password with two files with encrypted variables. The main variable I need is ansible_password that is used to connect to hosts, and it is different for different users.
I tried the vault ID feature with no luck. I have vault_user1.yml and vault_user2.yml. They were encrypted with the same password and two vault IDs:
If you supply a password and all vaults are encrypted with that password all can be decrypted, no need for a vault id. In your case, when a variable is defined twice the last value assigned is used. Try something like this
name: read user password
Ansible.builtin.include_vars: “{{ username }}.yml”
Then create multiple var files in vars:
vars/username1.yml
vars/username2.yml
So that the password is only read once for the user currently needed.
Thanks for the quick reply. I found one mistake in my solution. The variable files were in group_vars directory, so they were loaded by default.
I prepared new files in the vars directory but still didn’t get what I needed. I will need more time to check it.
If you post the parts of your playbook that reads the variables, I could give you some more tipps. Group and / or host_vars will only work if the group / host defines the user that should be configured. Otherwise I would use a task like the one I have posted
You’re encrypting vault_user1.yml and vault_user2.yml with the same password, but Ansible Vault IDs are only useful when you’re encrypting files with different passwords. Vault ID tagging doesn’t select which file to use — it just maps a name (user1, user2) to a specific password source. If both vault files use the same password, you can decrypt both without distinguishing IDs.
Proper Methodology to Fix the Use Case
GOAL:
Use one vault password file, two vault-encrypted variable files (vault_user1.yml and vault_user2.yml), and decrypt the correct one depending on which user is running.
SOLUTION:
Instead of using --vault-id, which is mainly for managing multiple passwords, just use --ask-vault-pass (for prompt) or --vault-password-file and load only the correct variable file per playbook:
I have /install/vars/vault_user1.yml and /install/vars/vault_user2.yml, and added this in /install/playbook1.yml as the first task:
- name: Read user password
hosts: all
tasks:
- name: Read user passwords
ansible.builtin.include_vars:
file: vault_user1.yml
- name: Check ansible_password
ansible.builtin.debug:
msg:
- "The password is: {{ ansible_password }}"
- "The become password is: {{ ansible_become_password }}"
But it failed with this error:
“Data could not be sent to remote host "192.168.1.5". Make sure this host can be reached over ssh: user1@192.168.1.5: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).”
So it looks like it tries to log in using SSH keys and not the password from the variable file although I provide the vault password.
Use -vvv when running the playbook. I bet ansible is using the correct password but with the wrong user (root instead of user1). This can be set if I’m not mistaken with