Ansible Vault encrypt and decrypt

Encrypt text into an Ansible Vault (format 1.1 or 1.2, AES256) and decrypt vaults back.

loading Ansible Vault encrypt and decrypt
About this tool

What is Ansible Vault?

Ansible Vault is the way Ansible keeps secrets inside playbooks and inventories. Instead of leaving a database password or an API key in plain text in a Git repository, you store it encrypted. Ansible asks for the vault password when it runs, decrypts the value in memory and uses it. The command line tools are ansible-vault encrypt, decrypt, view and encrypt_string.

This page does the same thing in your browser. It encrypts text into a vault, decrypts a vault back to text, and reads the !vault | blocks that encrypt_string prints. The output is compatible with ansible-vault: a vault made here opens there, and the other way around. It supports format 1.1 and format 1.2, which adds a vault id (a label such as prod or dev), both with AES256.

The format is simple. The first line is a header, $ANSIBLE_VAULT;1.1;AES256 (or 1.2 with the label at the end). The body is hexadecimal text. Inside it there are three parts: a random 32-byte salt, an HMAC tag and the ciphertext. The key comes from the password with PBKDF2-HMAC-SHA256 and 10000 rounds, which gives an AES key, an HMAC key and a counter. The text is padded and encrypted with AES-256 in CTR mode, and the HMAC-SHA256 tag protects the ciphertext from changes.

Everything runs in your browser. The password and the secret are not sent anywhere. Still, think before pasting a production password into any web page: for the most sensitive secrets, use the command line on your own machine. Also remember that a vault is only as strong as its password, because anyone with the file can try passwords offline.

How to encrypt or decrypt a vault

  1. In What to do, choose Encrypt or Decrypt.
  2. To encrypt, type the secret in Text to encrypt:. To decrypt, paste the whole block in Vault to decrypt (the $ANSIBLE_VAULT block, or a !vault | block):. Indentation and the !vault | line are accepted.
  3. Type the Vault password:.
  4. When encrypting, choose the Format: (1.1 or 1.2). With 1.2 you can set the Vault id (label):. Fill in Variable name (optional): if you want the YAML value to start with the name, for example db_password.
  5. Click Encrypt again or Decrypt. The Result shows the vault as a file and as a YAML value.
  6. Copy it with Copy text, or use Download secret.vault. Decrypt this to check it sends the new vault to the decrypt side so you can confirm the round trip.

Examples

A vault made with OpenSSL, step by step

The password is ansible-pw, the text is hello vault and the salt is the bytes 00 to 1f. This is the exact vault the tool writes with that salt, and it decrypts back to hello vault:

$ANSIBLE_VAULT;1.1;AES256
30303031303230333034303530363037303830393061306230633064306530663130313131323133
3134313531363137313831393161316231633164316531660a653263633332653165623564646338
66373562313263663239316662656639626132313864636663643932333839656463313535343566
3962326665646464390a613631373862633334656638356664643863613032346433383939626431
3463

Normally the salt is random, so your vault looks different every time. That is expected: every one of them decrypts to the same text.

A value inside a YAML file

With the variable name db_password, the YAML result starts like this, followed by the hexadecimal lines indented under it:

db_password: !vault |
          $ANSIBLE_VAULT;1.1;AES256
          3030...

Paste it into group_vars or a playbook. You can paste the same block back into the decrypt side.

A vault with a label

With format 1.2 and the label prod, the first line is $ANSIBLE_VAULT;1.2;AES256;prod. Ansible uses the label to pick the right password when you have several, with --vault-id prod@prompt. If you leave it empty, the label is default.

Common mistakes

  • "Wrong password, or the vault was changed: the integrity check (HMAC) failed." The password does not match, or a character of the body changed. Check the password, the keyboard layout and trailing spaces.
  • "This is not an Ansible Vault. It must start with $ANSIBLE_VAULT;1.1;AES256 (or a !vault | line before it)." You pasted something else, or the first line was cut off. Paste from the header down.
  • "The body of the vault is damaged. After the first line it must be hexadecimal text; check that nothing was cut or changed." A line is missing or a character was altered by an editor or an email client.
  • "This vault format version is not supported. Versions 1.1 and 1.2 are." The header names another version.
  • "This vault uses the cipher {cipher}; only AES256 exists." The header names a different cipher.
  • "The vault id cannot have semicolons or line breaks." Remove them from the label.
  • "Type the vault password." The password cannot be empty.
  • Encrypted file, plain variable. A file made with encrypt is a whole-file vault, not a value. In a playbook, use encrypt_string, which is the YAML result here.

Frequently asked questions

Is a vault made here compatible with ansible-vault?

Yes. The tool writes the same format, and the test suite checks it byte for byte against a vault built with OpenSSL. You can encrypt here and decrypt with ansible-vault view, or the opposite.

Can I recover a vault if I forget the password?

No. There is no recovery and no back door. The only way in is to guess the password, and the 10000 PBKDF2 rounds make each guess a little slower, not impossible. Use a long, random password; the Diceware generator is a good source of one.

Why does the same text give a different vault each time?

Each encryption uses a new random salt, and the salt changes the key. The vaults differ but all decrypt to the same text with the same password.

Is AES256 vault the same as Jasypt or PGP?

No. They are different formats with different tools. For the Java world see the Jasypt encryptor, and for general encryption see the text encryption page.

Using the same vaults on the command line

Bash

# encrypt a value for a YAML file
ansible-vault encrypt_string 'hello vault' --name db_password

# encrypt and decrypt a file
ansible-vault encrypt secrets.yml
ansible-vault view secrets.yml
ansible-vault decrypt secrets.yml

# pick the password by label (format 1.2)
ansible-vault encrypt --vault-id prod@prompt secrets.yml
Report a problem