0
votes

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.

1

1 Answers

1
votes

Based on the current list of modules, it appears there is no such functionality. You'll need to create a new module or just cheat and use the aws cli in a normal command: invocation. If you go the route of creating a new module, please do consider submitting it as a PR to the Ansible project so others will benefit from your work.