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