2013-07-01 11:07:06 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: ec2_group
|
2013-11-18 23:55:49 +00:00
|
|
|
version_added: "1.3"
|
2013-07-01 11:07:06 +00:00
|
|
|
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:
|
|
|
|
- Description of the security group.
|
|
|
|
required: true
|
|
|
|
vpc_id:
|
|
|
|
description:
|
|
|
|
- ID of the VPC to create the group in.
|
2013-08-19 16:35:43 +00:00
|
|
|
required: false
|
2013-07-01 11:07:06 +00:00
|
|
|
rules:
|
|
|
|
description:
|
|
|
|
- List of firewall rules to enforce in this group (see example).
|
|
|
|
required: true
|
|
|
|
region:
|
|
|
|
description:
|
|
|
|
- the EC2 region to use
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
ec2_url:
|
|
|
|
description:
|
|
|
|
- Url to use to connect to EC2 or your Eucalyptus cloud (by default the module will use EC2 endpoints)
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
ec2_secret_key:
|
|
|
|
description:
|
|
|
|
- EC2 secret key
|
|
|
|
required: false
|
|
|
|
default: null
|
2013-10-17 15:11:59 +00:00
|
|
|
aliases: ['aws_secret_key']
|
2013-07-01 11:07:06 +00:00
|
|
|
ec2_access_key:
|
|
|
|
description:
|
|
|
|
- EC2 access key
|
|
|
|
required: false
|
|
|
|
default: null
|
2013-10-17 15:11:59 +00:00
|
|
|
aliases: ['aws_access_key']
|
2013-10-02 15:09:23 +00:00
|
|
|
state:
|
|
|
|
version_added: "1.4"
|
|
|
|
description:
|
|
|
|
- create or delete security group
|
|
|
|
required: false
|
|
|
|
default: 'present'
|
|
|
|
aliases: []
|
|
|
|
|
2013-07-01 11:07:06 +00:00
|
|
|
requirements: [ "boto" ]
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: example ec2 group
|
|
|
|
local_action:
|
|
|
|
module: ec2_group
|
|
|
|
name: example
|
|
|
|
description: an example EC2 group
|
|
|
|
vpc_id: 12345
|
|
|
|
region: eu-west-1a
|
|
|
|
ec2_secret_key: SECRET
|
|
|
|
ec2_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
|
|
|
|
- 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: abcdef
|
|
|
|
'''
|
|
|
|
|
|
|
|
try:
|
|
|
|
import boto.ec2
|
|
|
|
except ImportError:
|
|
|
|
print "failed=True msg='boto required for this module'"
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def addRulesToLookup(rules, prefix, dict):
|
|
|
|
for rule in rules:
|
|
|
|
for grant in rule.grants:
|
|
|
|
dict["%s-%s-%s-%s-%s-%s" % (prefix, rule.ip_protocol, rule.from_port, rule.to_port,
|
|
|
|
grant.group_id, grant.cidr_ip)] = rule
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
name=dict(required=True),
|
|
|
|
description=dict(required=True),
|
|
|
|
vpc_id=dict(),
|
|
|
|
rules=dict(),
|
|
|
|
ec2_url=dict(aliases=['EC2_URL']),
|
2013-10-17 15:11:59 +00:00
|
|
|
ec2_secret_key=dict(aliases=['EC2_SECRET_KEY', 'aws_secret_key'], no_log=True),
|
|
|
|
ec2_access_key=dict(aliases=['EC2_ACCESS_KEY', 'aws_access_key']),
|
2013-12-17 02:04:12 +00:00
|
|
|
region=dict(choices=AWS_REGIONS),
|
2013-10-02 15:09:23 +00:00
|
|
|
state = dict(default='present', choices=['present', 'absent']),
|
2013-07-01 11:07:06 +00:00
|
|
|
),
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
2013-11-01 15:59:24 +00:00
|
|
|
|
2013-07-01 11:07:06 +00:00
|
|
|
name = module.params['name']
|
|
|
|
description = module.params['description']
|
|
|
|
vpc_id = module.params['vpc_id']
|
|
|
|
rules = module.params['rules']
|
2013-10-02 15:09:23 +00:00
|
|
|
state = module.params.get('state')
|
2013-07-01 11:07:06 +00:00
|
|
|
|
|
|
|
changed = False
|
|
|
|
|
2013-12-17 02:04:12 +00:00
|
|
|
ec2 = ec2_connect(module)
|
2013-07-01 11:07:06 +00:00
|
|
|
|
|
|
|
# find the group if present
|
|
|
|
group = None
|
|
|
|
groups = {}
|
|
|
|
for curGroup in ec2.get_all_security_groups():
|
|
|
|
groups[curGroup.id] = curGroup
|
|
|
|
|
|
|
|
if curGroup.name == name and curGroup.vpc_id == vpc_id:
|
|
|
|
group = curGroup
|
|
|
|
|
2013-10-02 15:09:23 +00:00
|
|
|
# Ensure requested group is absent
|
|
|
|
if state == 'absent':
|
|
|
|
if group:
|
|
|
|
'''found a match, delete it'''
|
|
|
|
try:
|
|
|
|
group.delete()
|
|
|
|
except Exception, e:
|
|
|
|
module.fail_json(msg="Unable to delete security group '%s' - %s" % (group, e))
|
|
|
|
else:
|
|
|
|
group = None
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
'''no match found, no changes required'''
|
|
|
|
|
|
|
|
# Ensure requested group is present
|
|
|
|
elif state == 'present':
|
|
|
|
if group:
|
|
|
|
'''existing group found'''
|
|
|
|
# check the group parameters are correct
|
|
|
|
group_in_use = False
|
|
|
|
rs = ec2.get_all_instances()
|
|
|
|
for r in rs:
|
|
|
|
for i in r.instances:
|
|
|
|
group_in_use |= reduce(lambda x, y: x | (y.name == 'public-ssh'), i.groups, False)
|
|
|
|
|
|
|
|
if group.description != description:
|
|
|
|
if group_in_use:
|
|
|
|
module.fail_json(msg="Group description does not match, but it is in use so cannot be changed.")
|
|
|
|
|
|
|
|
# if the group doesn't exist, create it now
|
|
|
|
else:
|
|
|
|
'''no match found, create it'''
|
|
|
|
if not module.check_mode:
|
|
|
|
group = ec2.create_security_group(name, description, vpc_id=vpc_id)
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Unsupported state requested: %s" % state)
|
2013-07-01 11:07:06 +00:00
|
|
|
|
|
|
|
# create a lookup for all existing rules on the group
|
|
|
|
if group:
|
2013-10-02 15:09:23 +00:00
|
|
|
groupRules = {}
|
2013-07-01 11:07:06 +00:00
|
|
|
addRulesToLookup(group.rules, 'in', groupRules)
|
|
|
|
|
2013-10-02 15:09:23 +00:00
|
|
|
# Now, go through all provided rules and ensure they are there.
|
|
|
|
if rules:
|
|
|
|
for rule in rules:
|
|
|
|
group_id = None
|
|
|
|
ip = None
|
|
|
|
if 'group_id' in rule and 'cidr_ip' in rule:
|
|
|
|
module.fail_json(msg="Specify group_id OR cidr_ip, not both")
|
|
|
|
elif 'group_id' in rule:
|
|
|
|
group_id = rule['group_id']
|
|
|
|
elif 'cidr_ip' in rule:
|
|
|
|
ip = rule['cidr_ip']
|
2013-07-01 11:07:06 +00:00
|
|
|
|
2013-10-02 15:09:23 +00:00
|
|
|
if rule['proto'] == 'all':
|
|
|
|
rule['proto'] = -1
|
|
|
|
rule['from_port'] = None
|
|
|
|
rule['to_port'] = None
|
2013-07-01 11:07:06 +00:00
|
|
|
|
2013-10-02 15:09:23 +00:00
|
|
|
# If rule already exists, don't later delete it
|
|
|
|
ruleId = "%s-%s-%s-%s-%s-%s" % ('in', rule['proto'], rule['from_port'], rule['to_port'], group_id, ip)
|
|
|
|
if ruleId in groupRules:
|
|
|
|
del groupRules[ruleId]
|
|
|
|
# Otherwise, add new rule
|
|
|
|
else:
|
|
|
|
grantGroup = None
|
|
|
|
if group_id:
|
|
|
|
grantGroup = groups[group_id]
|
|
|
|
|
|
|
|
if not module.check_mode:
|
|
|
|
group.authorize(rule['proto'], rule['from_port'], rule['to_port'], ip, grantGroup)
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
# Finally, remove anything left in the groupRules -- these will be defunct rules
|
|
|
|
for rule in groupRules.itervalues():
|
|
|
|
for grant in rule.grants:
|
|
|
|
grantGroup = None
|
|
|
|
if grant.group_id:
|
|
|
|
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
|
2013-07-01 11:07:06 +00:00
|
|
|
|
2013-10-02 15:09:23 +00:00
|
|
|
if group:
|
|
|
|
module.exit_json(changed=changed, group_id=group.id)
|
|
|
|
else:
|
2013-07-01 11:07:06 +00:00
|
|
|
module.exit_json(changed=changed, group_id=None)
|
|
|
|
|
2013-11-01 15:59:24 +00:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
from ansible.module_utils.ec2 import *
|
|
|
|
|
2013-07-01 11:07:06 +00:00
|
|
|
main()
|