将 Ansible playbook 中的变量替换为 EC2 安全组规则

cmm*_*bee 5 amazon-web-services ansible

我正在尝试使用 Ansible 来配置 AWS EC2 安全组。

我希望其中一些组有多个规则。有些规则仅适用于我的内部 VPC,而其他规则则对全世界开放。

我想在从列表变量读取配置的循环中创建安全组,但我不想对内部 VPC 的 CIDR 进行硬编码。我宁愿从我的 VPC 事实中获取 CIDR,但我还没有找到一种令人满意的方法将 CIDR 替换到规则中。

为了更清楚地说明这一点,这里有一个(人为的)示例。我的原始组列表对所有 CIDR 进行了硬编码:

aws_security_groups:
  - name: Webservers
    description: Security group for webservers
    region: my_aws_region
    rules: 
      - proto: tcp
        from_port: 22
        to_port: 22
        cidr_ip: 0.0.0.0/0
      - proto: tcp
        from_port: 80
        to_port: 80
        cidr_ip: 0.0.0.0/0
  - name: Databases
    description: Security group for internal database access
    region: my_aws_region
    rules: 
      - proto: tcp
        from_port: 22
        to_port: 22
        cidr_ip: 0.0.0.0/0
      - proto: tcp
        from_port: 3306
        to_port: 3306
        cidr_ip: <vpc.cidr.hard.coded/16>
Run Code Online (Sandbox Code Playgroud)

原剧的作品很简单:

- name: Provision EC2 security groups
  ec2_group:
    name: "{{ item.name }}"
    description: "{{ item.description }}"
    region: "{{ item.region }}"
    state: present
    rules: "{{ item.rules }}"
  with_items: "{{ aws_security_groups }}"
Run Code Online (Sandbox Code Playgroud)

我可以添加或减去规则,并且知道组将被同步。但是,我不想对 CIDR 进行硬编码...我希望我的组列表如下所示:

aws_security_groups:
  - name: Webservers
    description: Security group for webservers
    region: my_aws_region
    rules: 
      - proto: tcp
        from_port: 22
        to_port: 22
        cidr_ip: all
      - proto: tcp
        from_port: 80
        to_port: 80
        cidr_ip: all

  - name: Databases
    description: Security group for internal database access
    region: my_aws_region
    rules: 
      - proto: tcp
        from_port: 22
        to_port: 22
        cidr_ip: all
      - proto: tcp
        from_port: 3306
        to_port: 3306
        cidr_ip: internal
Run Code Online (Sandbox Code Playgroud)

我目前的策略是使用为每个规则单独with_subelements运行一堆ec2_group命令:

- name: Gather EC2 VPC facts
  ec2_vpc_net_facts:
    region: my_aws_region
  register: vpcs

- name: Provision EC2 security groups
  ec2_group:
    name: "{{ item.0.name }}"
    description: "{{ item.0.description }}"
    region: "{{ item.0.region }}"
    state: present
    purge_rules: false
    rules: 
      - from_port: "{{ item.1.from_port }}"
        to_port: "{{ item.1.to_port }}"
        proto: "{{ item.1.proto }}"
        cidr_ip: "{{ (item.1.cidr_ip == 'internal') | ternary(vpcs.vpcs.0.cidr_block, (item.1.cidr_ip == 'all') | ternary('0.0.0.0/0', item.1.cidr_ip)) }}"
  with_subelements: 
    - "{{ aws_security_groups }}"
    - rules
Run Code Online (Sandbox Code Playgroud)

替换工作,但我坚持一些不舒服的权衡。

首先,我必须关闭,purge_rules因为如果我不这样做,每个循环都会删除以前的规则。正因为如此,如果我对规则进行更改,我可能会被我需要跟踪和清理的旧规则所困扰。

其次,嵌套的三元组笨拙且不易扩展。

第三,当一个电话足够时,我会拨打多个电话。

我觉得我遗漏了一些明显的东西,或者我把它复杂化了。我怎样才能完成 CIDR 替换,或者更一般地说,Ansible 剧本中的任何此类替换?替代更可取还是有更好的方法来完成相同的任务?

非常感谢您对此的任何帮助。

tec*_*raf 5

以下方式可以避免purge_rules问题并且更清洁,恕我直言。

任务:

- set_fact:
    from_template: "{{ lookup('template', './template.j2') }}"
  vars:
    to_template_aws_security_groups: "{{ aws_security_groups }}"
    to_template_vpcs: "{{ vpcs }}"

- name: Provision EC2 security groups
  ec2_group:
    name: "{{ item.name }}"
    description: "{{ item.description }}"
    region: "{{ item.region }}"
    state: present
    rules: "{{ item.rules }}"
  with_items: "{{ from_template.aws_security_groups }}"
Run Code Online (Sandbox Code Playgroud)

template.j2

{
  "aws_security_groups": [
    {% for aws_security_group in to_template_aws_security_groups %}
    {
      "description": "{{ aws_security_group.description }}",
      "name": "{{ aws_security_group.name }}",
      "region": "{{ aws_security_group.region }}",
      "rules": [
        {% for rule in aws_security_group.rules %}
        {
          "cidr_ip": "{{ (rule.cidr_ip == 'internal') | ternary(to_template_vpcs.vpcs.0.cidr_block, (rule.cidr_ip == 'all') | ternary('0.0.0.0/0', rule.cidr_ip)) }}",
          "from_port": "{{ rule.from_port }}",
          "proto": "{{ rule.proto }}",
          "to_port": {{ rule.to_port }}
        }{% if not loop.last %},{% endif %}
        {% endfor %}
      ]
    }{% if not loop.last %},{% endif %}
    {% endfor %}
  ]
}
Run Code Online (Sandbox Code Playgroud)