gpt4 book ai didi

amazon-ec2 - 使用 Ansible 在 AWS 中创建 VPC

转载 作者:行者123 更新时间:2023-12-03 09:47:34 24 4
gpt4 key购买 nike

以下显示了我在 AWS 中创建 VPC 的 Ansible 剧本。

剧本将执行:

  1. 使用 CIDR 创建 VPC
  2. 然后创建路由表
  3. 然后标记路由表
  4. 最后创建路由表。

代码如下:

---
- name: To set up internet gateway
hosts: all
tasks:
- name: creating vpc
ec2_vpc:
region: ap-northeast-1
state: present
cidr_block: 20.0.0.0/16
resource_tags: { "Name":"Test" }
register: vpc
- name: Creating internet gateway for the vpc created
ec2_vpc_igw:
region: ap-northeast-1
state: present
vpc_id: "{{ vpc.vpc_id }}"
register: igw
- name: Tagging the gateway we just created
ec2_tag:
resource: "{{ igw.gateway_id }}"
#with_items: igw.gateway_id
state: present
region: ap-northeast-1
tags:
Name: test-test
- name: Creating route table
ec2_vpc_route_table:
region: ap-northeast-1
propagating_vgw_ids: yes
vpc_id: "{{ vpc.vpc_id }}"
subnets:
- '20.0.0.0/16'
routes:
- dest: 0.0.0.0/0
gateway_id: "{{ igw.gateway_id }}"

但是当我执行我的剧本时,我得到如下错误

failed: [172.30.1.237] => {"failed": true, "parsed": false}
Traceback (most recent call last):
File "/home/ubuntu/.ansible/tmp/ansible-tmp-1450103975.3-140284971977416/ec2_vpc_route_table", line 2411, in <module>
main()
File "/home/ubuntu/.ansible/tmp/ansible-tmp-1450103975.3-140284971977416/ec2_vpc_route_table", line 588, in main
result = ensure_route_table_present(connection, module)
File "/home/ubuntu/.ansible/tmp/ansible-tmp-1450103975.3-140284971977416/ec2_vpc_route_table", line 519, in ensur e_route_table_present
check_mode=check_mode)
File "/home/ubuntu/.ansible/tmp/ansible-tmp-1450103975.3-140284971977416/ec2_vpc_route_table", line 398, in ensure_propagation
dry_run=check_mode)
File "/usr/local/lib/python2.7/dist-packages/boto/vpc/__init__.py", line 1492, in enable_vgw_route_propagation
return self.get_status('EnableVgwRoutePropagation', params)
File "/usr/local/lib/python2.7/dist-packages/boto/connection.py", line 1227, in get_status
raise self.ResponseError(response.status, response.reason, body)
boto.exception.EC2ResponseError: EC2ResponseError: 400 Bad Request
<?xml version="1.0" encoding="UTF-8"?>
<Response><Errors><Error><Code>Gateway.NotAttached</Code><Message>resource True</Message></Error></Errors><RequestI D>4f34cefd-08c2-4180-b532-dd6e9e74f7e7</RequestID></Response>

除了缩进的错误,我还犯了什么错误。它创建 VPC 以及互联网网关。但是在使用路由表模块时。我收到错误消息。

最佳答案

我建议通过创建 VPC 来创建互联网网关,如下所示:

- name: To set up internet gateway
hosts: all
tasks:
- name: Create VPC and Subnet
ec2_vpc:
state: present
region: ap-northeast-1
cidr_block: 20.0.0.0/16
subnets:
- cidr: 20.0.0.0/16
resource_tags: {"Name":"Test Subnet"}
route_tables:
- subnets:
- 20.0.0.0/16
routes:
- dest: 0.0.0.0/0
gw: igw
wait: yes
internet_gateway: yes
resource_tags:
Name: "Test VPC"
register: vpc

- name: get igw
ec2_vpc_igw:
vpc_id: "{{ vpc.vpc_id }}"
region: ap-northeast-1
state: present
register: igw

- name: Tagging the new internet gateway created
ec2_tag:
resource: "{{ igw.gateway_id }}"
state: present
region: ap-northeast-1
tags:
Name: test-gateway

'gw' 选项可以接受 'igw' 并自动创建一个互联网网关,您可以在创建 VPC 后使用注册变量 'vpc' 标记互联网网关。

编辑:我更新了 playbook 并对其进行了测试,它可以正常工作。
像那样使用它。

关于amazon-ec2 - 使用 Ansible 在 AWS 中创建 VPC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34270293/

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