UCS NTP Server module (#42375)

* UCS NTP Server module initial commit

* change name to ntp_server, keep name as alias for ntp_server
pull/41479/merge
John McDonough 6 years ago committed by Dag Wieers
parent c527af58f1
commit f89d3721b4

@ -0,0 +1,170 @@
#!/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_ntp_server
short_description: Configures NTP server on Cisco UCS Manager
extends_documentation_fragment:
- ucs
description:
- Configures NTP server 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 an NTP server.
- If C(present), will add or update an NTP server.
choices: [absent, present]
default: present
ntp_server:
description:
- NTP server IP address or hostname.
- Enter up to 63 characters that form a valid hostname.
- Enter a valid IPV4 Address.
aliases: [ name ]
default: ""
description:
description:
- A user-defined description of the NTP 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 ]
default: ""
requirements:
- ucsmsdk
author:
- John McDonough (@movinalot)
- CiscoUcs (@CiscoUcs)
version_added: "2.7"
'''
EXAMPLES = r'''
- name: Configure NTP server
ucs_ntp_server:
hostname: 172.16.143.150
username: admin
password: password
ntp_server: 10.10.10.10
description: Internal NTP Server by IP address
state: present
- name: Configure NTP server
ucs_ntp_server:
hostname: 172.16.143.150
username: admin
password: password
ntp_server: pool.ntp.org
description: External NTP Server by hostname
state: present
- name: Remove NTP server
ucs_ntp_server:
hostname: 172.16.143.150
username: admin
password: password
ntp_server: 10.10.10.10
state: absent
- name: Remove NTP server
ucs_ntp_server:
hostname: 172.16.143.150
username: admin
password: password
ntp_server: pool.ntp.org
state: absent
'''
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(
ntp_server=dict(type='str', aliases=['name']),
description=dict(type='str', aliases=['descr'], default=''),
state=dict(type='str', default='present', choices=['present', 'absent']),
)
module = AnsibleModule(
argument_spec,
supports_check_mode=True,
required_if=[
['state', 'present', ['ntp_server']],
],
)
# UCSModule verifies ucsmsdk is present and exits on failure. Imports are below ucs object creation.
ucs = UCSModule(module)
err = False
from ucsmsdk.mometa.comm.CommNtpProvider import CommNtpProvider
changed = False
try:
mo_exists = False
props_match = False
dn = 'sys/svc-ext/datetime-svc/ntp-' + module.params['ntp_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 = CommNtpProvider(parent_mo_or_dn='sys/svc-ext/datetime-svc',
name=module.params['ntp_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()

@ -0,0 +1,7 @@
# Not enabled, but can be used with the UCS Platform Emulator or UCS hardware.
# Example integration_config.yml:
# ---
# ucs_hostname: 172.22.251.170
# ucs_username: admin
# ucs_password: password
unsupported

@ -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: NTP Server absent
ucs_ntp_server: &ntp_server_absent
<<: *login_info
ntp_server: pool.ntp.org
state: absent
# Test present (check_mode)
- name: NTP Server present (check_mode)
ucs_ntp_server: &ntp_server_present
<<: *login_info
ntp_server: pool.ntp.org
check_mode: yes
register: cm_ntp_server_present
# Present (normal mode)
- name: NTP Server present (normal mode)
ucs_ntp_server: *ntp_server_present
register: nm_ntp_server_present
# Test present again (idempotent)
- name: NTP Server present again (check_mode)
ucs_ntp_server: *ntp_server_present
check_mode: yes
register: cm_ntp_server_present_again
# Present again (normal mode)
- name: NTP Server present again (normal mode)
ucs_ntp_server: *ntp_server_present
register: nm_ntp_server_present_again
# Verfiy present
- name: Verify NTP Server present results
assert:
that:
- cm_ntp_server_present.changed == nm_ntp_server_present.changed == true
- cm_ntp_server_present_again.changed == nm_ntp_server_present_again.changed == false
# Test change (check_mode)
- name: NTP NTP Server change (check_mode)
ucs_ntp_server: &ntp_server_change
<<: *ntp_server_present
ntp_server: 10.10.10.10
check_mode: yes
register: cm_ntp_ntp_server_change
# Change (normal mode)
- name: NTP NTP Server change (normal mode)
ucs_ntp_server: *ntp_server_change
register: nm_ntp_ntp_server_change
# Test change again (idempotent)
- name: NTP NTP Server change again (check_mode)
ucs_ntp_server: *ntp_server_change
check_mode: yes
register: cm_ntp_ntp_server_change_again
# Change again (normal mode)
- name: NTP NTP Server change again (normal mode)
ucs_ntp_server: *ntp_server_change
register: nm_ntp_ntp_server_change_again
# Verfiy change
- name: Verify NTP NTP Server change results
assert:
that:
- cm_ntp_ntp_server_change.changed == nm_ntp_ntp_server_change.changed == true
- cm_ntp_ntp_server_change_again.changed == nm_ntp_ntp_server_change_again.changed == false
# Teardown (clean environment)
- name: NTP Server absent (check_mode)
ucs_ntp_server: *ntp_server_absent
check_mode: yes
register: cm_ntp_server_absent
# Absent (normal mode)
- name: NTP Server absent (normal mode)
ucs_ntp_server: *ntp_server_absent
register: nm_ntp_server_absent
# Test absent again (idempotent)
- name: NTP Server absent again (check_mode)
ucs_ntp_server: *ntp_server_absent
check_mode: yes
register: cm_ntp_server_absent_again
# Absent again (normal mode)
- name: NTP Server absent again (normal mode)
ucs_ntp_server: *ntp_server_absent
register: nm_ntp_server_absent_again
# Verfiy absent
- name: Verify NTP Server absent results
assert:
that:
- cm_ntp_server_absent.changed == nm_ntp_server_absent.changed == true
- cm_ntp_server_absent_again.changed == nm_ntp_server_absent_again.changed == false
Loading…
Cancel
Save