AUTOMATESQL
Back to all guides
November 1, 2025•8 min

What is Inline Vault Encryption? A Guide to Using ansible-vault encrypt_string

Nov 01, 2025

vault inline encryption

In our last post, you learned how to create a fully encrypted file with ansible-vault create. This is the perfect solution for grouping a set of related secrets, like all the credentials for a new SQL Server build.

But what happens when you don't need to lock down an entire file?

Sometimes, you have a large configuration file with dozens of settings that are not sensitive. Think tempdb data and log file paths, edition to install, and which features of SQL Server to install. Hiding all that information inside an encrypted vault would be counterproductive, especially when you need to collaborate with other teams who need to see that configuration. You need a way to perform a more granular level of encryption—to protect just a small subset of the values while leaving the rest of the file in plain text.

Today, you'll learn how to do exactly that with ansible-vault encrypt_string.

Scenario: A Shared Variable File

We'll use a common example of creating a new environment for a dev team. This involves installing SQL Server 2025 Developer Edition on 4 new servers to support a new flagship product. You're using the automatesql.mssql.sql_install role for this deployment, and have defined a common set of variables to be used.

It may look something like this in a file named sqlservers.yml:

Most of this file is harmless configuration settings. But those *_svc_password values are a major problem. Checking this file into source control as-is would expose a credential. However, using ansible-vault create to encrypt the entire file would mean DBAs couldn't easily view or edit the remaining variables. We need a better way.

Visibility and Security

With ansible-vault encrypt_string, you get the best of both worlds. The command allows you to encrypt a single string and then paste that encrypted block right into your plain-text YAML file.

The sqlservers.yml file remains readable for everyone, promoting transparency and easy collaboration. But the sensitive service account passwords are transformed into an unreadable, encrypted block of text using AES-256 encryption. It can be safely checked into source control, and only Ansible, armed with the vault password, can decrypt it at runtime.

Inline Encryption in 4 Steps

Let's walk through the process.

Step 1: Create Your Plain-Text Variable File

First, create the sqlservers.yml file from the scenario above and save it in your project directory.

Step 2: Encrypt Your Secret Strings

Now, on the command line, we'll use ansible-vault encrypt_string. This command prompts you for the string you want to encrypt, asks for a vault password, and then outputs the encrypted block.

Run the following command:

  • --stdin-name 'sql_install_sql_svc_password' tells Ansible to create a YAML variable with this name.

The command will first prompt you for the vault password you want to use. You can use the same password from our last post or create a new one.

Technical Screenshot

Now, type the password you want to encrypt (MyPassword123) and then press Ctrl+D to finish. Ansible will output the encrypted block:

Technical Screenshot

Repeat this step for any additional passwords you need to encrypt (e.g., MyOtherPassword123). Use the same vault password for each variable.

Technical Screenshot

Step 3: Update Your YAML File

Copy the entire output, from sql_install_svc_password: and sql_install_agent_svc_password:, all the way to the last character. Go back into your sqlservers.yml file and replace the plain-text password lines with these new, encrypted blocks.

Your file should now look like this:

Step 4: Use the Variable in a Playbook

Using these variables is identical to using a variable from a fully vaulted file. Create a new playbook, test_inline_vault.yml, and again, use no_log: true on any task that handles the secret.

Run it with the --ask-vault-pass, and provide the password you used in step 2.

Technical Screenshot

This output is exactly what we wanted.

Ansible successfully reads and displays the plain-text variables, while the task using the vaulted password runs successfully, but conceals its output. You have achieved both visibility and security in the same file.

Conclusion:

Ansible Vault is not only powerful but flexible as well. You can protect entire files or encrypt single strings with granular precision, providing the flexibility to secure your automation in almost any scenario.

But what about true, hands-off automation?

Constantly typing in a vault password isn't practical for scheduled tasks or CI/CD pipelines.

In our next post, we'll solve that by learning how to use a vault password file.

See you next week!

Share this article:
The Database Platform Automation Roadmap

The Database Platform Automation Roadmap

Move from checklists and scattered scripts to desired-state Ansible playbooks—so you can stop wondering whether production still matches what you built. Includes the 60-second self-audit and 23-page field guide (PDF).