-
Notifications
You must be signed in to change notification settings - Fork 11
/
gen-ansible-vars.py
executable file
·64 lines (50 loc) · 2.2 KB
/
gen-ansible-vars.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
import json
import os
import yaml
terraform_cmd = ['terraform', 'output', '-no-color', '-json']
ansible_var_file_paths = ('group_vars/all.yml', 'vars/main.yml')
def load_terraform_vars():
print('->> Reading Terraform Outputs')
with open('tf_outputs.json', 'r') as f:
raw_output = f.read()
vars = json.loads(raw_output)
for key, m in vars.iteritems():
vars[key] = m['value']
return vars
def update_ansible_vars(source_vars, var_file):
with open(var_file, "r+") as f:
f.seek(0)
vars = yaml.load(f)
changed = False
for k in vars:
val = source_vars.get(k)
if val is not None:
changed = True
vars[k] = str(val)
if changed:
print('-> Updated {}.'.format(var_file))
f.seek(0)
yaml_vars = yaml.dump(vars, default_flow_style=False)
print(yaml_vars)
f.write(yaml_vars)
f.truncate()
if __name__ == '__main__':
terraform_vars = load_terraform_vars()
print('->> Updating CircleCI vars file (run update-circleci-env.py to push to CircleCI).')
update_ansible_vars({'AWS_ACCESS_KEY_ID': terraform_vars.get('master_circleci_access_key'),
'AWS_SECRET_ACCESS_KEY': terraform_vars.get('master_circleci_access_secret'),
'AWS_CODE_DEPLOY_KEY': terraform_vars.get('circleci_access_key'),
'AWS_CODE_DEPLOY_SECRET': terraform_vars.get('circleci_access_secret'),
'AWS_CODE_DEPLOY_S3_BUCKET': terraform_vars.get('ci_bucket'),
'CI_BUCKET': terraform_vars.get('ci_bucket'),
'STATIC_MEDIA_BUCKET': terraform_vars.get('static_media_bucket'),
'CIRCLE_API_TOKEN': terraform_vars.get('circleci_api_token')},
'circleci.env.yml')
print('->> Updating Ansible vars')
print
for root, sub_folders, files in os.walk('playbooks'):
for file in files:
abs_path = os.path.join(root, file)
if abs_path.endswith(ansible_var_file_paths):
update_ansible_vars(terraform_vars, abs_path)