-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.yaml
135 lines (124 loc) · 4.95 KB
/
main.yaml
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
---
- hosts: "{{ hosts_pattern | default('localhost') }}"
gather_facts: True
run_once: True
vars:
default_binaries:
'x86_64':
binary_url: 'https://releases.hashicorp.com/terraform/1.3.0/terraform_1.3.0_linux_amd64.zip'
sha256_checksum: '380ca822883176af928c80e5771d1c0ac9d69b13c6d746e6202482aedde7d457'
'aarch64':
binary_url: 'https://releases.hashicorp.com/terraform/1.3.0/terraform_1.3.0_linux_arm64.zip'
sha256_checksum: '0a15de6f934cf2217e5055412e7600d342b4f7dcc133564690776fece6213a9a'
'arm64':
binary_url: 'https://releases.hashicorp.com/terraform/1.3.0/terraform_1.3.0_darwin_arm64.zip'
sha256_checksum: '6a3512a1b1006f2edc6fe5f51add9a6e1ef3967912ecf27e66f22e70b9ad7158'
terraform_check_mode: false
use_remote_project: false
remote_execution: false
pre_tasks:
- name: Use Default Binaries If Not Provided By User
block:
- name: Discover Host CPU Arhitecture If Required
debug:
msg:
- "{{ ansible_architecture }}"
- name: Fail If Current CPU Arhitecture Has No Defaults
fail:
msg: "CPU arhitecture '{{ ansible_architecture }}' has no defaults, user input is required"
when: ansible_architecture not in default_binaries.keys()
- name: Set Binaries Variables
set_fact:
binary_archive_url: "{{ default_binaries[ansible_architecture]['binary_url'] }}"
binary_archive_sha256_checksum: "{{ default_binaries[ansible_architecture]['sha256_checksum'] }}"
when:
- binary_archive_url is not defined
- binary_archive_sha256_checksum is not defined
- name: Fail If Binary Provided Is Not Compiled For Host
fail:
msg:
- "Binary not compiled for {{ ansible_system | lower }}."
- 'Please provide correct archive.'
when: ansible_system | lower not in binary_archive_url | lower
- name: Fail If Infrastracture State Is Not Correct
fail:
msg:
- "terraform_plan_state must be set to 'present' or 'absent'."
when: terraform_plan_state is not in ['present', 'absent']
- name: Fail If Check Mode Is Not Correct
fail:
msg:
- "terraform_check_mode must be boolean ('true' or 'false')."
when: terraform_check_mode | lower not in ['true', 'false']
- name: Fail If Terraform Project Path Not Defined
fail:
msg:
- 'terraform_project_path is not defined.'
when: terraform_project_path is not defined
- name: Fail If Project Path Does Not Exist Locally
delegate_to: localhost
stat:
path: "{{ terraform_project_path }}"
register: terraform_project_path_stat
failed_when: not terraform_project_path_stat['stat']['exists']
when:
- not use_remote_project
- name: Fail If Project Path Does Not Exist On Remote
stat:
path: "{{ terraform_project_path }}"
register: terraform_project_path_stat
failed_when: not terraform_project_path_stat['stat']['exists']
when:
- use_remote_project
tasks:
- name: Prepare Remote Environment If Required
include_role:
name: remote_environment
tasks_from: prepare
when:
- ansible_host != 'localhost'
- not use_remote_project
- name: Terraform Provisioning
block:
- name: Prepare Terrafrom Environment
import_role:
name: terraform_environment
tasks_from: prepare
- name: Execute Terraform Plan
import_role:
name: terraform_environment
tasks_from: plan
rescue:
- name: Notify Error In Execution Terraform Plan
debug:
msg: 'Failed to execute terraform plan.'
always:
- name: Cleanup Remote Environment
include_role:
name: remote_environment
tasks_from: cleanup
when: ir_terraform_project_temp_dir is defined
- name: Cleanup Terraform Environment
import_role:
name: terraform_environment
tasks_from: cleanup
- name: Summarize Terraform Execution
debug:
msg:
- "Execution failed: {{ terraform_plan_execution['failed'] }}"
- "Output:"
- |-
{%- if 'stdout_lines' in terraform_plan_execution -%}
{{ terraform_plan_execution['stdout_lines'] }}
{%- else -%}
[]
{%- endif -%}
- "Error:"
- |-
{%- if 'stderr_lines' in terraform_plan_execution -%}
{{ terraform_plan_execution['stderr_lines'] }}
{%- else -%}
{%- set msg= terraform_plan_execution['msg'].strip() | regex_replace('[\r\n]+','STRING_TO_SPLIT') -%}
{{ msg.split('STRING_TO_SPLIT') }}
{%- endif -%}
failed_when: terraform_plan_execution is failed