Skip to content

Instantly share code, notes, and snippets.

@bbenson29
Created January 4, 2017 01:28
Show Gist options
  • Save bbenson29/d1b585fa645e48a2bcf7ab7a0f54a930 to your computer and use it in GitHub Desktop.
Save bbenson29/d1b585fa645e48a2bcf7ab7a0f54a930 to your computer and use it in GitHub Desktop.
Create multiple EC2 Security Group using Ansible loop
---
- hosts: localhost
connection: local
gather_facts: false
vars:
instances:
- name: consul.test.version
group: "test.version"
security_group: ["default", "consul.test.version"]
- name: apache.web.version
group: "web.version"
security_group: ["default", "apache.web.version"]
security_groups:
- name: consul.test.version
rules:
- proto: tcp
from_port: 8300
to_port: 8300
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 22
to_port: 22
cidr_ip: 0.0.0.0/0
rules_egress: []
- name: apache.web.version
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
- proto: tcp
from_port: 22
to_port: 22
cidr_ip: 0.0.0.0/0
rules_egress: []
tasks:
- name: Configure EC2 security groups
ec2_group:
name: "{{ item.name }}"
description: Example
region: "{{item.region | default('eu-west-2') }}"
state: present
rules: "{{ item.rules }}"
rules_egress: "{{ item.rules_egress }}"
with_items: "{{ security_groups }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment