I'm trying to figure out how to provide the following facilities to a Python script so that it can:
ansible.cfg
and read vault_password_file
variablevault_password_file
and temporarily store in a Python variableI found this code via google but it did not appear to work when I tried it:
import ansible.utils
bar = dict()
bar = ansible.utils._load_vars_from_path("secrets.yml", results=bar, vault_password="password")
print bar
Throws this error:
$ python ansible-vault-ex.py
Traceback (most recent call last):
File "ansible-vault-ex.py", line 5, in <module>
bar = ansible.utils._load_vars_from_path("credentials.vault", results=bar, vault_password="password")
AttributeError: 'module' object has no attribute '_load_vars_from_path'
When I investigated this I saw no indications of this function in any Ansible related files, leading me to believe that this method no longer worked with some newer version(s) of Ansible.
Bottom line is that I'd like some method for importing Ansible libraries/modules from a Python script, so that I can interact with ansible-vault
managed files programmatically from Python.
Consider using the the ansible-vault package
Install it by:
$ pip install ansible-vault
and then it is as simple as:
from ansible_vault import Vault
vault = Vault('password')
print vault.load(open('/path/to/your/vault.yml').read())
To use the ansible code directly look at the source of that package. The simplest would be:
Ansible <= 2.3
from ansible.parsing.vault import VaultLib
vault = VaultLib('password')
print(vault.decrypt(open('/path/to/vault.yml').read()))
Ansible >= 2.4
from ansible.constants import DEFAULT_VAULT_ID_MATCH
from ansible.parsing.vault import VaultLib, VaultSecret
vault = VaultLib([(DEFAULT_VAULT_ID_MATCH, VaultSecret('password'.encode()))])
print(vault.decrypt(open('/path/to/vault.yml').read()))
The amount of source code is equal but the package provides automatic yaml parsing + handling of both Ansible versions.
If you have configured a vault_password_file in ansible.cfg, you can pass the password to VaultLib as followed
Import :
from ansible import constants as C
from ansible.parsing.vault import VaultLib
from ansible.cli import CLI
from ansible.parsing.dataloader import DataLoader
And then, you can call :
loader = DataLoader()
vault_secret = CLI.setup_vault_secrets(
loader=loader,
vault_ids=C.DEFAULT_VAULT_IDENTITY_LIST
)
vault = VaultLib(vault_secret)
vault.decrypt(open('/path/to/vault.yml').read())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With