使用 salt stack 和 boto.vpc 创建完整的 VPC

Create full VPC using salt stack and boto.vpc

我能够使用盐状态在 AWS 中创建 VPC,使用 boto.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 等的定义获得创建 VPC 过程生成的 VPC 的名称?

Update:目前,以下示例中使用的每个状态模块都接受 vpc_namevpc_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 如果找到匹配项,它将 return 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,然后您可以将其传递给其他状态。
  • VPC 名称、区域、aws 密钥和其他可以设置为状态顶部的变量,以便将来更容易修改,而不是遍历整个状态寻找它们。
  • 为了正确获得 vpc_id,您需要使用执行模块创建 VPC,因为将首先评估 jinja 代码。

完整状态应该是这样的

{% 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 完成了类似的状态。

您现在可以将 "vpc_name" 用于 boto_vpc.subnet_present。您至少不需要使用 "vpc_id" 版本 salt 2016.11.3 (Carbon)