If I encrypt host_vars/*
files with ansible-vault
, I don't seem to have a chance to have nonencrypted host vars other than those residing in the inventory file. Am I missing something?
相关问题
- Access ansible.cfg variable in task
- Unable to get “exclude” option working with unarch
- Ansible: find file and loop over paths
- Generating tuples variables in ansible templates
- Ansible: setting user on dynamic ec2
相关文章
- Ansible create postgresql user with access to all
- Difference between shell and command in ansible
- How to make Ansible run one certain task only on o
- clone a specific branch from git through ansible p
- Check if a list contains an item in Ansible
- Defining host as variable in Ansible hosts file
- ansible/jinja2 get unique subelements
- Custom credentials in Ansible Tower with Custom Py
As it turns out,
host_vars
- andgroup_vars
-files might be directories in actuality. That is, instead of creatinghost_vars/example.com
one might createhost_vars/example.com/vault
andhost_vars/example.com/vars
. All the files residing in the directory are read. Which settles it.Additionally, the best practice is to store sensitive variables prefixed with
vault_
in thevault
file, and reassign them to non-prefixed variables in thevars
file. Like so:vault
:vars
:That way, you'll be able to find encrypted variables with
grep
, or any similar tool.Here is a full example, with all of the linkages penciled in. Something I sorely needed while I was nutting this out. Context, context, context.
Ansible config
Ansible will automatically load this file if it is in the same directory as your called playbook.
Hosts file
This file defines host groups. In this example, only the
local
group is important.Playbook to gather ec2 facts about any of my instances with the following attributes:
ap-southeast-1
regionName: ec2-go
The playbook.
Now for the Ansible Vault MAGIC that arises from the following directory structure
Guess what? I made no effort to access any of these files with
vars_files
in my playbook, that's the MAGIC! When my playbook is addressing the host group[local]
, ansible automatically accesses the/path/to/playbook/group_vars/local
directory and loads thevars
AND the encryptedvault
file if we reference an encrypted var (we'll get to that right now).Vars
Notice that we automatically access the encrypted vault file by prepending bare vars with
vault_
. No further action required to link these up. It is idiomatic (best practice) to keep the var names the same, i.e.myVar
-->vault_myVar
. The advantage of all this dancing around is that it makes variable names visible (thinkgrep
), rather than hidden within an encrpyted file.Vault (unencrpyted)
We encrypt this file with
ansible-vault encrypt vault
and a password. We decrypt withansible-vault decrypt vault
or more safely, just edit without decryption withansible-vault edit vault
:Vault (encrypted)
$ANSIBLE_VAULT;1.1;AES256 36613032373533636330363539653565343463333135633564303036653732616435663462306637 6363383237386461613338626362653465343636366264610a623537393937646635366638393362 65353235653166396565333231336332666135663239386162633862356534393066383265333466 6465373962326662350a613339343234376564373662316234653364386337323130663039313239 35353031393437333463346132643632323865623963373862303539363162326161396464353031 39336334346438396161386365653161653231396430646433613132376233666431663863393066 35343433623563653164353730386339316366656666306265353931393337363937376632396332 32333164393534366337663333626566366134373766373137336366366230613763333939633165 35383432666233656363316630643031366431656261386531326162343035393739653366353462 3930356637616130373033333266393639666233666362313935
Run the playbook:
You'll need the cmdline argument
-ask-vault-pass
because of that ONE variable that is referenced from the vault.You can use this ansible feature : http://docs.ansible.com/ansible/playbooks_best_practices.html#best-practices-for-variables-and-vaults
Ansible will read automatically vault.yml as encrypted yaml file.
Simply don't encrypt
host_vars/*
, but instead encrypt only variable files that you want encrypted. This article describes a really nice approach: https://www.reinteractive.net/posts/167-ansible-real-life-good-practicesEssentially what you have are nested/chained variables.
This is your plain text variable file:
And this is your variable file that you are going to encrypt:
In your playbook you refer to
db_password
and it'll resolve into the encrypted password. Using this approach your variable names are still readable plain text, however variable values are securely encrypted.