ansible-target-practice/site.yml

50 lines
1.5 KiB
YAML
Raw Normal View History

2020-10-24 11:14:47 +00:00
- hosts: tag_env_prod
remote_user: ec2-user
gather_facts: false # of no use
connection: local # prevent from trying to ssh into instance
vars:
target_group_arn: "arn:aws:elasticloadbalancing:eu-west-1:440357826049:targetgroup/TestAnsible/c2afd83500139d9a"
tasks:
- name: DEBUG
debug:
msg: "{{ hostvars[inventory_hostname].instance_id }}"
- name: Start instances
amazon.aws.ec2:
instance_ids: "{{ hostvars[inventory_hostname].instance_id }}"
state: running
when: targetpractice == "true"
- name: Wait for instances to be reachable
wait_for:
host: "{{ inventory_hostname }}"
port: 22
when: targetpractice == "true"
- name: Register targets in TargetGroup
community.aws.elb_target:
target_group_arn: "{{ target_group_arn }}"
state: present
target_id: "{{ hostvars[inventory_hostname].instance_id }}"
target_status: "unused"
when: targetpractice == "true"
- name: Deregister targets in TargetGroup
community.aws.elb_target:
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
amazon.aws.ec2:
instance_ids: "{{ hostvars[inventory_hostname].instance_id }}"
#instance_ids: "{{ play_hosts }}"
2020-10-30 01:56:42 +00:00
state: stopped
2020-10-24 11:14:47 +00:00
when: targetpractice == "false"