2014-09-26 01:01:01 +00:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
2015-07-04 03:57:53 +00:00
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
2014-09-26 01:01:01 +00:00
2017-03-14 16:07:22 +00:00
ANSIBLE_METADATA = { ' metadata_version ' : ' 1.0 ' ,
' status ' : [ ' stableinterface ' ] ,
' supported_by ' : ' curated ' }
2016-12-06 10:35:05 +00:00
2014-09-26 01:01:01 +00:00
DOCUMENTATION = '''
- - -
module : ec2_group
2015-06-15 18:41:22 +00:00
author : " Andrew de Quincey (@adq) "
2014-09-26 01:01:01 +00:00
version_added : " 1.3 "
short_description : maintain an ec2 VPC security group .
description :
- maintains ec2 security groups . This module has a dependency on python - boto > = 2.5
options :
name :
description :
- Name of the security group .
required : true
description :
description :
2017-03-07 19:55:17 +00:00
- Description of the security group . Required when C ( state ) is C ( present ) .
required : false
2014-09-26 01:01:01 +00:00
vpc_id :
description :
- ID of the VPC to create the group in .
required : false
rules :
description :
2017-03-17 20:14:20 +00:00
- List of firewall inbound rules to enforce in this group ( see example ) . If none are supplied ,
no inbound rules will be enabled . Rules list may include its own name in ` group_name ` .
This allows idempotent loopback additions ( e . g . allow group to acccess itself ) .
2014-09-26 01:01:01 +00:00
required : false
rules_egress :
description :
2017-03-17 20:14:20 +00:00
- List of firewall outbound rules to enforce in this group ( see example ) . If none are supplied ,
a default all - out rule is assumed . If an empty list is supplied , no outbound rules will be enabled .
2014-09-26 01:01:01 +00:00
required : false
version_added : " 1.6 "
state :
version_added : " 1.4 "
description :
- Create or delete a security group
required : false
default : ' present '
choices : [ " present " , " absent " ]
aliases : [ ]
purge_rules :
version_added : " 1.8 "
description :
- Purge existing rules on security group that are not found in rules
required : false
default : ' true '
aliases : [ ]
purge_rules_egress :
version_added : " 1.8 "
description :
2014-11-29 05:05:22 +00:00
- Purge existing rules_egress on security group that are not found in rules_egress
2014-09-26 01:01:01 +00:00
required : false
default : ' true '
aliases : [ ]
2016-12-08 02:33:38 +00:00
extends_documentation_fragment :
- aws
- ec2
2014-09-26 01:01:01 +00:00
notes :
- If a rule declares a group_name and that group doesn ' t exist, it will be
automatically created . In that case , group_desc should be provided as well .
The module will refuse to create a depended - on group without a description .
'''
EXAMPLES = '''
- name : example ec2 group
2014-12-01 20:14:57 +00:00
ec2_group :
2014-09-26 01:01:01 +00:00
name : example
description : an example EC2 group
vpc_id : 12345
2017-04-13 19:11:00 +00:00
region : eu - west - 1
2014-09-26 01:01:01 +00:00
aws_secret_key : SECRET
aws_access_key : ACCESS
rules :
- proto : tcp
from_port : 80
to_port : 80
cidr_ip : 0.0 .0 .0 / 0
- proto : tcp
from_port : 22
to_port : 22
cidr_ip : 10.0 .0 .0 / 8
2015-05-22 10:34:41 +00:00
- proto : tcp
from_port : 443
to_port : 443
group_id : amazon - elb / sg - 87654321 / amazon - elb - sg
- proto : tcp
from_port : 3306
to_port : 3306
group_id : 123412341234 / sg - 87654321 / exact - name - of - sg
2014-09-26 01:01:01 +00:00
- proto : udp
from_port : 10050
to_port : 10050
cidr_ip : 10.0 .0 .0 / 8
- proto : udp
from_port : 10051
to_port : 10051
group_id : sg - 12345678
2015-09-22 13:56:13 +00:00
- proto : icmp
from_port : 8 # icmp type, -1 = any type
to_port : - 1 # icmp subtype, -1 = any subtype
cidr_ip : 10.0 .0 .0 / 8
2014-09-26 01:01:01 +00:00
- proto : all
# the containing group name may be specified here
group_name : example
rules_egress :
- proto : tcp
from_port : 80
to_port : 80
2014-10-23 12:19:23 +00:00
cidr_ip : 0.0 .0 .0 / 0
2014-09-26 01:01:01 +00:00
group_name : example - other
# description to use if example-other needs to be created
group_desc : other example EC2 group
2017-01-29 16:29:54 +00:00
- name : example2 ec2 group
ec2_group :
name : example2
description : an example2 EC2 group
vpc_id : 12345
2017-04-13 19:11:00 +00:00
region : eu - west - 1
2017-01-29 16:29:54 +00:00
rules :
2017-04-04 18:09:28 +00:00
# 'ports' rule keyword was introduced in version 2.4. It accepts a single port value or a list of values including ranges (from_port-to_port).
2017-01-29 16:29:54 +00:00
- proto : tcp
ports : 22
group_name : example - vpn
- proto : tcp
ports :
- 80
- 443
- 8080 - 8099
cidr_ip : 0.0 .0 .0 / 0
2017-04-04 18:09:28 +00:00
# Rule sources list support was added in version 2.4. This allows to define multiple sources per source type as well as multiple source types per rule.
2017-01-29 16:29:54 +00:00
- proto : tcp
ports :
- 6379
- 26379
group_name :
- example - vpn
- example - redis
- proto : tcp
ports : 5665
group_name : example - vpn
cidr_ip :
- 172.16 .1 .0 / 24
- 172.16 .17 .0 / 24
group_id :
- sg - edcd9784
2014-09-26 01:01:01 +00:00
'''
2017-05-12 18:54:25 +00:00
import json
2017-01-29 16:29:54 +00:00
import re
import time
from ansible . module_utils . basic import AnsibleModule
from ansible . module_utils . ec2 import ec2_connect , ec2_argument_spec
2014-09-26 01:01:01 +00:00
try :
import boto . ec2
2015-05-22 10:34:41 +00:00
from boto . ec2 . securitygroup import SecurityGroup
2017-03-26 13:33:29 +00:00
from boto . exception import BotoServerError
2015-04-01 23:16:54 +00:00
HAS_BOTO = True
2014-09-26 01:01:01 +00:00
except ImportError :
2015-04-01 23:16:54 +00:00
HAS_BOTO = False
2014-09-26 01:01:01 +00:00
2017-03-26 13:33:29 +00:00
import traceback
2014-09-26 01:01:01 +00:00
2017-05-12 18:54:25 +00:00
def deduplicate_rules_args ( rules ) :
""" Returns unique rules """
if rules is None :
return None
return list ( dict ( zip ( ( json . dumps ( r , sort_keys = True ) for r in rules ) , rules ) ) . values ( ) )
2014-12-17 11:06:05 +00:00
def make_rule_key ( prefix , rule , group_id , cidr_ip ) :
""" Creates a unique key for an individual group rule """
if isinstance ( rule , dict ) :
2014-12-17 17:01:50 +00:00
proto , from_port , to_port = [ rule . get ( x , None ) for x in ( ' proto ' , ' from_port ' , ' to_port ' ) ]
2017-03-17 20:14:20 +00:00
# fix for 11177
2015-06-05 07:01:59 +00:00
if proto not in [ ' icmp ' , ' tcp ' , ' udp ' ] and from_port == - 1 and to_port == - 1 :
from_port = ' none '
2017-03-17 20:14:20 +00:00
to_port = ' none '
2015-06-05 07:01:59 +00:00
2014-12-17 11:06:05 +00:00
else : # isinstance boto.ec2.securitygroup.IPPermissions
2014-12-17 17:01:50 +00:00
proto , from_port , to_port = [ getattr ( rule , x , None ) for x in ( ' ip_protocol ' , ' from_port ' , ' to_port ' ) ]
2014-12-17 11:06:05 +00:00
key = " %s - %s - %s - %s - %s - %s " % ( prefix , proto , from_port , to_port , group_id , cidr_ip )
return key . lower ( ) . replace ( ' -none ' , ' -None ' )
2017-05-12 18:54:25 +00:00
def addRulesToLookup ( rules , prefix , rules_dict ) :
2014-09-26 01:01:01 +00:00
for rule in rules :
for grant in rule . grants :
2017-05-12 18:54:25 +00:00
rules_dict [ make_rule_key ( prefix , rule , grant . group_id , grant . cidr_ip ) ] = ( rule , grant )
2014-09-26 01:01:01 +00:00
2014-11-14 02:22:51 +00:00
def validate_rule ( module , rule ) :
VALID_PARAMS = ( ' cidr_ip ' ,
' group_id ' , ' group_name ' , ' group_desc ' ,
' proto ' , ' from_port ' , ' to_port ' )
2016-09-02 19:09:58 +00:00
if not isinstance ( rule , dict ) :
module . fail_json ( msg = ' Invalid rule parameter type [ %s ]. ' % type ( rule ) )
2014-11-14 02:22:51 +00:00
for k in rule :
if k not in VALID_PARAMS :
module . fail_json ( msg = ' Invalid rule parameter \' {} \' ' . format ( k ) )
if ' group_id ' in rule and ' cidr_ip ' in rule :
module . fail_json ( msg = ' Specify group_id OR cidr_ip, not both ' )
elif ' group_name ' in rule and ' cidr_ip ' in rule :
module . fail_json ( msg = ' Specify group_name OR cidr_ip, not both ' )
elif ' group_id ' in rule and ' group_name ' in rule :
module . fail_json ( msg = ' Specify group_id OR group_name, not both ' )
2014-09-26 01:01:01 +00:00
def get_target_from_rule ( module , ec2 , rule , name , group , groups , vpc_id ) :
"""
Returns tuple of ( group_id , ip ) after validating rule params .
rule : Dict describing a rule .
name : Name of the security group being managed .
groups : Dict of all available security groups .
AWS accepts an ip range or a security group as target of a rule . This
function validate the rule specification and return either a non - None
group_id or a non - None ip range .
"""
2015-05-22 10:34:41 +00:00
FOREIGN_SECURITY_GROUP_REGEX = ' ^( \ S+)/(sg- \ S+)/( \ S+) '
2014-09-26 01:01:01 +00:00
group_id = None
group_name = None
ip = None
target_group_created = False
if ' group_id ' in rule and ' cidr_ip ' in rule :
module . fail_json ( msg = " Specify group_id OR cidr_ip, not both " )
elif ' group_name ' in rule and ' cidr_ip ' in rule :
module . fail_json ( msg = " Specify group_name OR cidr_ip, not both " )
elif ' group_id ' in rule and ' group_name ' in rule :
module . fail_json ( msg = " Specify group_id OR group_name, not both " )
2015-05-22 10:34:41 +00:00
elif ' group_id ' in rule and re . match ( FOREIGN_SECURITY_GROUP_REGEX , rule [ ' group_id ' ] ) :
# this is a foreign Security Group. Since you can't fetch it you must create an instance of it
owner_id , group_id , group_name = re . match ( FOREIGN_SECURITY_GROUP_REGEX , rule [ ' group_id ' ] ) . groups ( )
group_instance = SecurityGroup ( owner_id = owner_id , name = group_name , id = group_id )
groups [ group_id ] = group_instance
groups [ group_name ] = group_instance
2014-09-26 01:01:01 +00:00
elif ' group_id ' in rule :
group_id = rule [ ' group_id ' ]
elif ' group_name ' in rule :
group_name = rule [ ' group_name ' ]
2015-04-27 18:26:13 +00:00
if group_name == name :
2014-09-26 01:01:01 +00:00
group_id = group . id
groups [ group_id ] = group
groups [ group_name ] = group
2016-07-27 18:11:59 +00:00
elif group_name in groups and ( vpc_id is None or groups [ group_name ] . vpc_id == vpc_id ) :
2015-04-27 18:26:13 +00:00
group_id = groups [ group_name ] . id
2014-09-26 01:01:01 +00:00
else :
if not rule . get ( ' group_desc ' , ' ' ) . strip ( ) :
module . fail_json ( msg = " group %s will be automatically created by rule %s and no description was provided " % ( group_name , rule ) )
if not module . check_mode :
auto_group = ec2 . create_security_group ( group_name , rule [ ' group_desc ' ] , vpc_id = vpc_id )
group_id = auto_group . id
groups [ group_id ] = auto_group
groups [ group_name ] = auto_group
target_group_created = True
elif ' cidr_ip ' in rule :
ip = rule [ ' cidr_ip ' ]
return group_id , ip , target_group_created
2017-01-29 16:29:54 +00:00
def ports_expand ( ports ) :
# takes a list of ports and returns a list of (port_from, port_to)
ports_expanded = [ ]
for port in ports :
if not isinstance ( port , str ) :
ports_expanded . append ( ( port , ) * 2 )
elif ' - ' in port :
2017-04-04 19:22:47 +00:00
ports_expanded . append ( tuple ( p . strip ( ) for p in port . split ( ' - ' , 1 ) ) )
2017-01-29 16:29:54 +00:00
else :
ports_expanded . append ( ( port . strip ( ) , ) * 2 )
return ports_expanded
def rule_expand_ports ( rule ) :
# takes a rule dict and returns a list of expanded rule dicts
if ' ports ' not in rule :
return [ rule ]
ports = rule [ ' ports ' ] if isinstance ( rule [ ' ports ' ] , list ) else [ rule [ ' ports ' ] ]
rule_expanded = [ ]
for from_to in ports_expand ( ports ) :
temp_rule = rule . copy ( )
del temp_rule [ ' ports ' ]
temp_rule [ ' from_port ' ] , temp_rule [ ' to_port ' ] = from_to
rule_expanded . append ( temp_rule )
return rule_expanded
def rules_expand_ports ( rules ) :
# takes a list of rules and expands it based on 'ports'
if not rules :
return rules
return [ rule for rule_complex in rules
for rule in rule_expand_ports ( rule_complex ) ]
def rule_expand_source ( rule , source_type ) :
# takes a rule dict and returns a list of expanded rule dicts for specified source_type
sources = rule [ source_type ] if isinstance ( rule [ source_type ] , list ) else [ rule [ source_type ] ]
source_types_all = ( ' cidr_ip ' , ' group_id ' , ' group_name ' )
rule_expanded = [ ]
for source in sources :
temp_rule = rule . copy ( )
for s in source_types_all :
temp_rule . pop ( s , None )
temp_rule [ source_type ] = source
rule_expanded . append ( temp_rule )
return rule_expanded
def rule_expand_sources ( rule ) :
# takes a rule dict and returns a list of expanded rule discts
source_types = ( stype for stype in ( ' cidr_ip ' , ' group_id ' , ' group_name ' ) if stype in rule )
return [ r for stype in source_types
for r in rule_expand_source ( rule , stype ) ]
def rules_expand_sources ( rules ) :
# takes a list of rules and expands it based on 'cidr_ip', 'group_id', 'group_name'
if not rules :
return rules
return [ rule for rule_complex in rules
for rule in rule_expand_sources ( rule_complex ) ]
2014-09-26 01:01:01 +00:00
def main ( ) :
argument_spec = ec2_argument_spec ( )
argument_spec . update ( dict (
2017-01-29 07:28:53 +00:00
name = dict ( type = ' str ' , required = True ) ,
2017-03-07 19:55:17 +00:00
description = dict ( type = ' str ' , required = False ) ,
2017-01-29 07:28:53 +00:00
vpc_id = dict ( type = ' str ' ) ,
rules = dict ( type = ' list ' ) ,
rules_egress = dict ( type = ' list ' ) ,
2017-03-17 20:14:20 +00:00
state = dict ( default = ' present ' , type = ' str ' , choices = [ ' present ' , ' absent ' ] ) ,
2017-01-29 07:28:53 +00:00
purge_rules = dict ( default = True , required = False , type = ' bool ' ) ,
purge_rules_egress = dict ( default = True , required = False , type = ' bool ' ) ,
)
2014-09-26 01:01:01 +00:00
)
module = AnsibleModule (
argument_spec = argument_spec ,
supports_check_mode = True ,
)
2015-04-01 23:16:54 +00:00
if not HAS_BOTO :
module . fail_json ( msg = ' boto required for this module ' )
2014-09-26 01:01:01 +00:00
name = module . params [ ' name ' ]
description = module . params [ ' description ' ]
vpc_id = module . params [ ' vpc_id ' ]
2017-05-12 18:54:25 +00:00
rules = deduplicate_rules_args ( rules_expand_sources ( rules_expand_ports ( module . params [ ' rules ' ] ) ) )
rules_egress = deduplicate_rules_args ( rules_expand_sources ( rules_expand_ports ( module . params [ ' rules_egress ' ] ) ) )
2014-09-26 01:01:01 +00:00
state = module . params . get ( ' state ' )
purge_rules = module . params [ ' purge_rules ' ]
purge_rules_egress = module . params [ ' purge_rules_egress ' ]
2017-03-07 19:55:17 +00:00
if state == ' present ' and not description :
module . fail_json ( msg = ' Must provide description when state is present. ' )
2014-09-26 01:01:01 +00:00
changed = False
ec2 = ec2_connect ( module )
# find the group if present
group = None
groups = { }
2017-03-26 13:33:29 +00:00
try :
security_groups = ec2 . get_all_security_groups ( )
except BotoServerError as e :
module . fail_json ( msg = " Error in get_all_security_groups: %s " % e . message , exception = traceback . format_exc ( ) )
for curGroup in security_groups :
2014-09-26 01:01:01 +00:00
groups [ curGroup . id ] = curGroup
2015-04-28 09:19:20 +00:00
if curGroup . name in groups :
# Prioritise groups from the current VPC
if vpc_id is None or curGroup . vpc_id == vpc_id :
groups [ curGroup . name ] = curGroup
else :
groups [ curGroup . name ] = curGroup
2014-09-26 01:01:01 +00:00
if curGroup . name == name and ( vpc_id is None or curGroup . vpc_id == vpc_id ) :
group = curGroup
# Ensure requested group is absent
if state == ' absent ' :
if group :
2017-03-26 13:33:29 +00:00
# found a match, delete it
2014-09-26 01:01:01 +00:00
try :
2016-03-30 18:48:20 +00:00
if not module . check_mode :
group . delete ( )
2017-03-26 13:33:29 +00:00
except BotoServerError as e :
module . fail_json ( msg = " Unable to delete security group ' %s ' - %s " % ( group , e . message ) , exception = traceback . format_exc ( ) )
2014-09-26 01:01:01 +00:00
else :
group = None
changed = True
else :
2017-03-26 13:33:29 +00:00
# no match found, no changes required
pass
2014-09-26 01:01:01 +00:00
# Ensure requested group is present
elif state == ' present ' :
if group :
2017-03-26 13:33:29 +00:00
# existing group
2014-09-26 01:01:01 +00:00
if group . description != description :
2017-03-26 13:33:29 +00:00
module . fail_json ( msg = " Group description does not match existing group. ec2_group does not support this case. " )
2014-09-26 01:01:01 +00:00
# if the group doesn't exist, create it now
else :
2017-03-26 13:33:29 +00:00
# no match found, create it
2014-09-26 01:01:01 +00:00
if not module . check_mode :
group = ec2 . create_security_group ( name , description , vpc_id = vpc_id )
# When a group is created, an egress_rule ALLOW ALL
# to 0.0.0.0/0 is added automatically but it's not
# reflected in the object returned by the AWS API
# call. We re-read the group for getting an updated object
# amazon sometimes takes a couple seconds to update the security group so wait till it exists
2017-03-17 20:14:20 +00:00
while len ( ec2 . get_all_security_groups ( filters = { ' group_id ' : group . id } ) ) == 0 :
2014-09-26 01:01:01 +00:00
time . sleep ( 0.1 )
group = ec2 . get_all_security_groups ( group_ids = ( group . id , ) ) [ 0 ]
changed = True
else :
module . fail_json ( msg = " Unsupported state requested: %s " % state )
# create a lookup for all existing rules on the group
if group :
# Manage ingress rules
groupRules = { }
addRulesToLookup ( group . rules , ' in ' , groupRules )
# Now, go through all provided rules and ensure they are there.
2014-11-13 23:03:59 +00:00
if rules is not None :
2014-09-26 01:01:01 +00:00
for rule in rules :
2014-11-14 02:22:51 +00:00
validate_rule ( module , rule )
2014-09-26 01:01:01 +00:00
group_id , ip , target_group_created = get_target_from_rule ( module , ec2 , rule , name , group , groups , vpc_id )
if target_group_created :
changed = True
if rule [ ' proto ' ] in ( ' all ' , ' -1 ' , - 1 ) :
rule [ ' proto ' ] = - 1
rule [ ' from_port ' ] = None
rule [ ' to_port ' ] = None
2014-09-30 00:26:06 +00:00
# Convert ip to list we can iterate over
if not isinstance ( ip , list ) :
ip = [ ip ]
2014-09-26 01:01:01 +00:00
# If rule already exists, don't later delete it
2014-09-30 00:26:06 +00:00
for thisip in ip :
ruleId = make_rule_key ( ' in ' , rule , group_id , thisip )
2017-05-12 18:54:25 +00:00
if ruleId not in groupRules :
2014-09-30 00:26:06 +00:00
grantGroup = None
if group_id :
grantGroup = groups [ group_id ]
if not module . check_mode :
group . authorize ( rule [ ' proto ' ] , rule [ ' from_port ' ] , rule [ ' to_port ' ] , thisip , grantGroup )
changed = True
2017-05-12 18:54:25 +00:00
else :
del groupRules [ ruleId ]
2014-09-26 01:01:01 +00:00
# Finally, remove anything left in the groupRules -- these will be defunct rules
if purge_rules :
2016-12-13 13:28:21 +00:00
for ( rule , grant ) in groupRules . values ( ) :
2015-01-24 05:52:37 +00:00
grantGroup = None
if grant . group_id :
2015-05-22 10:34:41 +00:00
if grant . owner_id != group . owner_id :
# this is a foreign Security Group. Since you can't fetch it you must create an instance of it
group_instance = SecurityGroup ( owner_id = grant . owner_id , name = grant . name , id = grant . group_id )
groups [ grant . group_id ] = group_instance
groups [ grant . name ] = group_instance
2015-01-24 05:52:37 +00:00
grantGroup = groups [ grant . group_id ]
if not module . check_mode :
group . revoke ( rule . ip_protocol , rule . from_port , rule . to_port , grant . cidr_ip , grantGroup )
changed = True
2014-09-26 01:01:01 +00:00
# Manage egress rules
groupRules = { }
addRulesToLookup ( group . rules_egress , ' out ' , groupRules )
# Now, go through all provided rules and ensure they are there.
2014-11-13 23:03:59 +00:00
if rules_egress is not None :
2014-09-26 01:01:01 +00:00
for rule in rules_egress :
2014-11-14 02:22:51 +00:00
validate_rule ( module , rule )
2014-09-26 01:01:01 +00:00
group_id , ip , target_group_created = get_target_from_rule ( module , ec2 , rule , name , group , groups , vpc_id )
if target_group_created :
changed = True
if rule [ ' proto ' ] in ( ' all ' , ' -1 ' , - 1 ) :
rule [ ' proto ' ] = - 1
rule [ ' from_port ' ] = None
rule [ ' to_port ' ] = None
2014-09-30 00:26:06 +00:00
# Convert ip to list we can iterate over
if not isinstance ( ip , list ) :
ip = [ ip ]
2014-09-26 01:01:01 +00:00
# If rule already exists, don't later delete it
2014-09-30 00:26:06 +00:00
for thisip in ip :
ruleId = make_rule_key ( ' out ' , rule , group_id , thisip )
if ruleId in groupRules :
del groupRules [ ruleId ]
# Otherwise, add new rule
else :
grantGroup = None
if group_id :
grantGroup = groups [ group_id ] . id
if not module . check_mode :
ec2 . authorize_security_group_egress (
2017-01-29 07:28:53 +00:00
group_id = group . id ,
ip_protocol = rule [ ' proto ' ] ,
from_port = rule [ ' from_port ' ] ,
to_port = rule [ ' to_port ' ] ,
src_group_id = grantGroup ,
cidr_ip = thisip )
2014-09-30 00:26:06 +00:00
changed = True
2017-04-03 12:16:03 +00:00
else :
# when no egress rules are specified,
2014-09-26 01:01:01 +00:00
# we add in a default allow all out rule, which was the
# default behavior before egress rules were added
default_egress_rule = ' out--1-None-None-None-0.0.0.0/0 '
if default_egress_rule not in groupRules :
2016-08-02 20:01:48 +00:00
if not module . check_mode :
ec2 . authorize_security_group_egress (
group_id = group . id ,
ip_protocol = - 1 ,
from_port = None ,
to_port = None ,
src_group_id = None ,
cidr_ip = ' 0.0.0.0/0 '
)
2014-09-26 01:01:01 +00:00
changed = True
else :
# make sure the default egress rule is not removed
del groupRules [ default_egress_rule ]
# Finally, remove anything left in the groupRules -- these will be defunct rules
if purge_rules_egress :
2016-12-13 13:28:21 +00:00
for ( rule , grant ) in groupRules . values ( ) :
2015-01-24 05:52:37 +00:00
grantGroup = None
if grant . group_id :
grantGroup = groups [ grant . group_id ] . id
if not module . check_mode :
ec2 . revoke_security_group_egress (
2017-01-29 07:28:53 +00:00
group_id = group . id ,
ip_protocol = rule . ip_protocol ,
from_port = rule . from_port ,
to_port = rule . to_port ,
src_group_id = grantGroup ,
cidr_ip = grant . cidr_ip )
2015-01-24 05:52:37 +00:00
changed = True
2014-09-26 01:01:01 +00:00
if group :
module . exit_json ( changed = changed , group_id = group . id )
else :
module . exit_json ( changed = changed , group_id = None )
2016-12-05 17:08:15 +00:00
if __name__ == ' __main__ ' :
main ( )