| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
ANSIBLE_METADATA = {'metadata_version': '1.1', |
| 10 |
'status': ['preview'], |
| 11 |
'supported_by': 'community'} |
| 12 |
|
| 13 |
DOCUMENTATION = r''' |
| 14 |
--- |
| 15 |
module: ucs_dns_server |
| 16 |
short_description: Configure DNS servers on Cisco UCS Manager |
| 17 |
description: |
| 18 |
- Configure DNS servers on Cisco UCS Manager. |
| 19 |
extends_documentation_fragment: cisco.ucs.ucs |
| 20 |
options: |
| 21 |
state: |
| 22 |
description: |
| 23 |
- If C(absent), will remove a DNS server. |
| 24 |
- If C(present), will add or update a DNS server. |
| 25 |
choices: [absent, present] |
| 26 |
default: present |
| 27 |
type: str |
| 28 |
|
| 29 |
dns_server: |
| 30 |
description: |
| 31 |
- DNS server IP address. |
| 32 |
- Enter a valid IPV4 Address. |
| 33 |
- UCS Manager supports up to 4 DNS Servers |
| 34 |
aliases: [ name ] |
| 35 |
type: str |
| 36 |
|
| 37 |
description: |
| 38 |
description: |
| 39 |
- A user-defined description of the DNS server. |
| 40 |
- Enter up to 256 characters. |
| 41 |
- "You can use any characters or spaces except the following:" |
| 42 |
- "` (accent mark), \ (backslash), ^ (carat), \" (double quote), = (equal sign), > (greater than), < (less than), or ' (single quote)." |
| 43 |
aliases: [ descr ] |
| 44 |
type: str |
| 45 |
|
| 46 |
delegate_to: |
| 47 |
description: |
| 48 |
- Where the module will be run |
| 49 |
default: localhost |
| 50 |
type: str |
| 51 |
|
| 52 |
requirements: |
| 53 |
- ucsmsdk |
| 54 |
|
| 55 |
author: |
| 56 |
- David Soper (@dsoper2) |
| 57 |
- John McDonough (@movinalot) |
| 58 |
- CiscoUcs (@CiscoUcs) |
| 59 |
''' |
| 60 |
|
| 61 |
EXAMPLES = r''' |
| 62 |
- name: Configure DNS server |
| 63 |
cisco.ucs.ucs_dns_server: |
| 64 |
hostname: 172.16.143.150 |
| 65 |
username: admin |
| 66 |
password: password |
| 67 |
dns_server: 10.10.10.10 |
| 68 |
description: DNS Server IP address |
| 69 |
state: present |
| 70 |
delegate_to: localhost |
| 71 |
|
| 72 |
- name: Remove DNS server |
| 73 |
cisco.ucs.ucs_dns_server: |
| 74 |
hostname: 172.16.143.150 |
| 75 |
username: admin |
| 76 |
password: password |
| 77 |
dns_server: 10.10.10.10 |
| 78 |
state: absent |
| 79 |
delegate_to: localhost |
| 80 |
''' |
| 81 |
|
| 82 |
RETURN = r''' |
| 83 |
# |
| 84 |
''' |
| 85 |
|
| 86 |
from ansible.module_utils.basic import AnsibleModule |
| 87 |
from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec |
| 88 |
|
| 89 |
|
| 90 |
def run_module(): |
| 91 |
argument_spec = ucs_argument_spec.copy() |
| 92 |
argument_spec.update( |
| 93 |
dns_server=dict(type='str', aliases=['name']), |
| 94 |
description=dict(type='str', aliases=['descr']), |
| 95 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 96 |
delegate_to=dict(type='str', default='localhost'), |
| 97 |
) |
| 98 |
|
| 99 |
module = AnsibleModule( |
| 100 |
argument_spec, |
| 101 |
supports_check_mode=True, |
| 102 |
required_if=[ |
| 103 |
['state', 'present', ['dns_server']], |
| 104 |
], |
| 105 |
) |
| 106 |
# UCSModule verifies ucsmsdk is present and exits on failure. |
| 107 |
# Imports are below for UCS object creation. |
| 108 |
ucs = UCSModule(module) |
| 109 |
from ucsmsdk.mometa.comm.CommDnsProvider import CommDnsProvider |
| 110 |
|
| 111 |
err = False |
| 112 |
changed = False |
| 113 |
|
| 114 |
try: |
| 115 |
mo_exists = False |
| 116 |
props_match = False |
| 117 |
|
| 118 |
dn = 'sys/svc-ext/dns-svc/dns-' + module.params['dns_server'] |
| 119 |
|
| 120 |
mo = ucs.login_handle.query_dn(dn) |
| 121 |
if mo: |
| 122 |
mo_exists = True |
| 123 |
|
| 124 |
if module.params['state'] == 'absent': |
| 125 |
if mo_exists: |
| 126 |
if not module.check_mode: |
| 127 |
ucs.login_handle.remove_mo(mo) |
| 128 |
ucs.login_handle.commit() |
| 129 |
changed = True |
| 130 |
else: |
| 131 |
if mo_exists: |
| 132 |
# check top-level mo props |
| 133 |
kwargs = dict(descr=module.params['description']) |
| 134 |
if mo.check_prop_match(**kwargs): |
| 135 |
props_match = True |
| 136 |
|
| 137 |
if not props_match: |
| 138 |
if not module.check_mode: |
| 139 |
# update/add mo |
| 140 |
mo = CommDnsProvider(parent_mo_or_dn='sys/svc-ext/dns-svc', |
| 141 |
name=module.params['dns_server'], |
| 142 |
descr=module.params['description']) |
| 143 |
ucs.login_handle.add_mo(mo, modify_present=True) |
| 144 |
ucs.login_handle.commit() |
| 145 |
changed = True |
| 146 |
|
| 147 |
except Exception as e: |
| 148 |
err = True |
| 149 |
ucs.result['msg'] = "setup error: %s " % str(e) |
| 150 |
|
| 151 |
ucs.result['changed'] = changed |
| 152 |
if err: |
| 153 |
module.fail_json(**ucs.result) |
| 154 |
module.exit_json(**ucs.result) |
| 155 |
|
| 156 |
|
| 157 |
def main(): |
| 158 |
run_module() |
| 159 |
|
| 160 |
|
| 161 |
if __name__ == '__main__': |
| 162 |
main() |