forked from redhat-cop/agnosticd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lifecycle_ec2.yml
66 lines (62 loc) · 2.23 KB
/
lifecycle_ec2.yml
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
---
- 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 EC2 instances filtering by guid
amazon.aws.ec2_instance_info:
filters:
"tag:guid": "{{ guid }}"
register: r_instances
- when:
- ACTION == 'stop'
- _instance_ids_running | length > 0
name: Stop instances
amazon.aws.ec2_instance:
state: stopped
wait: false
instance_ids: "{{ _instance_ids_running }}"
vars:
_instance_ids_running: >-
{{ r_instances.instances
| default([])
| selectattr("state.name", "equalto", "running")
| map(attribute="instance_id")
| list
}}
- when:
- ACTION == 'start'
- _instance_ids_stopped | length > 0
name: Start instances
amazon.aws.ec2_instance:
state: running
wait: false
instance_ids: "{{ _instance_ids_stopped }}"
vars:
_instance_ids_stopped: >-
{{ r_instances.instances
| default([])
| selectattr("state.name", "equalto", "stopped")
| map(attribute="instance_id")
| list
}}
- when: ACTION == 'status'
block:
- name: Report status in user info
agnosticd_user_info:
msg: |-
{{ "%-60s %-10s %s" | format("Instance", "State", "Type") }}
----------------------------------------------------------------
{% for instance in r_instances.instances %}
{{ "%-60s %-10s %s" | format(instance.tags.Name, instance.state.name, instance.instance_type) }}
{% endfor %}
- name: Print status information to a file
copy:
dest: "{{ output_dir }}/status.txt"
content: |-
{{ "%-60s %-10s %s" | format("Instance", "State", "Type") }}
----------------------------------------------------------------
{% for instance in r_instances.instances %}
{{ "%-60s %-10s %s" | format(instance.tags.Name, instance.state.name, instance.instance_type) }}
{% endfor %}