diff --git a/lib/ansible/modules/remote_management/ucs/ucs_dns_server.py b/lib/ansible/modules/remote_management/ucs/ucs_dns_server.py new file mode 100644 index 00000000000..93198f6dacf --- /dev/null +++ b/lib/ansible/modules/remote_management/ucs/ucs_dns_server.py @@ -0,0 +1,169 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +ANSIBLE_METADATA = {'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community'} + +DOCUMENTATION = r''' +--- +module: ucs_dns_server + +short_description: Configure DNS servers on Cisco UCS Manager + +extends_documentation_fragment: +- ucs + +description: +- Configure DNS servers on Cisco UCS Manager. +- Examples can be used with the L(UCS Platform Emulator,https://communities.cisco.com/ucspe). + +options: + state: + description: + - If C(absent), will remove a DNS server. + - If C(present), will add or update a DNS server. + choices: [absent, present] + default: present + type: str + + dns_server: + description: + - DNS server IP address. + - Enter a valid IPV4 Address. + - UCS Manager supports up to 4 DNS Servers + aliases: [ name ] + type: str + + description: + description: + - A user-defined description of the DNS server. + - Enter up to 256 characters. + - "You can use any characters or spaces except the following:" + - "` (accent mark), \ (backslash), ^ (carat), \" (double quote), = (equal sign), > (greater than), < (less than), or ' (single quote)." + aliases: [ descr ] + type: str + + delegate_to: + description: + - Where the module will be run + default: localhost + type: str + +requirements: +- ucsmsdk + +author: +- John McDonough (@movinalot) +- CiscoUcs (@CiscoUcs) + +version_added: "2.8" +''' + +EXAMPLES = r''' +- name: Configure DNS server + ucs_dns_server: + hostname: 172.16.143.150 + username: admin + password: password + dns_server: 10.10.10.10 + description: DNS Server IP address + state: present + delegate_to: localhost + +- name: Remove DNS server + ucs_dns_server: + hostname: 172.16.143.150 + username: admin + password: password + dns_server: 10.10.10.10 + state: absent + delegate_to: localhost +''' + +RETURN = r''' +# +''' + +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.remote_management.ucs import UCSModule, ucs_argument_spec + + +def run_module(): + argument_spec = ucs_argument_spec + argument_spec.update( + dns_server=dict(type='str', aliases=['name']), + description=dict(type='str', aliases=['descr'], default=''), + state=dict(type='str', default='present', choices=['present', 'absent']), + delegate_to=dict(type='str', default='localhost'), + ) + + module = AnsibleModule( + argument_spec, + supports_check_mode=True, + required_if=[ + ['state', 'present', ['dns_server']], + ], + ) + # UCSModule verifies ucsmsdk is present and exits on failure. + # Imports are below for UCS object creation. + ucs = UCSModule(module) + from ucsmsdk.mometa.comm.CommDnsProvider import CommDnsProvider + + err = False + changed = False + + try: + mo_exists = False + props_match = False + + dn = 'sys/svc-ext/dns-svc/dns-' + module.params['dns_server'] + + mo = ucs.login_handle.query_dn(dn) + if mo: + mo_exists = True + + if module.params['state'] == 'absent': + if mo_exists: + if not module.check_mode: + ucs.login_handle.remove_mo(mo) + ucs.login_handle.commit() + changed = True + else: + if mo_exists: + # check top-level mo props + kwargs = dict(descr=module.params['description']) + if mo.check_prop_match(**kwargs): + props_match = True + + if not props_match: + if not module.check_mode: + # update/add mo + mo = CommDnsProvider(parent_mo_or_dn='sys/svc-ext/dns-svc', + name=module.params['dns_server'], + descr=module.params['description']) + ucs.login_handle.add_mo(mo, modify_present=True) + ucs.login_handle.commit() + changed = True + + except Exception as e: + err = True + ucs.result['msg'] = "setup error: %s " % str(e) + + ucs.result['changed'] = changed + if err: + module.fail_json(**ucs.result) + module.exit_json(**ucs.result) + + +def main(): + run_module() + + +if __name__ == '__main__': + main() diff --git a/test/integration/targets/ucs_dns_server/aliases b/test/integration/targets/ucs_dns_server/aliases new file mode 100644 index 00000000000..39a783a0837 --- /dev/null +++ b/test/integration/targets/ucs_dns_server/aliases @@ -0,0 +1,7 @@ +# Not enabled, but can be used with the UCS Platform Emulator or UCS hardware. +# Example integration_config.yml: +# --- +# ucs_hostname: 10.10.10.10 +# ucs_username: admin +# ucs_password: password +unsupported \ No newline at end of file diff --git a/test/integration/targets/ucs_dns_server/tasks/main.yml b/test/integration/targets/ucs_dns_server/tasks/main.yml new file mode 100644 index 00000000000..9050d82d474 --- /dev/null +++ b/test/integration/targets/ucs_dns_server/tasks/main.yml @@ -0,0 +1,110 @@ +# Test code for the UCS modules +# Copyright 2018, John McDonough (@movinalot) + +- name: Test that we have a UCS host, UCS username, and UCS password. + fail: + msg: 'Please define the following variables: ucs_hostname, ucs_username and ucs_password.' + when: ucs_hostname is not defined or ucs_username is not defined or ucs_password is not defined + vars: + login_info: &login_info + hostname: "{{ ucs_hostname }}" + username: "{{ ucs_username }}" + password: "{{ ucs_password }}" + +# Setup (clean environment) +- name: DNS Server absent + ucs_dns_server: &dns_server_absent + <<: *login_info + dns_server: 10.10.10.10 + state: absent + +# Test present (check_mode) +- name: DNS Server present (check_mode) + ucs_dns_server: &dns_server_present + <<: *login_info + dns_server: 10.10.10.10 + check_mode: yes + register: cm_dns_server_present + +# Present (normal mode) +- name: DNS Server present (normal mode) + ucs_dns_server: *dns_server_present + register: nm_dns_server_present + +# Test present again (idempotent) +- name: DNS Server present again (check_mode) + ucs_dns_server: *dns_server_present + check_mode: yes + register: cm_dns_server_present_again + +# Present again (normal mode) +- name: DNS Server present again (normal mode) + ucs_dns_server: *dns_server_present + register: nm_dns_server_present_again + +# Verfiy present +- name: Verify DNS Server present results + assert: + that: + - cm_dns_server_present.changed == nm_dns_server_present.changed == true + - cm_dns_server_present_again.changed == nm_dns_server_present_again.changed == false + +# Test change (check_mode) +- name: DNS DNS Server change (check_mode) + ucs_dns_server: &dns_server_change + <<: *dns_server_present + dns_server: 10.10.10.10 + check_mode: yes + register: cm_dns_dns_server_change + +# Change (normal mode) +- name: DNS DNS Server change (normal mode) + ucs_dns_server: *dns_server_change + register: nm_dns_dns_server_change + +# Test change again (idempotent) +- name: DNS DNS Server change again (check_mode) + ucs_dns_server: *dns_server_change + check_mode: yes + register: cm_dns_dns_server_change_again + +# Change again (normal mode) +- name: DNS DNS Server change again (normal mode) + ucs_dns_server: *dns_server_change + register: nm_dns_dns_server_change_again + +# Verfiy change +- name: Verify DNS DNS Server change results + assert: + that: + - cm_dns_dns_server_change.changed == nm_dns_dns_server_change.changed == true + - cm_dns_dns_server_change_again.changed == nm_dns_dns_server_change_again.changed == false + +# Teardown (clean environment) +- name: DNS Server absent (check_mode) + ucs_dns_server: *dns_server_absent + check_mode: yes + register: cm_dns_server_absent + +# Absent (normal mode) +- name: DNS Server absent (normal mode) + ucs_dns_server: *dns_server_absent + register: nm_dns_server_absent + +# Test absent again (idempotent) +- name: DNS Server absent again (check_mode) + ucs_dns_server: *dns_server_absent + check_mode: yes + register: cm_dns_server_absent_again + +# Absent again (normal mode) +- name: DNS Server absent again (normal mode) + ucs_dns_server: *dns_server_absent + register: nm_dns_server_absent_again + +# Verfiy absent +- name: Verify DNS Server absent results + assert: + that: + - cm_dns_server_absent.changed == nm_dns_server_absent.changed == true + - cm_dns_server_absent_again.changed == nm_dns_server_absent_again.changed == false