我可以使用boto.vpc使用salt状态在AWS中创建VPC。但我还需要创建创建(除了VPC本身)子网,互联网网关,基于我能够创建的原始VPC的路由表。
因此,如果VPC定义如下所示:
Create VPC:
boto_vpc.present:
- name: dlab-new
- cidr_block: 10.0.0.1/24
- dns_hostnames: True
- region: us-east-1
- keyid: keyid
- key: key
如何在VPC配置的后续部分中引用原始VPC?因为在创建之前我不会知道VPC的vpc_id。是否有一个变量我可以在后续的子网,IGW和使用变量的路由表定义中使用?
Create subnet:
boto_vpc.subnet_present:
- name: dlab-new-subnet
- vpc_id: ?????
- cidr_block: 10.0.0.1/24
- region: us-east-1
- keyid: keyid
- key: key
Create internet gateway:
boto_vpc.internet_gateway_present:
- name: dlab-igw
- vpc_name: ????
- keyid: keyid
- key: key
Create route:
boto_vpc.route_table_present:
- name: my_route_table
- vpc_id: ???
- routes:
- destination_cidr_block: 10.0.0.1/24
instance_id: i-123456
- subnet_names:
- dlab-new-subnet
- region: us-east-1
- profile:
keyid: keyid
key: key
是否有任何方法可以使用变量代替-vpc_id值,以允许子网,IGW等定义获取Create VPC进程生成的VPC的名称?
您现在可以将“vpc_name”用于boto_vpc.subnet_present。您至少在版本盐2016.11.3(碳)时不需要使用“vpc_id”
更新:目前,以下示例中使用的每个状态模块都接受vpc_name
或vpc_id
,如文档中所述:
vpc_name: Name of the VPC in which the subnet should be placed. Either vpc_name or vpc_id must be provided.
vpc_id: Id of the VPC in which the subnet should be placed. Either vpc_name or vpc_id must be provided.
为了获得现有VPC的VPC ID,您可以使用boto_vpc execution module
您所在州的第一部分将创建一个名为dlab-new
的VPC,然后您可以从命令行salt minion_name boto_vpc.get_id dlab-new
测试它,如果找到匹配项将返回VPC ID。
可以在各州内调用执行模块,如下所示:
{% set vpc_id = salt.boto_vpc.get_id(name='dlab-new', region='us-east-1', keyid=keyid, key=key)['id'] %}
更多信息和示例JINJA IN STATES
vpc_id
变量将保存结果,在这种情况下将是dlab-new
的VPC Id,然后您可以将其传递给其他状态。完整的状态应该像这样
{% set custom_vpc_name = 'dlab-new' %}
{% set custom_keyid = keyid %}
{% set custom_key = key %}
{% set custom_region = 'us-east-1' %}
{% set cidr_block = '10.0.0.1/24' %}
{% set instance_id = 'i-123456' %}
{% set create_vpc = salt.boto_vpc.create(vpc_name=custom_vpc_name,cidr_block=cidr_block,enable_dns_hostnames=True,region=custom_region,keyid=custom_keyid,key=custom_key) %}
#this line is using boto_vpc execution module and get_id function which will return the VPC id if a match is found and your vpc will be created as described above with the name 'dlab-new'
{% set vpc_id = salt.boto_vpc.get_id(name=custom_vpc_name, region=custom_region, keyid=custom_keyid, key=custom_key)['id'] %}
Create subnet:
boto_vpc.subnet_present:
- name: {{ custom_vpc_name }}-subnet
- vpc_id: {{ vpc_id }}
- cidr_block: {{ cidr_block }}
- region: {{ custom_region }}
- keyid: {{ custom_keyid }}
- key: {{ custom_key }}
Create internet gateway:
boto_vpc.internet_gateway_present:
- name: {{ custom_vpc_name }}-igw
- vpc_id: {{ vpc_id }} # I have changed this line from vpc_name into vpc_id, is that what you meant ?
- keyid: {{ custom_keyid }}
- key: {{ custom_key }}
Create route:
boto_vpc.route_table_present:
- name: my_route_table
- vpc_id: {{ vpc_id }}
- routes:
- destination_cidr_block: {{ cidr_block }}
instance_id: {{ instance_id }}
- subnet_names:
- {{ custom_vpc_name }}-subnet
- region: {{ custom_region }}
- profile:
keyid: {{ custom_keyid }}
key: {{ custom_key }}
这是未经测试的代码,但我使用Salt完成了类似的状态。