Bowe Strickland
2020-03-09 cb0b2619b9e8271f5a9b2f9b1a6b8d66ced3af9a
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
---
- 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:
    - when: ACTION == 'stop'
      name: Stop instances by (guid,env_type) tags
      ec2_instance:
        state: stopped
        wait: no
        filters:
          "tag:guid": "{{ guid }}"
          # TODO: uncomment this after a few weeks
          #"tag:env_type": "{{ env_type }}"
 
    - when: ACTION == 'start'
      name: Start instances by (guid, env_type) tags
      ec2_instance:
        state: started
        wait: no
        filters:
          "tag:guid": "{{ guid }}"
          # TODO: uncomment this after a few weeks
          #"tag:env_type": "{{ env_type }}"
 
    - when: ACTION == 'status'
      block:
        - name: Get EC2 facts using (guid, env_type) tag
          ec2_instance_facts:
            filters:
              "tag:guid": "{{ guid }}"
              # TODO: uncomment this after a few weeks
              #"tag:env_type": "{{ env_type }}"
          register: r_instances
 
        - name: Print status information to a file
          copy:
            dest: "{{ output_dir }}/status.txt"
            content: |-
              {{ "%-60s" | format('Instance') }} State      Type
              ----------------------------------------------------------------
              {% for instance in r_instances.instances %}
              {{ "%-60s" | format(instance.tags.Name) }} {{ "%-10s" | format(instance.state.name) }} {{ instance.instance_type }}
              {% endfor %}