- Ansible 2 Cloud Automation Cookbook
- Aditya Patawari Vikas Aggarwal
- 251字
- 2021-06-24 18:43:39
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
推薦閱讀
- RPA:流程自動化引領數字勞動力革命
- 網絡組建與互聯
- 中國戰略性新興產業研究與發展·增材制造
- LMMS:A Complete Guide to Dance Music Production Beginner's Guide
- 電子設備及系統人機工程設計(第2版)
- 智能鼠原理與制作(進階篇)
- INSTANT Adobe Story Starter
- Web璀璨:Silverlight應用技術完全指南
- 深度學習原理與 TensorFlow實踐
- Embedded Linux Development using Yocto Projects(Second Edition)
- FANUC工業機器人虛擬仿真教程
- 渲染王3ds Max三維特效動畫技術
- 巧學活用Photoshop
- Learning OpenShift
- 智能機器人:從“深藍”到AlphaGo