使用AWS中的Ansible将子网部署到不同的可用区域

时间:2015-12-15 17:51:23

标签: amazon-web-services ansible ansible-playbook subnet vpc

我正在使用ansible将子网放入AWS的不同可用区域。我想将subnet1放入us-east-1a,然后将subnet2放入us-east-1b,依此类推。目前我只能将服务器放到us-east-1a上。这是ansible脚本。

---
- name: Create AWS VPC and Subnets
  hosts: localhost
  connection: local
  gather_facts: false
  vars:
      region: us-east-1
      prefix: mahela_ansible
      az1: us-east-1a
      az2: us-east-1b
      az3: us-east-1c
 tasks:
 - name: Create VPC
   local_action:
     module: ec2_vpc
     region: "{{ region }}"
     cidr_block: 10.123.0.0/16
     resource_tags: '{"Name":"{{ prefix }}"}'
     subnets:
      - name: Cassandra Subnet
        cidr: 10.123.0.0/24
        az: "{{ az1 }}"
        resource_tags: '{"Name":"{{ prefix }}_cassandra"}'

      - name: MongoDB Subnet
        cidr: 10.123.1.0/24
        az: "{{ az2 }}"
        resource_tags: '{"Name":"{{ prefix }}_Mongodb"}'

      - name: Elastic Search
        cidr: 10.123.2.0/24
        az: "{{ az3 }}"
        resource_tags: '{"Name":"{{ prefix }}_elasticsearch"}'

1 个答案:

答案 0 :(得分:1)

这个例子可能对你有帮助。

roles/vpc/defaults/main.yml文件如下所示:

---
# Variables that can provide as extra vars
VPC_NAME: test
VPC_REGION: us-east-1 # N.Virginia
VPC_CIDR: "172.25.0.0/16"
VPC_CLASS_DEFAULT: "172.25"

# Variables for VPC
vpc_name: "{{ VPC_NAME }}"
vpc_region: "{{ VPC_REGION }}"
vpc_cidr_block: "{{ VPC_CIDR }}"
public_cidr_1: "{{ VPC_CLASS_DEFAULT }}.10.0/24"
public_az_1: "{{ vpc_region }}a"
public_cidr_2: "{{ VPC_CLASS_DEFAULT }}.20.0/24"
public_az_2: "{{ vpc_region }}b"
private_cidr_1: "{{ VPC_CLASS_DEFAULT }}.30.0/24"
private_az_1: "{{ vpc_region }}a"
private_cidr_2: "{{ VPC_CLASS_DEFAULT }}.40.0/24"
private_az_2: "{{ vpc_region }}b"

# Please don't change the variables below, until you know what you are doing
#
# Subnets Defination for VPC
vpc_subnets:
  - cidr: "{{ public_cidr_1 }}" # Public Subnet-1
    az: "{{ public_az_1 }}"
    resource_tags: { "Name":"{{ vpc_name }}-{{ public_az_1 }}-public_subnet-1", "Type":"Public", "Alias":"Public_Subnet_1" }
  - cidr: "{{ public_cidr_2 }}" # Public Subnet-2
    az: "{{ public_az_2 }}"
    resource_tags: { "Name":"{{ vpc_name }}-{{ public_az_2 }}-public-subnet-2", "Type":"Public", "Alias":"Public_Subnet_2" }
  - cidr: "{{ private_cidr_1 }}" # Private Subnet-1
    az: "{{ private_az_1 }}"
    resource_tags: { "Name":"{{ vpc_name }}-{{ private_az_1 }}-private-subnet-1", "Type":"Private", "Alias":"Private_Subnet_1" }
  - cidr: "{{ private_cidr_2 }}" # Private Subnet-2
    az: "{{ private_az_2 }}"
    resource_tags: { "Name":"{{ vpc_name }}-{{ private_az_2 }}-private-subnet-2", "Type":"Private", "Alias":"Private_Subnet_2" }

然后roles/vpc/tasks/main.yml文件将是这样的:

---
- name: Creating an AWS VPC inside mentioned Region
  ec2_vpc:
    region: "{{ vpc_region }}"
    state:  present
    cidr_block: "{{ vpc_cidr_block }}"
    resource_tags: { "Name":"{{ vpc_name }}-vpc", "Environment":"{{ ENVIRONMENT }}" }
    subnets: "{{ vpc_subnets }}" 
    internet_gateway: yes
  register: vpc

- name: Tag the Internet Gateway
  ec2_tag:
    resource: "{{ vpc.igw_id }}"
    region: "{{ vpc_region }}"
    state: present
    tags:
      Name: "{{ vpc_name }}-igw"
  register: igw

- name: Set up Public Subnets Route Table
  ec2_vpc_route_table:
    vpc_id: "{{ vpc.vpc_id }}"
    region: "{{ vpc_region }}"
    state: present
    tags:
      Name: "Public-RT-for-{{ vpc_name }}-vpc"
    subnets:
      "{{ vpc.subnets | get_public_subnets_ids('Type','Public') }}"
    routes:
      - dest: 0.0.0.0/0
        gateway_id: "{{ vpc.igw_id }}"
  register: public_rt

如需完整参考,请查看此github repo

希望它可以帮助你或其他人。