gpt4 book ai didi

amazon-ec2 - 如何使用 Ansible ec2_vpc_route_table 将路由添加到 VPC 的默认/主路由表?

转载 作者:行者123 更新时间:2023-12-01 18:12:33 36 4
gpt4 key购买 nike

我在使用 Ansible 和 ec2_vpc_route_table 模块在 NAT 场景中向新创建的 VPC 的默认路由表添加额外路由时遇到了真正的麻烦。我的剧本中的相关摘录是...

创建 VPC

- name: Create Production VPC
ec2_vpc:
region: "{{ aws.region }}"
aws_access_key: "{{ aws.access_key }}"
aws_secret_key: "{{ aws.secret_key }}"
state: present
cidr_block: 10.0.0.0/16
resource_tags: { "Name":"Production" }
internet_gateway: yes
dns_hostnames: yes
dns_support: yes
subnets:
- cidr: 10.0.10.0/24
resource_tags: { "Name":"A - NAT" }
- cidr: 10.0.20.0/24
resource_tags: { "Name":"B - Public" }
- cidr: 10.0.30.0/24
resource_tags: { "Name":"C - Private" }
wait: yes
register: prod_vpc

收集事实并附加新路线

- name: Gather default Route Table facts
ec2_vpc_route_table_facts:
region: "{{ aws.region }}"
filters:
vpc-id: "{{ prod_vpc.vpc.id }}"
register: vpc_default_route

- name: Add NAT routes
ec2_vpc_route_table:
aws_access_key: "{{ aws.access_key }}"
aws_secret_key: "{{ aws.secret_key }}"
vpc_id: "{{ prod_vpc.vpc.id }}"
region: "{{ aws.region }}"
route_table_id: "{{ vpc_default_route.route_tables[0].id }}"
lookup: id
tags:
Name: NAT
subnets:
- '10.0.10.0/24'
routes:
- dest: 0.0.0.0/0
gateway_id: igw

首先,我尝试在 ec2_vpc 任务中包含路由的创建,但这实际上最终创建了第二个路由表,而不是在默认表中包含路由。

首先,为什么要创建第二个表而不是将路由添加到默认表中?

因为将其包含在 vpc 创建中不起作用,所以我回到了上面,它仅标识默认或main路由表。现在的问题是,当我尝试使用 ec2_vpc_route_table 添加 NAT 路由时,Ansible 失败并出现以下错误...

fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "Unable to associate subnets for route table RouteTable:rtb-..., error: EC2ResponseError: 400 Bad Request\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Response><Errors><Error><Code>InvalidParameterValue</Code><Message>cannot disassociate the main route table association rtbassoc-...</Message></Error></Errors><RequestID>...</RequestID></Response>"}

尽管我明确提供了 route_table_idlookup: id,但该错误听起来像是它没有更新现有表,但它基本上是重新创建它,尝试删除进程中的原始路由表(即main路由表)。

如何将其他路由附加到现有路由表?

到目前为止,我能够使用的唯一解决方法是通过 command: ... 方法调用 EC2 CLI,但这显然并不理想。

我们使用的是 Ansible 2.3.0 (devel 14a2757116)

任何帮助将不胜感激!

最佳答案

我通过查找并使用 route_table_id 修改主路由表,成功地更新了它。

- name: Lookup VPC facts
ec2_vpc_net_facts:
region: "{{ region }}"
filters:
"tag:Name": "{{ vpc_name }}"
register: vpc_group

- name: Create vgw
ec2_vpc_vgw:
region: "{{ region }}"
vpc_id: "{{ vpc_group.vpcs[0].id }}"
name: "{{ vpc_name }}"
type: ipsec.1
state: present
validate_certs: no
register: vpc_vgw

- name: Create igw
ec2_vpc_igw:
region: "{{ region }}"
vpc_id: "{{ vpc_group.vpcs[0].id }}"
state: present
validate_certs: no
register: igw

- name: Lookup route tables
ec2_vpc_route_table_facts:
region: "{{ region }}"
filters:
vpc-id: "{{ vpc_group.vpcs[0].id }}"
register: vpc_route_tables

- name: Setup route tables
ec2_vpc_route_table:
region: "{{ region }}"
vpc_id: "{{ vpc_group.vpcs[0].id }}"
lookup: id
purge_subnets: false
route_table_id: "{{ vpc_route_tables.route_tables[0].id }}"
subnets: "{{ subnet_ids }}"
routes:
- dest: 10.0.0.0/8
gateway_id: "{{ vpc_vgw.vgw.id }}"
- dest: 0.0.0.0/0
gateway_id: "{{ igw.gateway_id }}"

注意:您需要禁用清除子网,否则当 ansible 尝试从主路由表中删除默认子网关联时,您会收到错误消息。

关于amazon-ec2 - 如何使用 Ansible ec2_vpc_route_table 将路由添加到 VPC 的默认/主路由表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41130456/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com