Created
March 30, 2020 15:45
-
-
Save prelegalwonder/448ee7f0a1e246744f83661b89feed5e to your computer and use it in GitHub Desktop.
TF - Ansible Vault External Datasource
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import string,sys,os,types | |
import json | |
from ansible_vault import Vault | |
def readPass(passFile): | |
try: | |
file = open(passFile, 'r') | |
contents = file.read() | |
cleaned = contents.strip('\n') | |
return cleaned | |
except EOFError as ex: | |
print("Caught the EOF error.") | |
raise ex | |
except IOError as ex: | |
print("Caught the I/O error.") | |
raise ex | |
passFile = sys.argv[1] | |
vaultFile = sys.argv[2] | |
param = sys.argv[3].split('.') | |
paramLen = len(param)-1 | |
passwd = readPass(passFile) | |
vault = Vault(passwd) | |
data = vault.load(open(vaultFile).read()) | |
for val in param: | |
if val in data and param.index(val) != paramLen : | |
data = data.pop(val) | |
else: | |
if not isinstance(data[val], str) and len(data[val]) > 1: | |
print(json.dumps(data[val])) | |
else: | |
newdict = {} | |
newdict[val] = data[val] | |
print(json.dumps(newdict)) |
Author
prelegalwonder
commented
Mar 30, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment