diff --git a/README.md b/README.md index e87ad85..adbddd1 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,18 @@ # ansible-target-practice +This repository is an example that accomplishes the following: + +- stop/start based on a variable (`targetpractice`) +- If instances need to be started they are registered in a targetgroup when they become + reachable +- If instances need to be stopped the are first removed from the targetgroup + + +## Dependencies +``` +ansible-galaxy collection install amazon.aws +ansible-galaxy collection install community.aws +``` + ## EC2 credentials Make sure you have a profile that can access the necessary AWS resources. @@ -84,13 +98,17 @@ You can use the options defined in the AWS CLI `--filter` section. [](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html#options) - -## Run Ansible +## Test the inventory Testing the inventory can be done using `ansible-inventory` ``` ansible-inventory -i aws_ec2.yml --list ``` +## Testing + +``` +ansible-playbook -i aws_ec2.yml site.yml --extra-vars '{"targetpractice": "false"}' +``` diff --git a/ansible.cfg b/ansible.cfg new file mode 100644 index 0000000..b7f2c56 --- /dev/null +++ b/ansible.cfg @@ -0,0 +1,2 @@ +[defaults] +deprecation_warnings=False diff --git a/aws_ec2.yml b/aws_ec2.yml index dd90357..7f9df00 100644 --- a/aws_ec2.yml +++ b/aws_ec2.yml @@ -3,12 +3,14 @@ regions: - eu-west-1 filters: tag:env: - - dev - prod hostnames: + - network-interface.association.public-ip + - network-interface.addresses.private-ip-address - ip-address - network-interface.addresses.private-ip-address - private-dns-name + - instance-id keyed_groups: - key: tags.env separator: "" diff --git a/site.yml b/site.yml new file mode 100644 index 0000000..d12bea0 --- /dev/null +++ b/site.yml @@ -0,0 +1,49 @@ +- 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 + target_id: "i-0c6411e58bbaccfad" + 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 }}" + state: running + when: targetpractice == "false" +