在使用Ansible和ec2_vpc_route_table模塊的NAT場景中,我很難將其他路由添加到新創建的VPC的默認路由表中。從我的劇本的相關節選......如何使用Ansible ec2_vpc_route_table將路由添加到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>"}
該錯誤使得它聽起來像,dispite我明確地提供route_table_id
的事實,和lookup: id
,它不更新現有的表,但它基本上正在重新創建它,試圖刪除過程中的原始表(即main
路由表)。
如何將附加路由附加到現有的main
路由表中?
到目前爲止,我已經能夠使用的唯一解決方法是通過command: ...
方法調用EC2 CLI,但這顯然不理想。
我們是在Ansible 2.3.0 (devel 14a2757116)
任何幫助將不勝感激!
源代碼中有一些[註釋](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/amazon/ec2_vpc.py#L585),指出_boto可以現在還沒有設置主表。不知道它是否仍然是一個有效的陳述。 –
@KonstantinSuvorov謝謝你指出,這解釋了一下。麻煩的是,我並沒有真的試圖刪除它,我試圖追加它,但似乎行爲是刪除和重建。 – oucil