Skip to main content

Utility for Ansible Vault inline-encrypted variables

Project description

Vaulti

Like ansible-vault edit, but for files with inline encrypted variables!

Edit, create, encrypt and decrypt ansible-vault in-line encrypted variables in yaml files.

Usage example

https://github.com/user-attachments/assets/74480694-3333-4405-8f68-248af21c9999

Quick start

# Install it
pip install vaulti-ansible

# Open a file for editing
vaulti file.yml

# See more options
vaulti -h

Features

  • Encrypt or decrypt files by adding or removing custom tags
  • Change encrypted values directly
  • Works for simple variables, lists, dicts, multiline variables (literals) and anchors
  • Works with multiple / non-default vault-ids
  • Print decrypted files to stdout
  • Some Quality of Life features to let you reopen the file if simple mistakes are made

See docs folder for more examples and user guides

Why this exists

The standard ansible-vault works fine for encrypting/decrypting/editing whole files, but there are times you don't want to encrypt entire files; for example:

If you use AWX/AAP, having vault-encrypted files is a bit difficult; you either have to:

  • include the vault password in whichever container/Execution environment you are running the playbooks (therefore requiring a custom container image), or
  • decrypt the file when syncing the inventory (making all your secrets plaintext for those with high enough access in AWX)

Additionally, if your control repo is getting large, with lots of host_vars variables, group_vars variables, complex playbooks and roles, and you want to find out where certain variables are defined, you won't be able to search full vault-encrypted files easily, since all the keys are also encrypted.

So then you try inline encryption, which solves pretty much all of these problems, but using it with ansible-vault edit <file> is no longer possible... you now have to do something like this instead:

## To encrypt:
ansible-vault encrypt_string # <enter>
SomePasswordOrSomething # <Ctrl-D>, NOT <enter> unless you need the newline encrypted too
# Then copy the output into your yaml file, making sure the indentation is still ok

## To edit:
# Not possible, just encrypt a new string and replace it.

## To view:
ansible -i the/relevant/inventory the-relevant-host -m debug -a "var=TheRelevantVariable"

## To decrypt:
# Not possible, just view it and copy-paste the content where needed

Not really easy to remember the encrypt and view steps, pretty error-prone and requires you to actually run something with ansible, putting the variable somewhere where it will actually be read (hostvars or groupvars). It is much easier to just open the decrypted content and edit it directly.

Caveats

Editing yaml files using any YAML-loader is a "destructive" process. The content is loaded, and then dumped/re-generated as a new file after editing. Anything that isn't loaded, like comments or extra newlines will be gone.

Since I'm using ruamel.yaml, it stores the comments and newlines too, however it will still re-generate the content every time you use it. This results in some "non-negotiable" changes to your files that you should be aware of:

  • Indentation for your multiline strings will always end up with a fixed (default two) spaces relative to the variable it belongs to; i.e. not the 10 spaces indented or whatever the default is from the ansible-vault encrypt_string output. This is good for consistency, but it does mean that the indentation of your inline-encrypted variables will probably change the first time you use this if you've previously used ansible-vault encrypt_string to generate the encrypted strings. If you don't change the decrypted value it should remain the same though, except for the indent change.
  • Extra whitespaces will be removed whereever it is found (for example key: value -> key: value)

Also, there are a few "opinionated" things I've hardcoded, which are relatively easy to comment out or change in setup_yaml() if you wish it. I might make this configurable via a config file later if there is a need.

  • Header (---) and footer (...) will be added automatically to the variable file if it doesn't exist.
  • An indent equals two spaces
  • The hyphen starting each list items is indented, not inline with the parent key
  • An extra newline is added below the ansible-vault output, for readability.
  • No automatic line breaks for long values.

Finally, a word on diffs. The utility revolves around decrypting and reencrypting the variables, which means that every time you open a file with it, the encrypted string actually changes (because it has a different salt for each reencrypt). Part of the utility is therefore dedicated to looping through the re-encrypted file, comparing it with the original decrypted data, and preferring the old encrypted string if the actual decrypted value hasn't changed. That means that any git diff produced by these changes will usually only involve the relevant changed variables, but it is a "best effort" process based on very simple list index and dict key lookups. If you change the number encrypted variables in a list, the items whose list index was changed will be re-encrypted with a new salt, since the original value cannot be found. Same goes for any variables where you change the key name. Create the key/entry with a regular editor first if this is important to you.

Dependencies

Won't put the dependencies in the pyproject.toml file for now, since with Ansible, sometimes you want ansible-core on a specific version to keep a consistent execution environment. Any mention of the required libraries will make pip upgrade ansible and ansible-core packages even if the requirements don't make it necessary.

Having to use --no-deps for installing this tool in a non-breaking way is just asking for trouble.

Dependencies and their reasons are:

# We're using typing classes from Python3.9, and __future__ annotations is not not available before python 3.7.
# Since ansible seems to skip 3.7, let's just say 3.8. Might work with 3.7, if you are using that for some reason.
python>=3.8 

ruamel.yaml>=0.16.6 # Won't work before this version due to TaggedScalar changes
ansible>=2.4.0.0 # Won't work for older versions due to pycrypto

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vaulti_ansible-0.1.11.tar.gz (19.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

vaulti_ansible-0.1.11-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file vaulti_ansible-0.1.11.tar.gz.

File metadata

  • Download URL: vaulti_ansible-0.1.11.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for vaulti_ansible-0.1.11.tar.gz
Algorithm Hash digest
SHA256 88177a72e8208caa405cd66bcd66c36f6fac28eb3e4bb08bf3147fe060a2d5d2
MD5 f7e44e2309f52ff2c876d424086c52c9
BLAKE2b-256 7d6c225577c1b9db6bfc5ab0766004c3a562df2099ea665beca8ea20686e79c2

See more details on using hashes here.

Provenance

The following attestation bundles were made for vaulti_ansible-0.1.11.tar.gz:

Publisher: autopublish_pypi.yml on oveee92/vaulti

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file vaulti_ansible-0.1.11-py3-none-any.whl.

File metadata

File hashes

Hashes for vaulti_ansible-0.1.11-py3-none-any.whl
Algorithm Hash digest
SHA256 269109c40449e07c2b981d9539e93b972aa39c36222ff468369b36af94ce0644
MD5 0e8abd03512b36b651aab3fd90b95a25
BLAKE2b-256 77957d0cadba038f2b4aed36bcfa11ac9ec196f8b11364646e34261edbe0db62

See more details on using hashes here.

Provenance

The following attestation bundles were made for vaulti_ansible-0.1.11-py3-none-any.whl:

Publisher: autopublish_pypi.yml on oveee92/vaulti

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page