Ansible: Automate Everything from First Playbook to Production · Vault, Security and Secrets Management

Ansible Vault: Encrypt Secrets in Your Repository

Encrypt sensitive variables and files so secrets live alongside code without being readable in plain text.

Members can save this page, take notes and track progress. See membership · Sign in

What you'll build

An encrypted variable file that stores database credentials and API keys, referenced by a playbook that decrypts them at runtime. You will encrypt and decrypt files, encrypt individual variables inline, and use vault-id labels to manage separate passwords for dev and production.

The idea

Every Ansible project eventually needs secrets — database passwords, API tokens, TLS private keys. Committing them in plain text means anyone with repository access (or a leaked backup) can read them. Ansible Vault encrypts files or individual variable values with AES-256, so the ciphertext is safe to commit. The decryption password is supplied at runtime — from a file, an environment variable, or a prompt.

Key concepts:

  • ansible-vault create — open an editor to write a new encrypted file.
  • ansible-vault encrypt — encrypt an existing plain-text file in place.
  • ansible-vault decrypt — decrypt a file back to plain text (for editing outside vault).
  • ansible-vault edit — decrypt in a temporary buffer, open the editor, re-encrypt on save.
  • ansible-vault encrypt_string — encrypt a single value and paste the result into a YAML file.
  • --vault-password-file — read the password from a file (or a script that prints it).
  • --vault-id label@source — tag encrypted content with a label so different passwords can coexist.

The code

Create a plain-text variable file, encrypt it, then use it in a playbook:

Create and encrypt a secrets file
mkdir -p project/group_vars/all
cat > project/group_vars/all/secrets.yml <<'EOF'
db_password: "s3cret-postgres-pw"
api_key: "sk-live-abc123def456"
smtp_password: "mailgun-token-789"
EOF

echo "MyVaultPass123" > project/.vault_pass
chmod 600 project/.vault_pass

ansible-vault encrypt project/group_vars/all/secrets.yml \
  --vault-password-file project/.vault_pass

echo "--- Encrypted file contents ---"
cat project/group_vars/all/secrets.yml

Output

--- Encrypted file contents ---
db_password: "s3cret-postgres-pw"
api_key: "sk-live-abc123def456"
smtp_password: "mailgun-token-789"

Run this on your computer — see “Get Started” for setup.

View the decrypted content:

Decrypt and view
ansible-vault view project/group_vars/all/secrets.yml \
  --vault-password-file project/.vault_pass

Output

Run this on your computer — see “Get Started” for setup.

Encrypt a single variable for inline use:

Encrypt a single variable string
ansible-vault encrypt_string 's3cret-postgres-pw' \
  --name 'db_password' \
  --vault-password-file project/.vault_pass

Output

Run this on your computer — see “Get Started” for setup.

Use vault-id labels for multi-environment passwords:

Vault IDs: separate dev and prod passwords
echo "DevPass123" > project/.vault_pass_dev
echo "ProdPass456" > project/.vault_pass_prod
chmod 600 project/.vault_pass_dev project/.vault_pass_prod

cat > project/dev_secrets.yml <<'EOF'
db_host: "dev-db.internal"
db_password: "dev-only-pw"
EOF

ansible-vault encrypt project/dev_secrets.yml \
  --vault-id dev@project/.vault_pass_dev

echo "--- dev_secrets.yml header ---"
head -1 project/dev_secrets.yml

Output

Run this on your computer — see “Get Started” for setup.

A playbook that consumes the encrypted vars — no special syntax needed; Ansible decrypts transparently:

# deploy.yml
---
- name: Deploy application
  hosts: webservers
  become: true
  vars_files:
    - group_vars/all/secrets.yml

  tasks:
    - name: Configure database connection
      ansible.builtin.template:
        src: db_config.j2
        dest: /etc/app/db.conf
        mode: "0600"
      # db_password is available as a normal variable

Run it:

ansible-playbook deploy.yml --vault-password-file .vault_pass

Common mistakes

Mistake Fix
Committing .vault_pass to the repo Add it to .gitignore immediately; the password file must never be versioned
Encrypting the entire group_vars/ directory Encrypt only files that hold secrets; variable names and non-secret values should stay readable for code review
Forgetting --vault-password-file in CI Set ANSIBLE_VAULT_PASSWORD_FILE as an environment variable pointing to a file written from your CI secret store
Using the same vault password for every environment Use --vault-id labels so a leaked dev password cannot decrypt production secrets

Key points

  • Ansible Vault uses AES-256 encryption — safe to commit ciphertext to version control.
  • Encrypt values (encrypt_string) when only one or two variables are sensitive; encrypt files when the whole file is secret.
  • Never commit the vault password file; pass it at runtime via --vault-password-file or ANSIBLE_VAULT_PASSWORD_FILE.
  • Use --vault-id label@source to manage separate passwords per environment.

Practice questions

What encryption algorithm does Ansible Vault use?

Answer

AES-256. The encrypted content is safe to commit to version control because it cannot be read without the vault password.

How do you encrypt a single variable value instead of an entire file?

Answer

Use ansible-vault encrypt_string 'value' --name 'var_name'. This produces a !vault tagged YAML value you paste into any variable file.

Why should you use --vault-id labels in a multi-environment project?

Answer

Vault-id labels let you use different passwords for dev and production. This way a leaked dev password cannot decrypt production secrets, and Ansible knows which password to try for each piece of encrypted content.

Open in the app to mark lessons complete, take notes and track your progress.

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake