| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2019 Felix Fontein <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
module: reverse_dns |
| 14 |
short_description: Set or remove reverse DNS entry for IP |
| 15 |
version_added: 1.2.0 |
| 16 |
author: |
| 17 |
- Felix Fontein (@felixfontein) |
| 18 |
description: |
| 19 |
- Allows to set, update or remove a reverse DNS entry for an IP address. |
| 20 |
extends_documentation_fragment: |
| 21 |
- community.hrobot.robot |
| 22 |
- community.hrobot.attributes |
| 23 |
- community.hrobot.attributes.actiongroup_robot |
| 24 |
notes: |
| 25 |
- For the main IPv4 address of a server, deleting it actually sets it to a default hostname like C(static.X.Y.Z.W.clients.your-server.de). |
| 26 |
This substitution (delete is replaced by changing to this value) is done automatically by the API and results in the module |
| 27 |
not being idempotent in this case. |
| 28 |
attributes: |
| 29 |
action_group: |
| 30 |
version_added: 1.6.0 |
| 31 |
check_mode: |
| 32 |
support: full |
| 33 |
diff_mode: |
| 34 |
support: none |
| 35 |
idempotent: |
| 36 |
support: full |
| 37 |
|
| 38 |
options: |
| 39 |
ip: |
| 40 |
description: |
| 41 |
- The IP address to set or remove a reverse DNS entry for. |
| 42 |
type: str |
| 43 |
required: true |
| 44 |
state: |
| 45 |
description: |
| 46 |
- Whether to set or update (V(present)) or delete (V(absent)) the reverse DNS entry for O(ip). |
| 47 |
type: str |
| 48 |
default: present |
| 49 |
choices: |
| 50 |
- present |
| 51 |
- absent |
| 52 |
value: |
| 53 |
description: |
| 54 |
- The reverse DNS entry for O(ip). |
| 55 |
- Required if O(state=present). |
| 56 |
type: str |
| 57 |
""" |
| 58 |
|
| 59 |
EXAMPLES = r""" |
| 60 |
--- |
| 61 |
- name: Set reverse DNS entry for 1.2.3.4 |
| 62 |
community.hrobot.reverse_dns: |
| 63 |
hetzner_user: foo |
| 64 |
hetzner_password: bar |
| 65 |
ip: 1.2.3.4 |
| 66 |
value: foo.example.com |
| 67 |
|
| 68 |
- name: Remove reverse DNS entry for 2a01:f48:111:4221::1 |
| 69 |
community.hrobot.reverse_dns: |
| 70 |
hetzner_user: foo |
| 71 |
hetzner_password: bar |
| 72 |
ip: 2a01:f48:111:4221::1 |
| 73 |
state: absent |
| 74 |
""" |
| 75 |
|
| 76 |
RETURN = r"""#""" |
| 77 |
|
| 78 |
from ansible.module_utils.basic import AnsibleModule |
| 79 |
|
| 80 |
from ansible_collections.community.hrobot.plugins.module_utils.robot import ( |
| 81 |
BASE_URL, |
| 82 |
ROBOT_DEFAULT_ARGUMENT_SPEC, |
| 83 |
fetch_url_json, |
| 84 |
) |
| 85 |
|
| 86 |
try: |
| 87 |
from urllib.parse import urlencode |
| 88 |
except ImportError: |
| 89 |
# Python 2.x fallback: |
| 90 |
from urllib import urlencode |
| 91 |
|
| 92 |
|
| 93 |
def main(): |
| 94 |
argument_spec = dict( |
| 95 |
ip=dict(type='str', required=True), |
| 96 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 97 |
value=dict(type='str'), |
| 98 |
) |
| 99 |
argument_spec.update(ROBOT_DEFAULT_ARGUMENT_SPEC) |
| 100 |
module = AnsibleModule( |
| 101 |
argument_spec=argument_spec, |
| 102 |
supports_check_mode=True, |
| 103 |
required_if=[('state', 'present', ['value'])], |
| 104 |
) |
| 105 |
|
| 106 |
ip = module.params['ip'] |
| 107 |
state = module.params['state'] |
| 108 |
value = module.params['value'] |
| 109 |
|
| 110 |
url = "{0}/rdns/{1}".format(BASE_URL, ip) |
| 111 |
result, error = fetch_url_json(module, url, accept_errors=['IP_NOT_FOUND', 'RDNS_NOT_FOUND']) |
| 112 |
if error == 'RDNS_NOT_FOUND': |
| 113 |
current = None |
| 114 |
elif error: |
| 115 |
if error == 'IP_NOT_FOUND': |
| 116 |
module.fail_json(msg='The IP address was not found') |
| 117 |
raise AssertionError('Unexpected error {0}'.format(error)) # pragma: no cover |
| 118 |
else: |
| 119 |
current = result['rdns']['ptr'] |
| 120 |
|
| 121 |
changed = False |
| 122 |
expected = value if state == 'present' else None |
| 123 |
|
| 124 |
if current != expected: |
| 125 |
changed = True |
| 126 |
if not module.check_mode: |
| 127 |
if expected is None: |
| 128 |
fetch_url_json(module, url, method='DELETE', allow_empty_result=True) |
| 129 |
else: |
| 130 |
headers = {'Content-type': 'application/x-www-form-urlencoded'} |
| 131 |
data = {'ptr': expected} |
| 132 |
fetch_url_json(module, url, data=urlencode(data), headers=headers, method='POST') |
| 133 |
|
| 134 |
module.exit_json(changed=changed) |
| 135 |
|
| 136 |
|
| 137 |
if __name__ == '__main__': # pragma: no cover |
| 138 |
main() # pragma: no cover |