将 Ansible 行动手册中的变量替换为 EC2 安全组规则



我正在尝试使用 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>

原剧有效,非常简单:

- 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 }}"

我可以添加或减去规则,并知道组将被同步。但是,我不想对 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

我目前的策略是使用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

替换有效,但我陷入了一些不舒服的权衡。

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

其次,嵌套的三元很笨拙,不容易扩展。

第三,我打多个电话就足够了。

我觉得我错过了一些明显的东西,或者我把这件事复杂化了。如何完成 CIDR 替换,或者更一般地说,在 Ansible 手册中完成任何此类替换?替代是更可取还是有更好的方法来完成相同的任务?

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

以下方法可以避免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 }}"

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 %}
]
}

最新更新