How to automatically pass vault password when running Ansible playbook

ansibleansible-vault

I have an Ansible playbook with vault, and I want to ask for vault password through the prompt box in my web interface and then pass the posted password when running ansible playbook. I tried to use:

echo $password | ansible-playbook test.yml --ask-vault-pass

to pass the password to the playbook, but it doesn't work, the error message is:

"msg": "Attempting to decrypt but no vault secrets found"

I don't want to store password in file for some resons and now I just want to try to automatically pass password to the playbook while running it. Is there any advice to me? The ansible version is 2.4.

Best Solution

You can use a script instead of providing the password through an interactive interface.

Here's an example for your use case:

  1. Save path_to/vault_secret.sh file (add permissions to execute) with the following content:

    #!/bin/bash
    echo $password
    
  2. Execute:

    ansible-playbook test.yml --vault-password-file path_to/vault_secret.sh
    

Alternatively:

  1. Add to ansible.cfg:

    [defaults]
    vault_password_file=path_to/vault_secret.sh
    
  2. Execute:

    ansible-playbook test.yml
    
Related Question