【发布时间】:2016-12-13 21:00:28
【问题描述】:
我在使用 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_id 和lookup: id,它没有更新现有表,但它基本上是重新创建它,试图删除原始表(这是main路由表)在处理中。
如何将其他路由附加到现有的 main 路由表?
到目前为止,我能够使用的唯一解决方法是通过 command: ... 方法调用 EC2 CLI,但这显然并不理想。
我们在Ansible 2.3.0 (devel 14a2757116)
任何帮助将不胜感激!
【问题讨论】:
-
源码中有一些notes说明boto还不能设置主表。不确定它是否仍然是一个有效的声明。
-
@KonstantinSuvorov 感谢您指出这一点,这说明了一点。麻烦的是,我并不是真的想删除它,我想追加它,但似乎行为是删除和重建。
标签: amazon-ec2 routing ansible ansible-playbook ansible-2.x