forked from jhaals/ansible-vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvault.py
38 lines (28 loc) · 985 Bytes
/
vault.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os
import urllib2
import json
import sys
import hvac
from subprocess import check_output
from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase
class LookupModule(LookupBase):
def run(self, terms, variables, **kwargs):
key = terms[0]
try:
field = terms[1]
except:
field = None
user_id = os.getenv('VAULT_USER_ID')
if not user_id:
user_id = check_output(['sudo dmidecode -s system-uuid'], shell=True).rstrip()
url = os.getenv('VAULT_ADDR')
if not url:
raise AnsibleError('VAULT_ADDR environment variable is missing')
app_id = os.getenv('VAULT_APP_ID')
if not app_id:
raise AnsibleError('VAULT_APP_ID environment variable is missing')
client = hvac.Client(url=url)
client.auth_app_id(app_id, user_id)
result = client.read(key)
return [result['data'][field]] if field is not None else [result['data']]