Skip to content

Instantly share code, notes, and snippets.

@lvonk
Created November 19, 2013 19:17
Show Gist options
  • Save lvonk/7550884 to your computer and use it in GitHub Desktop.
Save lvonk/7550884 to your computer and use it in GitHub Desktop.
Ansible module that to register reverse DNS records in rackspace using pyrax.
#!/usr/bin/python
# Place this file in for instance my_project/modules and run playbook with
# ansible-playbook project.yml -i development --module-path modules
# Usage:
# - hosts: local-app
# connection: local
# user: root
# tasks:
# - name: Rackspace | Add PTR records
# local_action:
# module: rax_dns
# credentials: ~/.my_rax_credentials_file
# hostname: "foo.example.com"
# As a result it will create 2 reverse DNS records, for both ipv4 and ipv6.
# TODO: Make documentation according to ansible manual...
# - run from a playbook
import pyrax
def main():
module = AnsibleModule(
argument_spec = dict(
credentials = dict(required=True),
hostname = dict(required=True)
)
)
pyrax.set_setting("identity_type", "rackspace")
credentials = os.path.expanduser(module.params['credentials'])
hostname = module.params['hostname']
pyrax.set_credential_file(credentials)
cloudservers = pyrax.cloudservers
server = next((x for x in cloudservers.servers.list() if x.name == hostname), None)
if server:
ipv4 = getattr(server, 'accessIPv4')
ipv6 = getattr(server, 'accessIPv6')
dns = pyrax.cloud_dns
ptr_records = dns.list_ptr_records(server)
changed_ptr = False
records = []
for ip in [ipv4, ipv6]:
ptr_record = next((x for x in ptr_records if (x.data == ip and x.name == hostname)), None)
if not ptr_record:
changed_ptr = True
new_record = {
"name": hostname,
"type": "PTR",
"data": ip,
"ttl": 7200
}
records.append(new_record)
if records:
message = dns.add_ptr_records(server, records)
else:
message = "All PTR records already exist..."
module.exit_json(changed=changed_ptr, msg=message)
else:
module.fail_json(msg="Host {0} not found.".format(hostname))
# include magic from lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment