How to create an AWS EC2 instance and assign it an ELB using Ansible?

I am trying to use Ansible to create an instance of EC2, configure a web server and then register it in a load balancer. I have no problem creating an EC2 instance or setting up a web server, but all attempts to register it with an existing load balancer are not without changing errors depending on the code used.

Has anyone been successful in this?

Here are links to Ansible documentation for ec2 and ec2_elb modules:

http://docs.ansible.com/ec2_module.html

http://docs.ansible.com/ec2_elb_module.html

Alternatively, if it is not possible to register an EC2 instance against creating an ELB message, I would agree to another “game” that collects all EC2 instances with a specific name and goes through them, adding them to the ELB.

+3
source share
2 answers

That's what I'm doing:

- name: Add machine to elb
  local_action:
      module: ec2_elb
      aws_access_key: "{{lookup('env', 'AWS_ACCESS_KEY')}}"
      aws_secret_key: "{{lookup('env', 'AWS_SECRET_KEY')}}"
      region: "{{ansible_ec2_placement_region}}"
      instance_id: "{{ ansible_ec2_instance_id }}"
      ec2_elbs: "{{elb_name}}"
      state: present

The biggest problem was access and private keys. The ec2_elb module does not seem to use environment variables or read ~ / .boto, so I had to pass them manually.

Variables ansible_ec2_*are available if you use the ec2_facts module. You can fill in these parameters yourself.

+3
source

ec2 . , , , playbook.

- name: Creating webserver
  local_action:
    module: ec2
    region: "{{ region }}"
    key_name: "{{ key }}"
    instance_type: t1.micro
    image: "{{ ami_id }}"
    wait: yes
    assign_public_ip: yes
    group_id: ["{{ sg_webserver }}"]
    vpc_subnet_id: "{{ PublicSubnet }}"
    instance_tags: '{"Name": "webserver", "Environment": "Dev"}
  register: webserver
- name: Adding Webserver to ELB
  local_action: 
    module: ec2_elb
    ec2_elbs: "{{ elb_name }}"
    instance_id: "{{ item.id }}"
    state: 'present'
    region: "{{ region }}"
  with_items: nat.instances
0

All Articles