使用Ansible

时间:2015-05-15 17:31:36

标签: amazon-web-services amazon-ec2 ansible ansible-playbook

我有一个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

最好的方法是什么?

2 个答案:

答案 0 :(得分:1)

默认情况下,ec2_groups模块会明确设置为任何present组指定的规则,因为purge_rulespurge_rules_egress都默认为true。

如果您以前有过创建EC2 secruity组的任务,如下所示:

- 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

设置状态(即absentpresent)仅在安全组本身而不是其子规则上受支持。

因此,以下任务将确保没有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

答案 1 :(得分:0)

如果您未指定规则,则会创建默认的“全部允许”规则。如果创建空列表,则不会创建任何规则。这就是你想要的。

- 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