使用 Ansible 从 AWS EC2 安全组中删除规则
Remove a rule from AWS EC2 Security group using Ansible
我有一个 Ansible 脚本来创建 EC2 安全组。它看起来像这样:
- name: Create HTTP Security Group
local_action:
module: ec2_group
region: "{{ region }}"
vpc_id: "{{ vpc }}"
name: sg_http
description: Security group for HTTP access
rules:
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
register: sg_http
然而,这创建了一个具有入站 http 访问权限但也具有完全出站访问权限的安全组。我想编写一个任务,删除 AWS 自动添加的出口规则,允许所有传出流量,但不允许整个安全组。我尝试使用当前状态,但它似乎没有按预期工作:
- name: Delete HTTP Rule
local_action:
module: ec2_group
region: "{{ region }}"
vpc_id: "{{ vpc }}"
name: sg_http
description: Security group for HTTP access
egress_rules:
- proto: all
from_port: 0
to_port: 65535
cidr_ip: 0.0.0.0/0
state: absent
register: sg_http
执行此操作的更好方法是什么?
默认情况下,ec2_groups 模块将幂等地设置为任何 present
组指定的规则,因为 purge_rules
和 purge_rules_egress
都默认为 true。
如果您之前的任务是创建一个类似于以下内容的 EC2 安全组:
- name: Create HTTP and HTTPS Security Group
local_action:
module: ec2_group
region: "{{ region }}"
vpc_id: "{{ vpc }}"
name: sg_http
description: Security group for HTTP(S) access
rules:
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 443
to_port: 443
cidr_ip: 0.0.0.0/0
register: sg_http
或者您已经使用上述规则创建了安全组,然后决定不阻止所有非 SSL 流量,您可以简单地将任务更改为以下内容:
- name: Create HTTPS only Security Group
local_action:
module: ec2_group
region: "{{ region }}"
vpc_id: "{{ vpc }}"
name: sg_http
description: Security group for HTTPS access
rules:
- proto: tcp
from_port: 443
to_port: 443
cidr_ip: 0.0.0.0/0
register: sg_http
要指定 outbound/egress 规则,您必须使用 rules_egress
(在 Ansible 1.6 中添加)。与 rules
一样,如果未在任何地方指定,则默认为允许所有流量。
因此,为了结合这一点,我们可能希望将一个盒子锁定为只能在 3306 上进行通信以与 MySQL 数据库盒子对话,但也只为传入的 HTTPS 流量提供服务:
- name: Create web server security group (HTTPS only inbound and MySQL only outbound)
local_action:
module: ec2_group
region: "{{ region }}"
vpc_id: "{{ vpc }}"
name: sg_http
description: Security group for HTTPS access
rules:
- proto: tcp
from_port: 443
to_port: 443
cidr_ip: 0.0.0.0/0
rules_egress:
- proto: tcp
from_port: 3306
to_port: 3306
cidr_ip: 0.0.0.0/0
register: sg_http
设置状态(即 absent
或 present
)仅在安全组本身而不是它们的子规则上受支持。
因此,以下任务将确保没有 sg_http
安全组:
- name: Remove sg_http Security Group
local_action:
module: ec2_group
region: "{{ region }}"
vpc_id: "{{ vpc }}"
name: sg_http
description: Security group for HTTP(S) access
state: absent
register: sg_http
如果您不指定规则,则会创建默认 'allow all' 规则。如果您创建一个空列表,则不会创建任何规则。这就是你想要的。
- name: Create HTTP Security Group
local_action:
module: ec2_group
region: "{{ region }}"
vpc_id: "{{ vpc }}"
name: sg_http
description: Security group for HTTP access
rules:
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
rules_egress: []
register: sg_http
我有一个 Ansible 脚本来创建 EC2 安全组。它看起来像这样:
- name: Create HTTP Security Group
local_action:
module: ec2_group
region: "{{ region }}"
vpc_id: "{{ vpc }}"
name: sg_http
description: Security group for HTTP access
rules:
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
register: sg_http
然而,这创建了一个具有入站 http 访问权限但也具有完全出站访问权限的安全组。我想编写一个任务,删除 AWS 自动添加的出口规则,允许所有传出流量,但不允许整个安全组。我尝试使用当前状态,但它似乎没有按预期工作:
- name: Delete HTTP Rule
local_action:
module: ec2_group
region: "{{ region }}"
vpc_id: "{{ vpc }}"
name: sg_http
description: Security group for HTTP access
egress_rules:
- proto: all
from_port: 0
to_port: 65535
cidr_ip: 0.0.0.0/0
state: absent
register: sg_http
执行此操作的更好方法是什么?
默认情况下,ec2_groups 模块将幂等地设置为任何 present
组指定的规则,因为 purge_rules
和 purge_rules_egress
都默认为 true。
如果您之前的任务是创建一个类似于以下内容的 EC2 安全组:
- name: Create HTTP and HTTPS Security Group
local_action:
module: ec2_group
region: "{{ region }}"
vpc_id: "{{ vpc }}"
name: sg_http
description: Security group for HTTP(S) access
rules:
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 443
to_port: 443
cidr_ip: 0.0.0.0/0
register: sg_http
或者您已经使用上述规则创建了安全组,然后决定不阻止所有非 SSL 流量,您可以简单地将任务更改为以下内容:
- name: Create HTTPS only Security Group
local_action:
module: ec2_group
region: "{{ region }}"
vpc_id: "{{ vpc }}"
name: sg_http
description: Security group for HTTPS access
rules:
- proto: tcp
from_port: 443
to_port: 443
cidr_ip: 0.0.0.0/0
register: sg_http
要指定 outbound/egress 规则,您必须使用 rules_egress
(在 Ansible 1.6 中添加)。与 rules
一样,如果未在任何地方指定,则默认为允许所有流量。
因此,为了结合这一点,我们可能希望将一个盒子锁定为只能在 3306 上进行通信以与 MySQL 数据库盒子对话,但也只为传入的 HTTPS 流量提供服务:
- name: Create web server security group (HTTPS only inbound and MySQL only outbound)
local_action:
module: ec2_group
region: "{{ region }}"
vpc_id: "{{ vpc }}"
name: sg_http
description: Security group for HTTPS access
rules:
- proto: tcp
from_port: 443
to_port: 443
cidr_ip: 0.0.0.0/0
rules_egress:
- proto: tcp
from_port: 3306
to_port: 3306
cidr_ip: 0.0.0.0/0
register: sg_http
设置状态(即 absent
或 present
)仅在安全组本身而不是它们的子规则上受支持。
因此,以下任务将确保没有 sg_http
安全组:
- name: Remove sg_http Security Group
local_action:
module: ec2_group
region: "{{ region }}"
vpc_id: "{{ vpc }}"
name: sg_http
description: Security group for HTTP(S) access
state: absent
register: sg_http
如果您不指定规则,则会创建默认 'allow all' 规则。如果您创建一个空列表,则不会创建任何规则。这就是你想要的。
- name: Create HTTP Security Group
local_action:
module: ec2_group
region: "{{ region }}"
vpc_id: "{{ vpc }}"
name: sg_http
description: Security group for HTTP access
rules:
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
rules_egress: []
register: sg_http