forked from adenot/ansible-terraform-module
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterraform.py
More file actions
101 lines (84 loc) · 3.09 KB
/
terraform.py
File metadata and controls
101 lines (84 loc) · 3.09 KB
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2016, Allan Denot <adenot@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
import os
import datetime
try:
import boto
HAS_BOTO = True
except ImportError:
HAS_BOTO = False
def main():
argument_spec = ec2_argument_spec()
argument_spec.update(dict(
dir=dict(required=True, default=None),
terraform_bin=dict(required=False, default="terraform"),
vars=dict(type='dict', required=False, default={}),
action=dict(required=False, default="apply")
)
)
module = AnsibleModule(
argument_spec = argument_spec,
supports_check_mode=False
)
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module)
# Put any passed access credentials into environment variables for terraform
os.environ["AWS_ACCESS_KEY_ID"] = aws_connect_kwargs['aws_access_key_id']
os.environ["AWS_SECRET_ACCESS_KEY"] = aws_connect_kwargs['aws_secret_access_key']
os.environ["AWS_SESSION_TOKEN"] = aws_connect_kwargs['security_token']
project_dir = module.params.get('dir')
terraform_bin = module.params.get('terraform_bin')
terraform_action = module.params.get('action')
vars = module.params.get('vars')
if terraform_action == "destroy":
terraform_command = "%s destroy -force" % terraform_bin
else:
terraform_command = "%s %s -input=false" % (terraform_bin, terraform_action)
for var_key in vars:
os.environ["TF_VAR_"+var_key] = vars[var_key]
os.chdir(project_dir)
startd = datetime.datetime.now()
rc, out, err = module.run_command(terraform_command)
endd = datetime.datetime.now()
delta = endd - startd
try:
with open('terraform.tfstate') as data_file:
state = json.load(data_file)
except:
with open('.terraform/terraform.tfstate') as data_file:
state = json.load(data_file)
if out is None:
out = ''
if err is None:
err = ''
module.exit_json(
stdout = out.rstrip("\r\n"),
stderr = err.rstrip("\r\n"),
rc = rc,
start = str(startd),
end = str(endd),
delta = str(delta),
changed = True,
outputs = state['modules'][0]['outputs'],
resources= state['modules'][0]['resources']
)
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
from ansible.module_utils.ec2 import *
main()