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:
|
2015-07-28 20:00:37 +02:00
|
|
|
- Specifies the action to take.
|
2013-07-18 19:45:00 +02:00
|
|
|
required: true
|
|
|
|
choices: [ 'get', 'create', 'delete' ]
|
|
|
|
zone:
|
|
|
|
description:
|
|
|
|
- The DNS zone to modify
|
|
|
|
required: true
|
2015-04-15 12:43:00 +02:00
|
|
|
hosted_zone_id:
|
|
|
|
description:
|
|
|
|
- The Hosted Zone ID of the DNS zone to modify
|
|
|
|
required: false
|
2015-07-28 20:00:37 +02:00
|
|
|
version_added: "2.0"
|
2015-04-15 12:43:00 +02:00
|
|
|
default: null
|
2013-07-18 19:45:00 +02:00
|
|
|
record:
|
|
|
|
description:
|
|
|
|
- The full DNS record to create or delete
|
|
|
|
required: true
|
|
|
|
ttl:
|
|
|
|
description:
|
|
|
|
- The TTL to give the new record
|
|
|
|
required: false
|
|
|
|
default: 3600 (one hour)
|
|
|
|
type:
|
|
|
|
description:
|
|
|
|
- The type of DNS record to create
|
|
|
|
required: true
|
2016-02-17 05:29:53 +01:00
|
|
|
choices: [ 'A', 'CNAME', 'MX', 'AAAA', 'TXT', 'PTR', 'SRV', 'SPF', 'NS', 'SOA' ]
|
2015-02-07 00:19:11 +01:00
|
|
|
alias:
|
|
|
|
description:
|
|
|
|
- Indicates if this is an alias record.
|
|
|
|
required: false
|
2015-07-28 20:00:37 +02:00
|
|
|
version_added: "1.9"
|
2015-02-07 00:19:11 +01:00
|
|
|
default: False
|
2015-03-27 21:20:20 +01:00
|
|
|
choices: [ 'True', 'False' ]
|
2015-02-07 00:19:11 +01:00
|
|
|
alias_hosted_zone_id:
|
|
|
|
description:
|
|
|
|
- The hosted zone identifier.
|
|
|
|
required: false
|
2015-07-28 20:00:37 +02:00
|
|
|
version_added: "1.9"
|
2015-02-07 00:19:11 +01:00
|
|
|
default: null
|
2015-11-18 19:08:21 +01:00
|
|
|
alias_evaluate_target_health:
|
|
|
|
description:
|
|
|
|
- Whether or not to evaluate an alias target health. Useful for aliases to Elastic Load Balancers.
|
|
|
|
required: false
|
2016-01-12 00:16:07 +01:00
|
|
|
version_added: "2.1"
|
2015-11-18 19:08:21 +01:00
|
|
|
default: false
|
2013-07-18 19:45:00 +02:00
|
|
|
value:
|
|
|
|
description:
|
2015-02-07 00:19:11 +01:00
|
|
|
- The new value when creating a DNS record. Multiple comma-spaced values are allowed for non-alias records. When deleting a record all values for the record must be specified or Route53 will not delete it.
|
2013-07-18 19:45:00 +02:00
|
|
|
required: false
|
|
|
|
default: null
|
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
|
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
|
2015-02-27 04:35:10 +01:00
|
|
|
private_zone:
|
|
|
|
description:
|
|
|
|
- If set to true, the private zone matching the requested name within the domain will be used if there are both public and private zones. The default is to use the public zone.
|
|
|
|
required: false
|
|
|
|
default: false
|
|
|
|
version_added: "1.9"
|
2015-05-05 17:07:18 +02:00
|
|
|
identifier:
|
|
|
|
description:
|
|
|
|
- Weighted and latency-based resource record sets only. An identifier
|
|
|
|
that differentiates among multiple resource record sets that have the
|
|
|
|
same combination of DNS name and type.
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
version_added: "2.0"
|
|
|
|
weight:
|
|
|
|
description:
|
|
|
|
- Weighted resource record sets only. Among resource record sets that
|
|
|
|
have the same combination of DNS name and type, a value that
|
|
|
|
determines what portion of traffic for the current resource record set
|
|
|
|
is routed to the associated location.
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
version_added: "2.0"
|
|
|
|
region:
|
|
|
|
description:
|
|
|
|
- Latency-based resource record sets only Among resource record sets
|
|
|
|
that have the same combination of DNS name and type, a value that
|
|
|
|
determines which region this should be associated with for the
|
|
|
|
latency-based routing
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
version_added: "2.0"
|
|
|
|
health_check:
|
|
|
|
description:
|
|
|
|
- Health check to associate with this record
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
version_added: "2.0"
|
|
|
|
failover:
|
|
|
|
description:
|
|
|
|
- Failover resource record sets only. Whether this is the primary or
|
|
|
|
secondary resource record set.
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
version_added: "2.0"
|
2015-05-20 21:39:17 +02:00
|
|
|
vpc_id:
|
|
|
|
description:
|
2015-07-14 22:54:49 +02:00
|
|
|
- "When used in conjunction with private_zone: true, this will only modify records in the private hosted zone attached to this VPC."
|
|
|
|
- This allows you to have multiple private hosted zones, all with the same name, attached to different VPCs.
|
2015-05-20 21:39:17 +02:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
version_added: "2.0"
|
2016-01-11 15:45:50 +01:00
|
|
|
wait:
|
|
|
|
description:
|
|
|
|
- Wait until the changes have been replicated to all Amazon Route 53 DNS servers.
|
|
|
|
required: false
|
|
|
|
default: no
|
2016-01-12 15:33:58 +01:00
|
|
|
version_added: "2.1"
|
2016-01-11 16:43:26 +01:00
|
|
|
wait_timeout:
|
|
|
|
description:
|
|
|
|
- How long to wait for the changes to be replicated, in seconds.
|
|
|
|
required: false
|
|
|
|
default: 300
|
2016-01-12 15:33:58 +01:00
|
|
|
version_added: "2.1"
|
2016-01-28 18:24:02 +01:00
|
|
|
author:
|
2015-11-18 19:08:21 +01:00
|
|
|
- "Bruce Pennypacker (@bpennypacker)"
|
|
|
|
- "Mike Buzzetti <mike.buzzetti@gmail.com>"
|
2014-12-25 01:04:25 +01:00
|
|
|
extends_documentation_fragment: aws
|
2013-07-18 19:45:00 +02:00
|
|
|
'''
|
|
|
|
|
2014-12-01 21:14:57 +01:00
|
|
|
# FIXME: the command stuff should have a more state like configuration alias -- MPD
|
|
|
|
|
2013-07-18 19:45:00 +02:00
|
|
|
EXAMPLES = '''
|
2016-01-11 15:45:50 +01:00
|
|
|
# Add new.foo.com as an A record with 3 IPs and wait until the changes have been replicated
|
2014-12-01 21:14:57 +01: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
|
2016-01-11 15:45:50 +01:00
|
|
|
wait: yes
|
2013-07-18 19:45:00 +02:00
|
|
|
|
|
|
|
# Retrieve the details for new.foo.com
|
2014-12-01 21:14:57 +01:00
|
|
|
- route53:
|
|
|
|
command: get
|
|
|
|
zone: foo.com
|
|
|
|
record: new.foo.com
|
|
|
|
type: A
|
2013-07-18 19:45:00 +02:00
|
|
|
register: rec
|
|
|
|
|
2013-07-18 23:12:14 +02:00
|
|
|
# Delete new.foo.com A record using the results from the get command
|
2014-12-01 21:14:57 +01:00
|
|
|
- route53:
|
|
|
|
command: delete
|
|
|
|
zone: foo.com
|
|
|
|
record: "{{ rec.set.record }}"
|
2015-01-20 00:43:22 +01:00
|
|
|
ttl: "{{ rec.set.ttl }}"
|
2014-12-01 21:14:57 +01:00
|
|
|
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:
|
2014-12-01 21:14:57 +01:00
|
|
|
- route53:
|
|
|
|
command: "create"
|
|
|
|
zone: "foo.com"
|
|
|
|
record: "localhost.foo.com"
|
|
|
|
type: "AAAA"
|
|
|
|
ttl: "7200"
|
|
|
|
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:
|
2014-12-01 21:14:57 +01:00
|
|
|
- route53:
|
|
|
|
command: "create"
|
|
|
|
zone: "foo.com"
|
|
|
|
record: "localhost.foo.com"
|
|
|
|
type: "TXT"
|
|
|
|
ttl: "7200"
|
|
|
|
value: '"bar"'
|
2014-02-22 04:33:09 +01:00
|
|
|
|
2015-02-07 00:19:11 +01:00
|
|
|
# Add an alias record that points to an Amazon ELB:
|
|
|
|
- route53:
|
|
|
|
command=create
|
|
|
|
zone=foo.com
|
|
|
|
record=elb.foo.com
|
|
|
|
type=A
|
|
|
|
value="{{ elb_dns_name }}"
|
2015-03-27 21:20:20 +01:00
|
|
|
alias=True
|
2015-02-07 00:19:11 +01:00
|
|
|
alias_hosted_zone_id="{{ elb_zone_id }}"
|
|
|
|
|
2015-11-18 19:08:21 +01:00
|
|
|
# Add an alias record that points to an Amazon ELB and evaluates it health:
|
|
|
|
- route53:
|
|
|
|
command=create
|
|
|
|
zone=foo.com
|
|
|
|
record=elb.foo.com
|
|
|
|
type=A
|
|
|
|
value="{{ elb_dns_name }}"
|
|
|
|
alias=True
|
|
|
|
alias_hosted_zone_id="{{ elb_zone_id }}"
|
|
|
|
alias_evaluate_target_health=True
|
|
|
|
|
2015-04-15 12:43:00 +02:00
|
|
|
# Add an AAAA record with Hosted Zone ID. Note that because there are colons in the value
|
|
|
|
# that the entire parameter list must be quoted:
|
|
|
|
- route53:
|
|
|
|
command: "create"
|
|
|
|
zone: "foo.com"
|
2015-07-22 11:36:32 +02:00
|
|
|
hosted_zone_id: "Z2AABBCCDDEEFF"
|
2015-04-15 12:43:00 +02:00
|
|
|
record: "localhost.foo.com"
|
|
|
|
type: "AAAA"
|
|
|
|
ttl: "7200"
|
|
|
|
value: "::1"
|
2016-01-11 15:45:50 +01:00
|
|
|
|
2015-04-15 12:43:00 +02:00
|
|
|
# Add an AAAA record with Hosted Zone ID. Note that because there are colons in the value
|
|
|
|
# that the entire parameter list must be quoted:
|
|
|
|
- route53:
|
|
|
|
command: "create"
|
|
|
|
zone: "foo.com"
|
2015-07-22 11:36:32 +02:00
|
|
|
hosted_zone_id: "Z2AABBCCDDEEFF"
|
2015-04-15 12:43:00 +02:00
|
|
|
record: "localhost.foo.com"
|
|
|
|
type: "AAAA"
|
|
|
|
ttl: "7200"
|
|
|
|
value: "::1"
|
2016-01-11 15:45:50 +01:00
|
|
|
|
2015-05-05 17:07:18 +02:00
|
|
|
# Use a routing policy to distribute traffic:
|
|
|
|
- route53:
|
|
|
|
command: "create"
|
|
|
|
zone: "foo.com"
|
|
|
|
record: "www.foo.com"
|
|
|
|
type: "CNAME"
|
|
|
|
value: "host1.foo.com"
|
|
|
|
ttl: 30
|
|
|
|
# Routing policy
|
|
|
|
identifier: "host1@www"
|
|
|
|
weight: 100
|
|
|
|
health_check: "d994b780-3150-49fd-9205-356abdd42e75"
|
2014-02-22 04:33:09 +01:00
|
|
|
|
2013-07-18 23:12:14 +02:00
|
|
|
'''
|
2013-07-18 19:45:00 +02:00
|
|
|
|
2016-03-01 18:49:10 +01:00
|
|
|
MINIMUM_BOTO_VERSION = '2.28.0'
|
2016-01-11 15:45:50 +01:00
|
|
|
WAIT_RETRY_SLEEP = 5 # how many seconds to wait between propagation status polls
|
|
|
|
|
2016-01-11 16:43:26 +01:00
|
|
|
|
2013-10-09 19:48:07 +02:00
|
|
|
import time
|
2016-03-01 18:49:10 +01:00
|
|
|
import distutils.version
|
2013-07-18 19:45:00 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
import boto
|
2014-10-31 15:29:59 +01:00
|
|
|
import boto.ec2
|
2013-07-18 19:45:00 +02:00
|
|
|
from boto import route53
|
2014-12-25 01:04:25 +01:00
|
|
|
from boto.route53 import Route53Connection
|
2015-05-05 17:07:18 +02:00
|
|
|
from boto.route53.record import Record, ResourceRecordSets
|
2016-01-11 15:45:50 +01:00
|
|
|
from boto.route53.status import Status
|
2015-04-02 01:16:54 +02:00
|
|
|
HAS_BOTO = True
|
2013-07-18 19:45:00 +02:00
|
|
|
except ImportError:
|
2015-04-02 01:16:54 +02:00
|
|
|
HAS_BOTO = False
|
|
|
|
|
2016-01-11 16:43:26 +01:00
|
|
|
|
|
|
|
class TimeoutError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-05-20 21:39:17 +02:00
|
|
|
def get_zone_by_name(conn, module, zone_name, want_private, zone_id, want_vpc_id):
|
2015-07-06 12:02:24 +02:00
|
|
|
"""Finds a zone by name or zone_id"""
|
2015-05-05 17:07:18 +02:00
|
|
|
for zone in conn.get_zones():
|
|
|
|
# only save this zone id if the private status of the zone matches
|
|
|
|
# the private_zone_in boolean specified in the params
|
|
|
|
private_zone = module.boolean(zone.config.get('PrivateZone', False))
|
2015-07-06 12:02:24 +02:00
|
|
|
if private_zone == want_private and ((zone.name == zone_name and zone_id == None) or zone.id.replace('/hostedzone/', '') == zone_id):
|
2015-05-20 21:39:17 +02:00
|
|
|
if want_vpc_id:
|
|
|
|
# NOTE: These details aren't available in other boto methods, hence the necessary
|
|
|
|
# extra API call
|
|
|
|
zone_details = conn.get_hosted_zone(zone.id)['GetHostedZoneResponse']
|
|
|
|
# this is to deal with this boto bug: https://github.com/boto/boto/pull/2882
|
|
|
|
if isinstance(zone_details['VPCs'], dict):
|
|
|
|
if zone_details['VPCs']['VPC']['VPCId'] == want_vpc_id:
|
|
|
|
return zone
|
|
|
|
else: # Forward compatibility for when boto fixes that bug
|
|
|
|
if want_vpc_id in [v['VPCId'] for v in zone_details['VPCs']]:
|
|
|
|
return zone
|
|
|
|
else:
|
|
|
|
return zone
|
2015-05-05 17:07:18 +02:00
|
|
|
return None
|
|
|
|
|
2013-07-18 19:45:00 +02:00
|
|
|
|
2016-01-11 16:43:26 +01:00
|
|
|
def commit(changes, retry_interval, wait, wait_timeout):
|
2013-10-09 19:48:07 +02:00
|
|
|
"""Commit changes, but retry PriorRequestNotComplete errors."""
|
2016-01-11 15:45:50 +01:00
|
|
|
result = None
|
2013-10-09 19:48:07 +02:00
|
|
|
retry = 10
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
retry -= 1
|
2016-01-11 15:45:50 +01:00
|
|
|
result = changes.commit()
|
|
|
|
break
|
2013-10-09 19:48:07 +02:00
|
|
|
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-12-01 21:21:52 +01:00
|
|
|
time.sleep(float(retry_interval))
|
2013-10-09 19:48:07 +02:00
|
|
|
|
2016-01-11 15:45:50 +01:00
|
|
|
if wait:
|
2016-01-11 16:43:26 +01:00
|
|
|
timeout_time = time.time() + wait_timeout
|
2016-01-11 15:45:50 +01:00
|
|
|
connection = changes.connection
|
|
|
|
change = result['ChangeResourceRecordSetsResponse']['ChangeInfo']
|
|
|
|
status = Status(connection, change)
|
2016-01-11 16:43:26 +01:00
|
|
|
while status.status != 'INSYNC' and time.time() < timeout_time:
|
2016-01-11 15:45:50 +01:00
|
|
|
time.sleep(WAIT_RETRY_SLEEP)
|
|
|
|
status.update()
|
2016-01-11 16:43:26 +01:00
|
|
|
if time.time() >= timeout_time:
|
|
|
|
raise TimeoutError()
|
2016-01-11 15:45:50 +01:00
|
|
|
return result
|
|
|
|
|
2016-02-05 08:57:55 +01:00
|
|
|
# Shamelessly copied over from https://git.io/vgmDG
|
|
|
|
IGNORE_CODE = 'Throttling'
|
|
|
|
MAX_RETRIES=5
|
|
|
|
def invoke_with_throttling_retries(function_ref, *argv):
|
|
|
|
retries=0
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
retval=function_ref(*argv)
|
|
|
|
return retval
|
|
|
|
except boto.exception.BotoServerError, e:
|
|
|
|
if e.code != IGNORE_CODE or retries==MAX_RETRIES:
|
|
|
|
raise e
|
|
|
|
time.sleep(5 * (2**retries))
|
|
|
|
retries += 1
|
|
|
|
|
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(
|
2015-11-18 19:08:21 +01:00
|
|
|
command = dict(choices=['get', 'create', 'delete'], required=True),
|
|
|
|
zone = dict(required=True),
|
|
|
|
hosted_zone_id = dict(required=False, default=None),
|
|
|
|
record = dict(required=True),
|
|
|
|
ttl = dict(required=False, type='int', default=3600),
|
2016-02-17 05:29:53 +01:00
|
|
|
type = dict(choices=['A', 'CNAME', 'MX', 'AAAA', 'TXT', 'PTR', 'SRV', 'SPF', 'NS', 'SOA'], required=True),
|
2015-11-18 19:08:21 +01:00
|
|
|
alias = dict(required=False, type='bool'),
|
|
|
|
alias_hosted_zone_id = dict(required=False),
|
|
|
|
alias_evaluate_target_health = dict(required=False, type='bool', default=False),
|
|
|
|
value = dict(required=False),
|
|
|
|
overwrite = dict(required=False, type='bool'),
|
|
|
|
retry_interval = dict(required=False, default=500),
|
|
|
|
private_zone = dict(required=False, type='bool', default=False),
|
|
|
|
identifier = dict(required=False, default=None),
|
|
|
|
weight = dict(required=False, type='int'),
|
|
|
|
region = dict(required=False),
|
|
|
|
health_check = dict(required=False),
|
2016-01-11 16:43:26 +01:00
|
|
|
failover = dict(required=False),
|
|
|
|
vpc_id = dict(required=False),
|
|
|
|
wait = dict(required=False, type='bool', default=False),
|
|
|
|
wait_timeout = dict(required=False, type='int', default=300),
|
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
|
|
|
|
2015-04-02 01:16:54 +02:00
|
|
|
if not HAS_BOTO:
|
|
|
|
module.fail_json(msg='boto required for this module')
|
|
|
|
|
2016-03-01 18:49:10 +01:00
|
|
|
if distutils.version.StrictVersion(boto.__version__) < distutils.version.StrictVersion(MINIMUM_BOTO_VERSION):
|
|
|
|
module.fail_json(msg='Found boto in version %s, but >= %s is required' % (boto.__version__, MINIMUM_BOTO_VERSION))
|
|
|
|
|
2015-11-18 19:08:21 +01:00
|
|
|
command_in = module.params.get('command')
|
|
|
|
zone_in = module.params.get('zone').lower()
|
|
|
|
hosted_zone_id_in = module.params.get('hosted_zone_id')
|
|
|
|
ttl_in = module.params.get('ttl')
|
|
|
|
record_in = module.params.get('record').lower()
|
|
|
|
type_in = module.params.get('type')
|
|
|
|
value_in = module.params.get('value')
|
|
|
|
alias_in = module.params.get('alias')
|
|
|
|
alias_hosted_zone_id_in = module.params.get('alias_hosted_zone_id')
|
|
|
|
alias_evaluate_target_health_in = module.params.get('alias_evaluate_target_health')
|
|
|
|
retry_interval_in = module.params.get('retry_interval')
|
|
|
|
private_zone_in = module.params.get('private_zone')
|
|
|
|
identifier_in = module.params.get('identifier')
|
|
|
|
weight_in = module.params.get('weight')
|
|
|
|
region_in = module.params.get('region')
|
|
|
|
health_check_in = module.params.get('health_check')
|
|
|
|
failover_in = module.params.get('failover')
|
|
|
|
vpc_id_in = module.params.get('vpc_id')
|
2016-01-11 15:45:50 +01:00
|
|
|
wait_in = module.params.get('wait')
|
2016-01-11 16:43:26 +01:00
|
|
|
wait_timeout_in = module.params.get('wait_timeout')
|
2013-11-12 20:16:23 +01:00
|
|
|
|
2014-12-25 01:04:25 +01:00
|
|
|
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module)
|
2013-07-18 19:45:00 +02:00
|
|
|
|
|
|
|
value_list = ()
|
|
|
|
|
|
|
|
if type(value_in) is str:
|
|
|
|
if value_in:
|
2015-06-22 15:52:45 +02:00
|
|
|
value_list = sorted([s.strip() for s in value_in.split(',')])
|
2013-07-18 19:45:00 +02:00
|
|
|
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")
|
2015-02-27 16:50:00 +01:00
|
|
|
elif alias_in:
|
2015-02-07 00:19:11 +01:00
|
|
|
if len(value_list) != 1:
|
|
|
|
module.fail_json(msg = "parameter 'value' must contain a single dns name for alias create/delete")
|
|
|
|
elif not alias_hosted_zone_id_in:
|
|
|
|
module.fail_json(msg = "parameter 'alias_hosted_zone_id' required for alias create/delete")
|
2013-07-18 19:45:00 +02:00
|
|
|
|
2015-05-20 21:39:17 +02:00
|
|
|
if vpc_id_in and not private_zone_in:
|
|
|
|
module.fail_json(msg="parameter 'private_zone' must be true when specifying parameter"
|
|
|
|
" 'vpc_id'")
|
|
|
|
|
|
|
|
|
2016-01-11 15:45:50 +01:00
|
|
|
# connect to the route53 endpoint
|
2013-07-18 19:45:00 +02:00
|
|
|
try:
|
2014-12-25 01:04:25 +01:00
|
|
|
conn = Route53Connection(**aws_connect_kwargs)
|
2013-07-18 19:45:00 +02:00
|
|
|
except boto.exception.BotoServerError, e:
|
|
|
|
module.fail_json(msg = e.error_message)
|
|
|
|
|
2015-05-05 17:07:18 +02:00
|
|
|
# Find the named zone ID
|
2015-05-20 21:39:17 +02:00
|
|
|
zone = get_zone_by_name(conn, module, zone_in, private_zone_in, hosted_zone_id_in, vpc_id_in)
|
2013-07-18 19:45:00 +02:00
|
|
|
|
|
|
|
# Verify that the requested zone is already defined in Route53
|
2015-05-05 17:07:18 +02:00
|
|
|
if zone is None:
|
2013-07-18 19:45:00 +02:00
|
|
|
errmsg = "Zone %s does not exist in Route53" % zone_in
|
|
|
|
module.fail_json(msg = errmsg)
|
|
|
|
|
|
|
|
record = {}
|
2016-01-11 15:45:50 +01:00
|
|
|
|
2013-07-18 19:45:00 +02:00
|
|
|
found_record = False
|
2015-05-05 17:07:18 +02:00
|
|
|
wanted_rset = Record(name=record_in, type=type_in, ttl=ttl_in,
|
|
|
|
identifier=identifier_in, weight=weight_in, region=region_in,
|
|
|
|
health_check=health_check_in, failover=failover_in)
|
|
|
|
for v in value_list:
|
|
|
|
if alias_in:
|
2015-11-18 19:08:21 +01:00
|
|
|
wanted_rset.set_alias(alias_hosted_zone_id_in, v, alias_evaluate_target_health_in)
|
2015-05-05 17:07:18 +02:00
|
|
|
else:
|
|
|
|
wanted_rset.add_value(v)
|
|
|
|
|
|
|
|
sets = conn.get_all_rrsets(zone.id, name=record_in, type=type_in, identifier=identifier_in)
|
2013-07-18 19:45:00 +02:00
|
|
|
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', '@')
|
2015-11-13 11:32:10 +01:00
|
|
|
#Need to save this changes in rset, because of comparing rset.to_xml() == wanted_rset.to_xml() in next block
|
2015-11-18 19:08:21 +01:00
|
|
|
rset.name = decoded_name
|
2014-03-07 01:10:56 +01:00
|
|
|
|
2015-11-20 21:55:39 +01:00
|
|
|
if rset.type == type_in and decoded_name.lower() == record_in.lower() and str(rset.identifier) == str(identifier_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)
|
2015-04-15 12:43:00 +02:00
|
|
|
if hosted_zone_id_in:
|
|
|
|
record['hosted_zone_id'] = hosted_zone_id_in
|
2015-05-05 17:07:18 +02:00
|
|
|
record['identifier'] = rset.identifier
|
|
|
|
record['weight'] = rset.weight
|
|
|
|
record['region'] = rset.region
|
|
|
|
record['failover'] = rset.failover
|
|
|
|
record['health_check'] = rset.health_check
|
2015-04-15 12:43:00 +02:00
|
|
|
if hosted_zone_id_in:
|
|
|
|
record['hosted_zone_id'] = hosted_zone_id_in
|
2015-02-07 00:19:11 +01:00
|
|
|
if rset.alias_dns_name:
|
|
|
|
record['alias'] = True
|
|
|
|
record['value'] = rset.alias_dns_name
|
|
|
|
record['values'] = [rset.alias_dns_name]
|
|
|
|
record['alias_hosted_zone_id'] = rset.alias_hosted_zone_id
|
2015-11-18 19:08:21 +01:00
|
|
|
record['alias_evaluate_target_health'] = rset.alias_evaluate_target_health
|
2015-02-07 00:19:11 +01:00
|
|
|
else:
|
|
|
|
record['alias'] = False
|
|
|
|
record['value'] = ','.join(sorted(rset.resource_records))
|
|
|
|
record['values'] = sorted(rset.resource_records)
|
2015-05-05 17:07:18 +02:00
|
|
|
if command_in == 'create' and rset.to_xml() == wanted_rset.to_xml():
|
2013-07-18 19:45:00 +02:00
|
|
|
module.exit_json(changed=False)
|
2015-05-05 17:07:18 +02:00
|
|
|
break
|
2013-07-18 19:45:00 +02:00
|
|
|
|
|
|
|
if command_in == 'get':
|
2015-02-20 18:22:03 +01:00
|
|
|
if type_in == 'NS':
|
|
|
|
ns = record['values']
|
|
|
|
else:
|
|
|
|
# Retrieve name servers associated to the zone.
|
|
|
|
ns = conn.get_zone(zone_in).get_nameservers()
|
|
|
|
|
|
|
|
module.exit_json(changed=False, set=record, nameservers=ns)
|
2013-07-18 19:45:00 +02:00
|
|
|
|
|
|
|
if command_in == 'delete' and not found_record:
|
|
|
|
module.exit_json(changed=False)
|
|
|
|
|
2015-05-05 17:07:18 +02:00
|
|
|
changes = ResourceRecordSets(conn, zone.id)
|
2013-08-20 15:58:59 +02:00
|
|
|
|
2013-07-18 19:45:00 +02:00
|
|
|
if command_in == 'create' or command_in == 'delete':
|
2015-05-05 17:07:18 +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")
|
|
|
|
command = 'UPSERT'
|
|
|
|
else:
|
|
|
|
command = command_in.upper()
|
|
|
|
changes.add_change_record(command, wanted_rset)
|
2013-07-18 19:45:00 +02:00
|
|
|
|
|
|
|
try:
|
2016-02-05 08:57:55 +01:00
|
|
|
result = invoke_with_throttling_retries(commit, changes, retry_interval_in, wait_in, wait_timeout_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)
|
2016-01-11 16:43:26 +01:00
|
|
|
except TimeoutError:
|
|
|
|
module.fail_json(msg='Timeout waiting for changes to replicate')
|
2013-07-18 19:45:00 +02:00
|
|
|
|
|
|
|
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()
|