官术网_书友最值得收藏!

How to do it…

Let's start by creating an ELB. We will need to add the ELB to a security group. We also need to provide the region and subnet in which we want to create the ELB. We will make this ELB serve HTTP requests on port 80 and route the requests to port 80 of the instance:

- name: Create ELB in public subnet
ec2_elb_lb:
state: present
name: "{{ elb_name }}"
security_group_ids: "{{ my_first_sg.group_id }}"
region: "{{ aws_region }}"
subnets: "{{ my_public_subnet.subnet.id }}"
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
purge_subnets: yes
listeners:
- protocol: http
load_balancer_port: 80
instance_port: 80
register: my_first_elb
tags:
- elb
- recipe8

Once our ELB is set up, we need to identify the instances that we are going to attach to the ELB. One way to do so is by filtering the instance with a tag:

- name: Get all ec2 instances with given tags
ec2_instance_facts:
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
aws_region: "{{ aws_region }}"
filters:
"tag:Name": Public Instance
register: ec2_instances_public
tags:
- elb
- recipe8

Now that we have the instance(s) that we want to attach with the ELB, let's go ahead and attach them for real. The variable ec2_instances_public holds the details of the instances which we got by using the filter:

- name: Register all public instances with elb created
ec2_elb:
instance_id: "{{ item.id }}"
ec2_elbs: "{{ my_first_elb.elb.name }}"
state: present
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
aws_region: "{{ aws_region }}"
with_items:
- "{{ ec2_instances_public.instances }}"
ignore_errors: yes
tags:
- elb
- recipe8
主站蜘蛛池模板: 贵定县| 南丰县| 申扎县| 安泽县| 疏勒县| 广东省| 晋中市| 玉屏| 南阳市| 泰兴市| 祥云县| 桦川县| 隆子县| 彭泽县| 芮城县| 余庆县| 石城县| 江口县| 巴林左旗| 清新县| 台安县| 双峰县| 奎屯市| 响水县| 香格里拉县| 晋城| 佳木斯市| 临清市| 衡南县| 平武县| 定边县| 大港区| 延边| 莎车县| 合水县| 鹿泉市| 盐池县| 和硕县| 巴林右旗| 白山市| 馆陶县|