[英]Why do AWS EC2 RDP sessions continue to be active after removing the RDP rule from security group?
[英]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
要指定出站/出站规则,您必须使用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
如果您未指定规则,则将创建默认的“允许所有”规则。 如果创建一个空列表,则不会创建任何规则。 这就是您想要的。
- 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.