Ansible - Encrypting Sensitive Data with Vault

Hi there! I'm Dinesh, a passionate Cloud and DevOps enthusiast. I love to dive into the latest new technologies and sharing my journey through blog.
Search for a command to run...

Hi there! I'm Dinesh, a passionate Cloud and DevOps enthusiast. I love to dive into the latest new technologies and sharing my journey through blog.
No comments yet. Be the first to comment.
1. Difference between Docker and Kubernetes Docker → Builds and runs containers.Kubernetes → Orchestrates containers across multiple nodes. Key points: Docker = container runtime. Kubernetes = container orchestration tool. Kubernetes provides auto...
In this session, we learn how to monitor a Kubernetes cluster using Prometheus and Grafana.This is not just theory — there is a GitHub repository containing all installation commands and demo steps.The repo will also be enhanced later with advanced K...
1. What is a ConfigMap in Kubernetes? A ConfigMap is used to store non-sensitive configuration data that your application needs — such as: Database port Connection type Any general configuration values In normal applications (non-Kubernetes), de...
Kubernetes normally supports built-in resources like: Deployment Service Pod ConfigMap Secret Ingress These are called native resources. Sometimes companies (Istio, ArgoCD, Prometheus Operator, Kyverno, etc.) want to add new features that Kub...
1. Why Kubernetes Services Are Needed When a Pod is created in Kubernetes, it receives a dynamic IP address.If the Pod dies and restarts, its IP changes.So other Pods (like checkout → payments) cannot rely on Pod IP because it changes, creating issue...
Ansible Vault is a feature within Ansible that allows you to keep sensitive data such as passwords, API keys, and certificates encrypted within your playbooks. This ensures that your sensitive information is not exposed in plaintext.
Step 1: Test Connection to Hosts
First, ensure you can connect to your hosts using Ansible:
ansible all -i inventory.ini -m ping

Step 2: Create a Vault File
Next, create a YAML file to store your sensitive data:
vi vault.yaml
Add your variables (e.g., username and password) inside this file:

You can check the content of the file with:
cat vault.yaml

Step 3: Create a Playbook
Create a playbook (pass.yaml) that references the vault file:
vi pass.yaml
Include the following content in your playbook:
- hosts: all
remote_user: ubuntu
become: yes
vars_files:
- vault.yaml
tasks:
- name: Trying out echo command
debug:
msg: "Hello my username is {{ username }} & pass is {{ password }}"


Step 4: Run the Playbook
Run the playbook to verify that it works with the variables from the vault file:
ansible-playbook -i inventory.ini pass.yaml


Step 5: Encrypt the Vault File
To protect your sensitive data, encrypt the vault.yaml file:
ansible-vault encrypt vault.yaml

You can check that the file is encrypted by running:
cat vault.yaml

Step 6: Run the Playbook with Encrypted Vault
Even with the vault file encrypted, you can still run your playbook as usual:
ansible-playbook -i inventory.ini pass.yaml

Alternatively, if you want to be prompted for the vault password at runtime, use:
ansible-playbook -i inventory.ini pass.yaml --ask-vault-password

Step 7: View the Encrypted Vault
If you need to view the contents of the encrypted vault, use:
ansible-vault view vault.yaml

Step 8: Decrypt the Vault File
To decrypt the vault file, run:
ansible-vault decrypt vault.yaml

Finally, verify the decryption by checking the content of the file:
cat vault.yaml

Following these steps, you can effectively use Ansible Vault to manage and secure sensitive data in your playbooks.