- 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/Target/ae7eb341ab1e701f" tasks: # - name: DEBUG # debug: # msg: "{{ hostvars[inventory_hostname].instance_id }}" - name: Start instances local_action: module: amazon.aws.ec2 instance_ids: "{{ hostvars[inventory_hostname].instance_id }}" state: running when: targetpractice == "true" - name: Wait for instances to be reachable local_action: module: wait_for host: "{{ inventory_hostname }}" port: 22 when: targetpractice == "true" - name: 'check if example file exists' stat: path: /tmp/example-exists when: blitz == "true" and targetpractice == "true" register: example_exists_file - name: Register targets in TargetGroup local_action: module: 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" and example_exists_file.stat.exists == True - name: Deregister targets in TargetGroup local_action: module: community.aws.elb_target target_group_arn: "{{ target_group_arn }}" state: absent target_id: "{{ hostvars[inventory_hostname].instance_id }}" target_status: "unused" deregister_unused: yes when: targetpractice == "false" - name: Stop instances local_action: module: amazon.aws.ec2 instance_ids: "{{ hostvars[inventory_hostname].instance_id }}" state: stopped when: targetpractice == "false"