2016-12-13 57 views
2

在使用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)

任何幫助將不勝感激!

+0

源代碼中有一些[註釋](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/amazon/ec2_vpc.py#L585),指出_boto可以現在還沒有設置主表。不知道它是否仍然是一個有效的陳述。 –

+0

@KonstantinSuvorov謝謝你指出,這解釋了一下。麻煩的是,我並沒有真的試圖刪除它,我試圖追加它,但似乎行爲是刪除和重建。 – oucil

回答

1

http://docs.ansible.com/ansible/ec2_vpc_route_table_module.html#options

通過兩種標籤或者通過路由表ID查找路由表。非唯一標籤查找將失敗。 如果未指定標記,則不執行查找現有路由表並創建新的路由表。要更改路由表的標籤或刪除路由表,您必須按ID查找。

嘗試lookup: tag而不是lookup: id。 這應該工作。

0

我已經設法通過查找來更新主路由表,然後使用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試圖從主route_table刪除默認的子網關聯。