I am working on an Ansible project in which I would like to add to my Auto-scaling group an existing EC2 instance found by tag-Name. I was able to find it with an AMI or terminating the old instances. But I am simply looking for a way to add them to auto-scaling group like in web management console. Where I just right click on instance, select settings, attach it to auto-scaling group. Below code is all in 1 file.
Find EC2 instances:
- hosts: localhost
connection: local
gather_facts: no
tasks:
- ec2_remote_facts:
region: eu-central-1
filters:
"tag:Name": Ubuntu_From_AMI
register: ec2found
- name: Add found instances to group
add_host: hostname="{{ item.public_ip_address }}" groups=ec2instances
with_items: "{{ ec2found.instances }}"
Here is how I am adding the auto-scaling group :
- hosts: localhost
connection: local
gather_facts: no
tasks:
- name: Add auto-scaling groups.
ec2_asg:
name: magento_scaling_group
load_balancers: 'LB_NAME'
availability_zones: [ 'eu-central-1a', 'eu-central-1b', 'eu-central-1c' ]
launch_config_name: "{{ lc.name }}"
min_size: 0
max_size: 5
desired_capacity: 0
vpc_zone_identifier: [ 'subnet-e712ad8c', 'subnet-e12e8dac', 'subnet-28e91a55' ]
tags:
- environment: production
propagate_at_launch: no
Is it possible? Thank you.