Mitesh The Mouse
2019-09-09 44eee23f4f6b107ad62b92490bbc056f6c4a8bcc
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
---
 
- debug: 
      msg: "./files/cloud_providers/{{ stack_file }}"
 
- name: Check if template exists for the environment
  stat:
    path: "./files/cloud_providers/{{ stack_file }}"
  register: stat_local_template
 
- debug: var=stat_local_template
 
- name: Use CloudFormation template from the environment
  set_fact:
    cloudformation_template_src1: "./files/cloud_providers/{{ stack_file }}"
  when: stat_local_template.stat.exists
 
- name: Use the default CloudFormation template
  set_fact:
    cloudformation_template_src1: "templates/cloud_template.j2"
  when: not stat_local_template.stat.exists
 
- name: Print cloudformation_template_src1
  debug:
    var: cloudformation_template_src1
 
 
#- debug: var=hostvars['localhost']
 
- set_fact:
    cloudformation_template: "{{output_dir}}/{{ env_type }}.{{ guid }}.{{cloud_provider}}_cloud_isolated_template"
 
- name: Print cloudformation_template
  debug:
    var: cloudformation_template
 
- pause: 
- name: AWS Generate CloudFormation Template
  template:
    src: "{{ cloudformation_template_src1 }}"
    dest: "{{ cloudformation_template }}"
  tags:
    - aws_infrastructure_deployment
    - gen_cf_template
 
######################### Copy CF Template to S3 if too big
- name: Stat CloudFormation template
  stat:
    path: "{{ cloudformation_template }}"
  register: stat_template
  tags:
    - aws_infrastructure_deployment
    - gen_cf_template
 
- when:
    stat_template.stat.size > 51200
  tags:
    - aws_infrastructure_deployment
    - gen_cf_template
  environment:
    AWS_ACCESS_KEY_ID: "{{aws_access_key_id}}"
    AWS_SECRET_ACCESS_KEY: "{{aws_secret_access_key}}"
    AWS_DEFAULT_REGION: "{{aws_region_final|d(aws_region)}}"
  block:
    - name: Get user name
      command: aws iam get-user
      register: rget_user
      ignore_errors: yes
 
    # Bucket name must be globally unique. Use the userID to define the bucketname.
    # Otherwise when we use the code in another account it fails because the bucket
    # already exists.
    - set_fact:
        aws_account_user: "{{rget_user.stdout|from_json|json_query('User.UserId')}}"
      ignore_errors: yes
 
    - set_fact:
        bucket_templates: "cloudformation-templates-{{aws_account_user|default('user')|lower}}"
 
    - name: Create bucket
      s3_bucket:
        name: "{{bucket_templates}}"
    - name: Copy Template to S3
      aws_s3:
        bucket: "{{bucket_templates}}"
        object: "{{ env_type }}.{{ guid }}.{{cloud_provider}}_cloud_template"
        src: "{{ cloudformation_template }}"
        mode: put
 
######################### Validate CF Template
 
- name: validate cloudformation template (local)
  environment:
    AWS_ACCESS_KEY_ID: "{{aws_access_key_id}}"
    AWS_SECRET_ACCESS_KEY: "{{aws_secret_access_key}}"
    AWS_DEFAULT_REGION: "{{aws_region_final|d(aws_region)}}"
  command: >-
    aws cloudformation validate-template
    --region {{ aws_region_final | d(aws_region) | default(region) | default('us-east-1')}}
    --template-body file://{{ cloudformation_template }}
  changed_when: false
  register: cloudformation_validation
  until: cloudformation_validation is succeeded
  retries: "{{ cloudformation_retries }}"
  delay: 20
  tags:
    - aws_infrastructure_deployment
    - validate_cf_template
  when: stat_template.stat.size <= 51200
 
- name: validate cloudformation template (S3)
  environment:
    AWS_ACCESS_KEY_ID: "{{aws_access_key_id}}"
    AWS_SECRET_ACCESS_KEY: "{{aws_secret_access_key}}"
    AWS_DEFAULT_REGION: "{{aws_region_final|d(aws_region)}}"
  command: >-
    aws cloudformation validate-template
    --region {{ aws_region_final | d(aws_region) | default(region) | default('us-east-1')}}
    --template-url https://s3.amazonaws.com/{{bucket_templates}}/{{env_type}}.{{guid}}.{{cloud_provider}}_cloud_template
  changed_when: false
  register: cloudformation_validation
  until: cloudformation_validation is succeeded
  retries: "{{ cloudformation_retries }}"
  delay: 20
  tags:
    - aws_infrastructure_deployment
    - validate_cf_template
  when: stat_template.stat.size > 51200
 
######################### Launch CF Template