Table of Contents


Protecting your secrets

Ansible provides a facility to protect your data at rest. That facility is Vault, which allows for encrypting text files so that they are stored “at rest” in encrypted format

  • Encrypting data at rest
  • Protecting secrets while operating

Creating new encrypted files

ansible-vault create secrets.yaml
 

Use vi to store some sensitive data

cat secrets.yaml 
 
$ANSIBLE_VAULT;1.1;AES256
34303139316234623835626236393234643961336164326334363236643762326636323936373432
3036363935336335393166393336386538656532396239630a343764396564386363633134386432
39303262653134333762363233333131626237366363383639363834626363653437643439643339
3534366131313163330a623765633961653335393433633333373230383037303664356562363565 36353965313036626230366563373835663137663638326163636462636532326366306530333037
3535363234363434623436383536653939386561346136373938
 

Explore ansible-vault with edit, view, decrypt, encrypt

Change password

ansible-vault rekey secrets.yaml
 

Decrypt Encrypted Files During Playbook Runtime

Encrypt inventory.ini

ansible-vault encrypt inventory.ini

run playbook sample

ansible-playbook -i inventory.ini ansible-var-example.yaml \
  --ask-vault-pass

Using Ansible Vault Password File

echo -n "123456" > password_file
ansible-playbook -i inventory.ini ansible-var-example.yaml \
  --vault-pass-file password_file

Encrypting individual variables

For example, to encrypt the string cfreire using the only password stored in password_file and name the variable username:

ansible-vault encrypt_string --vault-password-file password_file \
  'cfreire' --name 'username'
echo -n 'cfreire' | ansible-vault encrypt_string \
  --vault-password-file password_file

Read from stdin

ansible-vault encrypt_string --vault-password-file password_file

You can add the output from any of the examples above to any playbook, variables file, or role for future use. Encrypted variables are larger than plain-text variables, but they protect your sensitive content while leaving the rest of the playbook, variables file, or role in plain text so you can easily read it.

Exercise #1 - Encrypt string

Encrypt the variable directory on the following yaml file:

ansible-var-example.yaml`

---
- name: use variable in script
  hosts: all
  vars:
    directory: /root
  gather_facts: false
  
  tasks:
  - name: ping servers
    ansible.builtin.ping:
    
  - name: list contents
    ansible.builtin.command: "ls {{directory}}"
    register: dir_out
 
  - debug: 
      msg: "{{ dir_out.stdout_lines }}"
 

solution

# Create a password file
echo "123456" > my_password.txt
ansible-vault encrypt_string --vault-password-file \
	my_password.txt root' --name 'directory'
	
Encryption successful
directory: !vault |       $ANSIBLE_VAULT;1.1;AES256613331333663336364613938613538313030663866363735366134383663653738633966653930653138616137303538663139316530613830316163653935630a626331323766363661643235383134663237653664643738373965306430303261383563636466613936376234313039373562666139633736613265306638380a313466666266346232333865303266653164386663633736656330653164

Copy/Paste to Playbook

ansible-var-example.yaml

---
- name: use variable in script
  hosts: all
  gather_facts: false
  
  vars:
    directory: !vault |
$ANSIBLE_VAULT;1.1;AES256
37336663373337376334666436313434393539356563333266383835643664656465313038303639
3535326565393131346466346165353963656565393461640a363566373465656437646236663435
32656463326565663731343235386633633438663634643930386566636138653532316463396636
3834646234376161310a373530376536356231643630366634363538383730343935626231303665
3838
    
  tasks:
  - name: ping servers
    ansible.builtin.ping:
    
  - name: list contents
    ansible.builtin.command: "ls {{directory}}"
    register: dir_out
 
  - debug: 
      msg: "{{ dir_out.stdout_lines }}"
ansible-playbook ansible-var-example.yaml \
	--vault-password-file my_password.txt

Obfuscate secrets

Generate 100 files with passwords

generate-passwords.sh

#!/bin/bash
mkdir passwords && cd passwords
for i in {1..100}; do 
  echo $(date +%N | md5sum | awk '{print $1}') > $(date +%N | md5sum | awk '{print $1}')
sleep $(awk -v min=0.1 -v max=1.0 'BEGIN{srand(); print min+rand()*(max-min)}')
done

Not shoeing command on history

HISTCONTROL=ignoreboth
 
# Start with a space on next line
 ansible-playbook debug.yml --vault-password-file=passwords/<choose-one-file>

Exercise #2 - Using secrets

  • Start from scratch
  • Use all ansible nodes VM’s
  • Install ansible only node1
  • Create a playbook that prints uptime off node2 and node3
  • Encrypt hosts file using ansible-vault

Since hosts as to be encryped format must be yml

$ tree
├── ansible.cfg
├── group_vars
├── inventory
│   └── hosts.yml
├── README.md
├──.vault_pass.txt
└── uptime.yml
 

inventory/hosts.yml

all:
  hosts:
    node2:
      ansible_host: 172.31.2.172
    node3:
      ansible_host: 172.31.6.116

Solution

ansible.cfg

[defaults]
inventory=inventory/hosts.yml
vault_password_file=.vault_pass.txt
host_key_checking=false
retry_files_enabled=false
interpreter_python=auto_silent
remote_user=redhat

vault_password_file can be specify on ansible.cfg

uptime.yml

- name: Report uptime from gathered facts
  hosts: all
  gather_facts: true
 
  tasks:
    - name: Show uptime facts for this host
      ansible.builtin.debug:
         msg: >-
          {{ inventory_hostname }} has been up for
          {{ ansible_facts.uptime_seconds }} seconds
          ({{ (ansible_facts.uptime_seconds / 3600) | round(2) }} hours)

Create a password with:

echo $(date +%N | md5sum) > .vault_pass.txt

.vault_pass.txt

34d91f3e10d805b0bb2bf85b5b147d27

README.md


ansible-inventory -i hosts.yml --graph --vars
#1. Encrypt the inventory file

	ansible-vault encrypt inventory/hosts.yml
	
#2. Test access with ping all

	ansible all -m ping
	
#3. Run the playbook
	
	ansible-playbook uptime.yml
	
	
PLAY [Report uptime from facts] **********************************************************************

TASK [Gathering Facts] *******************************************************************************
ok: [node2]
ok: [node3]

TASK [Show uptime] ***********************************************************************************
ok: [node3] => {
    "msg": "node3 has been running for 12496 seconds. 3.47 hours"
}
ok: [node2] => {
    "msg": "node2 has been running for 12495 seconds. 3.47 hours"
}

PLAY RECAP *******************************************************************************************
node2                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node3                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0