Skip to main content

Working with Ansible Vault

1. Creating Encrypted Files

To create a new encrypted file, use the ansible-vault create command. This will prompt you to enter a password and then open your default editor to write the content.

Command Description
ansible-vault create <var>filename.yml</var> Create a new encrypted file with the specified name

Example: Creating an encrypted file

ansible-vault create secret_vars.yml

2. Encrypting Existing Files

To encrypt an existing file, use the ansible-vault encrypt command. This will prompt you for a password and then encrypt the file in place.

Command Description
ansible-vault encrypt <var>filename.yml</var> Encrypt an existing file with the specified name

Example: Encrypting an existing file

ansible-vault encrypt existing_file.yml

3. Decrypting Files

To decrypt an encrypted file, use the ansible-vault decrypt command. This will prompt you for a password and then decrypt the file in place.

Command Description
ansible-vault decrypt <var>filename.yml</var> Decrypt an existing encrypted file with the specified name

Example: Decrypting a file

ansible-vault decrypt secret_vars.yml

4. Editing Encrypted Files

To edit an encrypted file, use the ansible-vault edit command. This will open your default editor with the decrypted content.

Command Description
ansible-vault edit <var>filename.yml</var> Edit an encrypted file with the specified name

Example: Editing an encrypted file

ansible-vault edit secret_vars.yml

5. Viewing Encrypted Files

To view the contents of an encrypted file, use the ansible-vault view command. This will display the decrypted content without modifying the file.

Command Description
ansible-vault view <var>filename.yml</var> View the decrypted content of an encrypted file

Example: Viewing an encrypted file

ansible-vault view secret_vars.yml

6. Changing Vault Password

To change the password for an encrypted file, use the ansible-vault rekey command. This will prompt you for the current password and then ask for a new password.

Command Description
ansible-vault rekey <var>filename.yml</var> Change the password for an encrypted file

Example: Changing the vault password

ansible-vault rekey secret_vars.yml

7. Using Vault in Playbooks

To use encrypted variables in playbooks, reference them using the ansible.builtin.lookup plugin with the vault source.

Plugin Description Usage
ansible.builtin.lookup Lookup plugin to access encrypted variables lookup('vault', 'secret_key')

Example: Using vault in a playbook

- name: Use encrypted variable
  hosts: localhost
  tasks:
    - name: Print secret value
      debug:
        msg: "Secret is {{ lookup('vault', 'secret_key') }}"

8. Using Vault Password File

To avoid entering the password manually each time, you can store it in a file and use it with the --vault-password-file option.

Option Description Usage
--vault-password-file Specify a file containing the vault password ansible-playbook playbook.yml --vault-password-file /path/to/password_file

Example: Using a vault password file

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

9. Using Multiple Vault IDs

You can use multiple vault IDs to organize encrypted content. Each ID has its own password and can be used independently.

Option Description Usage
--vault-id Specify a vault ID to use for encryption/decryption ansible-vault create --vault-id dev@vault_pass.txt secret_vars.yml

Example: Using multiple vault IDs

ansible-vault create --vault-id dev@vault_pass.txt secret_vars.yml

10. Encrypting Variables

To encrypt a variable directly, use the ansible-vault encrypt_string command. This will output an encrypted string that can be used in playbooks.

Command Description Usage
ansible-vault encrypt_string Encrypt a string and output it in the correct format ansible-vault encrypt_string 'secret_value' --name 'my_secret'

Example: Encrypting a variable string

ansible-vault encrypt_string 'secret_value' --name 'my_secret'

11. Using Inline Vault Variables

You can use inline vault variables in playbooks using the !vault tag.

Tag Description Usage
!vault Tag to indicate a vault-encrypted value my_secret: !vault 'ENC[AES256_GCM,data=...]'

Example: Using inline vault variables

- name: Use inline vault variable
  hosts: localhost
  tasks:
    - name: Print secret value
      debug:
        msg: "Secret is {{ my_secret }}"

12. Setting Vault ID Default

To set a default vault ID for all operations, you can configure it in the ansible.cfg file.

Configuration Description Usage
vault_id_default Set the default vault ID to use for operations vault_id_default = dev

Example: Setting default vault ID in ansible.cfg

[defaults]
vault_id_default = dev

Was this article helpful?