2020-10-24 11:14:47 +00:00
|
|
|
- hosts: tag_env_prod
|
|
|
|
remote_user: ec2-user
|
|
|
|
gather_facts: false # of no use
|
2020-10-31 10:01:05 +00:00
|
|
|
#connection: local # prevent from trying to ssh into instance
|
2020-10-24 11:14:47 +00:00
|
|
|
|
|
|
|
vars:
|
2020-10-31 10:01:05 +00:00
|
|
|
target_group_arn: "arn:aws:elasticloadbalancing:eu-west-1:440357826049:targetgroup/Target/ae7eb341ab1e701f"
|
2020-10-24 11:14:47 +00:00
|
|
|
|
|
|
|
tasks:
|
2020-10-31 10:01:05 +00:00
|
|
|
# - name: DEBUG
|
|
|
|
# debug:
|
|
|
|
# msg: "{{ hostvars[inventory_hostname].instance_id }}"
|
2020-10-24 11:14:47 +00:00
|
|
|
|
|
|
|
- name: Start instances
|
2020-10-31 10:01:05 +00:00
|
|
|
local_action:
|
|
|
|
module: amazon.aws.ec2
|
2020-10-24 11:14:47 +00:00
|
|
|
instance_ids: "{{ hostvars[inventory_hostname].instance_id }}"
|
|
|
|
state: running
|
|
|
|
when: targetpractice == "true"
|
|
|
|
|
|
|
|
- name: Wait for instances to be reachable
|
2020-10-31 10:01:05 +00:00
|
|
|
local_action:
|
|
|
|
module: wait_for
|
2020-10-24 11:14:47 +00:00
|
|
|
host: "{{ inventory_hostname }}"
|
|
|
|
port: 22
|
|
|
|
when: targetpractice == "true"
|
2020-10-31 10:01:05 +00:00
|
|
|
|
|
|
|
- name: 'check if example file exists'
|
|
|
|
stat:
|
|
|
|
path: /tmp/example-exists
|
|
|
|
when: blitz == "true" and targetpractice == "true"
|
|
|
|
register: example_exists_file
|
2020-10-24 11:14:47 +00:00
|
|
|
|
|
|
|
- name: Register targets in TargetGroup
|
2020-10-31 10:01:05 +00:00
|
|
|
local_action:
|
|
|
|
module: community.aws.elb_target
|
2020-10-24 11:14:47 +00:00
|
|
|
target_group_arn: "{{ target_group_arn }}"
|
|
|
|
state: present
|
|
|
|
target_id: "{{ hostvars[inventory_hostname].instance_id }}"
|
|
|
|
target_status: "unused"
|
2020-10-31 10:01:05 +00:00
|
|
|
when: targetpractice == "true" and example_exists_file.stat.exists == True
|
2020-10-24 11:14:47 +00:00
|
|
|
|
|
|
|
- name: Deregister targets in TargetGroup
|
2020-10-31 10:01:05 +00:00
|
|
|
local_action:
|
|
|
|
module: community.aws.elb_target
|
2020-10-24 11:14:47 +00:00
|
|
|
target_group_arn: "{{ target_group_arn }}"
|
|
|
|
state: absent
|
2020-10-30 01:56:42 +00:00
|
|
|
target_id: "{{ hostvars[inventory_hostname].instance_id }}"
|
2020-10-24 11:14:47 +00:00
|
|
|
target_status: "unused"
|
|
|
|
deregister_unused: yes
|
|
|
|
when: targetpractice == "false"
|
|
|
|
|
|
|
|
- name: Stop instances
|
2020-10-31 10:01:05 +00:00
|
|
|
local_action:
|
|
|
|
module: amazon.aws.ec2
|
2020-10-24 11:14:47 +00:00
|
|
|
instance_ids: "{{ hostvars[inventory_hostname].instance_id }}"
|
2020-10-30 01:56:42 +00:00
|
|
|
state: stopped
|
2020-10-24 11:14:47 +00:00
|
|
|
when: targetpractice == "false"
|
|
|
|
|