DEV Community

Mikhail Salosin
Mikhail Salosin

Posted on

4 3

Easy migration to Ansible Vault id

To keep all our tokens secure we use the Ansible vault to encrypt them. Historically all files with secrets were encrypted with a single password instead of using a vault id and password file. This week we decided to migrate to vault id.

All files encrypted with a password and without vault id specified will have the header $ANSIBLE_VAULT;1.1;AES256. We can use grep to find all files with this header. To do that run

grep "\$ANSIBLE_VAULT;1.1;AES256" group_vars/**/*.yml
Enter fullscreen mode Exit fullscreen mode

Now we have a list of files that looks like that:

group_vars/staging/amazon.yml:$ANSIBLE_VAULT;1.1;AES256
group_vars/staging/db.yml:$ANSIBLE_VAULT;1.1;AES256
group_vars/staging/docker_registry.yml:$ANSIBLE_VAULT;1.1;AES256
....
Enter fullscreen mode Exit fullscreen mode

Grep adds matched string at the end of every file. We can use the cut command to remove this part since we only need file names. cut -d: -f1 will leave only the file name.

And finally, we can use xargs to pass the file list to the ansible-vault rekey command to convert all encrypted files to encrypted files with vault id.

The full command will look like this:

grep "\$ANSIBLE_VAULT;1.1;AES256" group_vars/**/*.yml | cut -d: -f1 | xargs ansible-vault rekey --new-vault-id vaultID@vaultfile
Enter fullscreen mode Exit fullscreen mode

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more