2013-07-18 19:45:00 +02:00
#!/usr/bin/python
# 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/>.
DOCUMENTATION = '''
---
2013-07-21 17:20:37 +02:00
module: route53
version_added: "1.3"
2013-07-18 23:12:14 +02:00
short_description: add or delete entries in Amazons Route53 DNS service
2013-07-18 19:45:00 +02:00
description:
2013-07-18 23:12:14 +02:00
- Creates and deletes DNS records in Amazons Route53 service
2013-07-18 19:45:00 +02:00
options:
command:
description:
- Specifies the action to take.
required: true
default: null
aliases: []
choices: [ 'get', 'create', 'delete' ]
zone:
description:
- The DNS zone to modify
required: true
default: null
aliases: []
record:
description:
- The full DNS record to create or delete
required: true
default: null
aliases: []
ttl:
description:
- The TTL to give the new record
required: false
default: 3600 (one hour)
aliases: []
type:
description:
- The type of DNS record to create
required: true
default: null
aliases: []
choices: [ 'A', 'CNAME', 'MX', 'AAAA', 'TXT', 'PTR', 'SRV', 'SPF', 'NS' ]
value:
description:
- The new value when creating a DNS record. Multiple comma-spaced values are allowed. When deleting a record all values for the record must be specified or Route53 will not delete it.
required: false
default: null
aliases: []
2013-08-13 15:30:56 +02:00
aws_secret_key:
2013-07-18 19:45:00 +02:00
description:
2013-08-13 15:30:56 +02:00
- AWS secret key.
2013-07-18 19:45:00 +02:00
required: false
default: null
2013-08-13 15:30:56 +02:00
aliases: ['ec2_secret_key', 'secret_key']
aws_access_key:
2013-07-18 19:45:00 +02:00
description:
2013-08-13 15:30:56 +02:00
- AWS access key.
2013-07-18 19:45:00 +02:00
required: false
default: null
2013-08-13 15:30:56 +02:00
aliases: ['ec2_access_key', 'access_key']
2013-08-20 15:58:59 +02:00
overwrite:
description:
- Whether an existing record should be overwritten on create if values do not match
required: false
default: null
aliases: []
2014-07-14 21:07:47 +02:00
retry_interval:
description:
- In the case that route53 is still servicing a prior request, this module will wait and try again after this many seconds. If you have many domain names, the default of 500 seconds may be too long.
required: false
default: 500
aliases: []
2013-07-18 19:45:00 +02:00
requirements: [ "boto" ]
author: Bruce Pennypacker
'''
EXAMPLES = '''
2013-07-18 23:12:14 +02:00
# Add new.foo.com as an A record with 3 IPs
2013-07-18 19:45:00 +02:00
- route53: >
command=create
zone=foo.com
record=new.foo.com
type=A
ttl=7200
value=1.1.1.1,2.2.2.2,3.3.3.3
# Retrieve the details for new.foo.com
- route53: >
command=get
zone=foo.com
record=new.foo.com
type=A
register: rec
2013-07-18 23:12:14 +02:00
# Delete new.foo.com A record using the results from the get command
2013-07-18 19:45:00 +02:00
- route53: >
command=delete
zone=foo.com
2013-08-13 15:30:56 +02:00
record={{ rec.set.record }}
type={{ rec.set.type }}
value={{ rec.set.value }}
2013-07-18 23:12:14 +02:00
# Add an AAAA record. Note that because there are colons in the value
# that the entire parameter list must be quoted:
- route53: >
2014-02-14 17:44:55 +01:00
command=create
2013-07-18 23:12:14 +02:00
zone=foo.com
record=localhost.foo.com
type=AAAA
ttl=7200
2014-02-05 14:59:31 +01:00
value="::1"
2014-02-22 04:33:09 +01:00
# Add a TXT record. Note that TXT and SPF records must be surrounded
# by quotes when sent to Route 53:
- route53: >
command=create
zone=foo.com
record=localhost.foo.com
type=TXT
ttl=7200
value="\"bar\""
2013-07-18 23:12:14 +02:00
'''
2013-07-18 19:45:00 +02:00
import sys
2013-10-09 19:48:07 +02:00
import time
2013-07-18 19:45:00 +02:00
try:
import boto
from boto import route53
from boto.route53.record import ResourceRecordSets
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
2014-07-14 21:07:47 +02:00
def commit(changes, retry_interval):
2013-10-09 19:48:07 +02:00
"""Commit changes, but retry PriorRequestNotComplete errors."""
retry = 10
while True:
try:
retry -= 1
return changes.commit()
except boto.route53.exception.DNSServerError, e:
code = e.body.split("<Code>")[1]
code = code.split("</Code>")[0]
if code != 'PriorRequestNotComplete' or retry < 0:
raise e
2014-07-14 21:07:47 +02:00
time.sleep(retry_interval)
2013-10-09 19:48:07 +02:00
2013-07-18 19:45:00 +02:00
def main():
2014-03-12 01:19:03 +01:00
argument_spec = ec2_argument_spec()
2014-02-09 00:35:26 +01:00
argument_spec.update(dict(
2013-07-18 19:45:00 +02:00
command = dict(choices=['get', 'create', 'delete'], required=True),
zone = dict(required=True),
record = dict(required=True),
ttl = dict(required=False, default=3600),
type = dict(choices=['A', 'CNAME', 'MX', 'AAAA', 'TXT', 'PTR', 'SRV', 'SPF', 'NS'], required=True),
value = dict(required=False),
2014-07-14 21:07:47 +02:00
overwrite = dict(required=False, type='bool'),
retry_interval = dict(required=False, default=500)
2013-07-18 19:45:00 +02:00
)
)
2014-02-09 00:35:26 +01:00
module = AnsibleModule(argument_spec=argument_spec)
2013-07-18 19:45:00 +02:00
command_in = module.params.get('command')
2013-07-21 17:55:38 +02:00
zone_in = module.params.get('zone')
ttl_in = module.params.get('ttl')
record_in = module.params.get('record')
type_in = module.params.get('type')
value_in = module.params.get('value')
2014-07-14 21:07:47 +02:00
retry_interval_in = module.params.get('retry_interval')
2013-11-12 20:16:23 +01:00
ec2_url, aws_access_key, aws_secret_key, region = get_ec2_creds(module)
2013-07-18 19:45:00 +02:00
value_list = ()
if type(value_in) is str:
if value_in:
value_list = sorted(value_in.split(','))
elif type(value_in) is list:
value_list = sorted(value_in)
if zone_in[-1:] != '.':
zone_in += "."
if record_in[-1:] != '.':
record_in += "."
if command_in == 'create' or command_in == 'delete':
if not value_in:
module.fail_json(msg = "parameter 'value' required for create/delete")
# connect to the route53 endpoint
try:
2013-08-13 15:30:56 +02:00
conn = boto.route53.connection.Route53Connection(aws_access_key, aws_secret_key)
2013-07-18 19:45:00 +02:00
except boto.exception.BotoServerError, e:
module.fail_json(msg = e.error_message)
# Get all the existing hosted zones and save their ID's
zones = {}
results = conn.get_all_hosted_zones()
for r53zone in results['ListHostedZonesResponse']['HostedZones']:
zone_id = r53zone['Id'].replace('/hostedzone/', '')
zones[r53zone['Name']] = zone_id
# Verify that the requested zone is already defined in Route53
if not zone_in in zones:
errmsg = "Zone %s does not exist in Route53" % zone_in
module.fail_json(msg = errmsg)
record = {}
found_record = False
sets = conn.get_all_rrsets(zones[zone_in])
for rset in sets:
2014-03-07 01:10:56 +01:00
# Due to a bug in either AWS or Boto, "special" characters are returned as octals, preventing round
# tripping of things like * and @.
decoded_name = rset.name.replace(r'\052', '*')
2014-05-08 20:20:32 +02:00
decoded_name = decoded_name.replace(r'\100', '@')
2014-03-07 01:10:56 +01:00
if rset.type == type_in and decoded_name == record_in:
2013-07-18 19:45:00 +02:00
found_record = True
record['zone'] = zone_in
record['type'] = rset.type
2014-03-07 01:10:56 +01:00
record['record'] = decoded_name
2013-07-18 19:45:00 +02:00
record['ttl'] = rset.ttl
record['value'] = ','.join(sorted(rset.resource_records))
record['values'] = sorted(rset.resource_records)
2014-01-15 00:51:59 +01:00
if value_list == sorted(rset.resource_records) and record['ttl'] == ttl_in and command_in == 'create':
2013-07-18 19:45:00 +02:00
module.exit_json(changed=False)
if command_in == 'get':
module.exit_json(changed=False, set=record)
if command_in == 'delete' and not found_record:
module.exit_json(changed=False)
changes = ResourceRecordSets(conn, zones[zone_in])
2013-08-20 15:58:59 +02:00
if command_in == 'create' and found_record:
if not module.params['overwrite']:
module.fail_json(msg = "Record already exists with different value. Set 'overwrite' to replace it")
else:
2014-01-15 00:51:59 +01:00
change = changes.add_change("DELETE", record_in, type_in, record['ttl'])
2013-08-20 15:58:59 +02:00
for v in record['values']:
change.add_value(v)
2013-07-18 19:45:00 +02:00
if command_in == 'create' or command_in == 'delete':
change = changes.add_change(command_in.upper(), record_in, type_in, ttl_in)
for v in value_list:
change.add_value(v)
try:
2014-07-14 21:07:47 +02:00
result = commit(changes, retry_interval_in)
2013-07-18 19:45:00 +02:00
except boto.route53.exception.DNSServerError, e:
txt = e.body.split("<Message>")[1]
txt = txt.split("</Message>")[0]
module.fail_json(msg = txt)
module.exit_json(changed=True)
2013-11-12 20:16:23 +01:00
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.ec2 import *
2013-07-18 19:45:00 +02:00
main()