-
Notifications
You must be signed in to change notification settings - Fork 11
/
fetch_terraform_backend_outputs.py
160 lines (138 loc) · 3.92 KB
/
fetch_terraform_backend_outputs.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2018, Stefan Roman <[email protected]>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
ANSIBLE_METADATA = {
'status': ['preview'],
'supported_by': 'community',
'metadata_version': '1.1'
}
DOCUMENTATION = '''
---
module: fetch_terraform_backend_outputs
short_description: Get output variables from Terraform s3 backend.
description:
- Get output variables from Terraform s3 backend.
version_added: "2.4"
author: Stefan Roman (@katapultcloud)
options:
bucket:
description:
- Name of the s3 bucket where Terraform state is stored.
required: true
object:
description:
- Name of the s3 object where Terraform state is stored.
required: true
aws_profile:
description:
- Name of the aws profile to be used.
default: "default"
aws_access_key:
description:
- AWS access key to be used for bucket access.
- If declared aws_profile option is ignored and aws_secret_access_key option is required.
default: ""
aws_secret_access_key:
description:
- AWS secret access key to be used for bucket access.
- If declared aws_profile option is ignored and aws_access_key option is required.
default: ""
aws_region:
description:
- ID of AWS region to connect to s3 bucket from.
default: "us-east-1"
...
'''
EXAMPLES = '''
---
- name: Get Terraform EFS backend variables
fetch_terraform_backend_outputs:
bucket: "example-bucket"
object: "storage/terraform.tfstate"
register: terraform_storage
- name: Mount EFS storage
mount:
state: "mounted"
path: /mnt
src: "{{ terraform_storage.vars.efs_id }}"
fstype: efs
opts: rw
...
'''
RETURN = '''
---
vars:
description:
- Outputs from Terraform backend in JSON format are returned upon successful execution.
type: json
returned: success
version_added: "2.4"
...
'''
from ansible.module_utils.basic import *
import pprint
import boto3
import json
def format_data(data):
pretty_data = json.loads(data)
result = {}
permanent = pretty_data['outputs']
for key, value in permanent.items():
result[key] = value['value']
return result
def backend_pull(client, data):
s3 = client.resource('s3')
obj = s3.Object(data['bucket'], data['object'])
raw_data = obj.get()['Body'].read().decode('utf-8')
return format_data(raw_data)
def build_client(data, ansible_module):
aws_access_key = data['aws_access_key']
aws_secret_access_key = data['aws_secret_access_key']
aws_profile = data['aws_profile']
aws_region = data['aws_region']
if aws_access_key and aws_secret_access_key:
return boto3.session.Session(
aws_access_key_id=aws_access_key,
aws_secret_access_key=aws_secret_access_key,
region_name=aws_region)
elif not aws_access_key and not aws_secret_access_key:
return boto3.session.Session(profile_name=aws_profile)
else:
return False
def main():
arguments = {
"bucket": {
"required": True,
"type": "str"
},
"object": {
"required": True,
"type": "str"
},
"aws_profile": {
"default": "default",
"type": "str"
},
"aws_access_key": {
"default": "",
"type": "str"
},
"aws_secret_access_key": {
"default": "",
"type": "str"
},
"aws_region": {
"default": "us-east-1",
"type": "str"
}
}
module = AnsibleModule(argument_spec=arguments)
s3_client = build_client(module.params, module)
if s3_client:
result = backend_pull(s3_client, module.params)
module.exit_json(changed=False, vars=result)
else:
module.fail_json(msg="Wrong AWS credentials")
if __name__ == '__main__':
main()