community.general/lib/ansible/modules/network/nxos/nxos_ntp_options.py

167 lines
4.6 KiB
Python
Raw Normal View History

2016-09-15 09:28:59 +00:00
#!/usr/bin/python
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
2017-08-16 03:16:38 +00:00
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'network'}
2016-12-06 10:35:05 +00:00
2016-09-15 09:28:59 +00:00
DOCUMENTATION = '''
---
module: nxos_ntp_options
extends_documentation_fragment: nxos
version_added: "2.2"
2016-09-15 09:28:59 +00:00
short_description: Manages NTP options.
description:
- Manages NTP options, e.g. authoritative server and logging.
author:
- Jason Edelman (@jedelman8)
notes:
- Tested against NXOSv 7.3.(0)D1(1) on VIRL
2018-04-13 10:17:32 +00:00
- When C(state=absent), master and logging will be set to False and
stratum will be removed as well
2016-09-15 09:28:59 +00:00
options:
master:
description:
- Sets whether the device is an authoritative NTP server.
type: bool
2016-11-02 13:49:55 +00:00
stratum:
2016-09-15 09:28:59 +00:00
description:
- If C(master=true), an optional stratum can be supplied (1-15).
The device default is 8.
logging:
description:
- Sets whether NTP logging is enabled on the device.
type: bool
2016-09-15 09:28:59 +00:00
state:
description:
- Manage the state of the resource.
default: present
choices: ['present','absent']
'''
EXAMPLES = '''
# Basic NTP options configuration
- nxos_ntp_options:
master: true
stratum: 12
logging: false
host: "{{ inventory_hostname }}"
username: "{{ un }}"
password: "{{ pwd }}"
2016-09-15 09:28:59 +00:00
'''
RETURN = '''
updates:
description: command sent to the device
returned: always
type: list
2018-04-13 10:17:32 +00:00
sample: ["no ntp logging", "ntp master 12"]
2016-09-15 09:28:59 +00:00
'''
2017-08-08 16:12:02 +00:00
import re
2016-09-15 09:28:59 +00:00
from ansible.module_utils.network.nxos.nxos import get_config, load_config, run_commands
from ansible.module_utils.network.nxos.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
2016-09-15 09:28:59 +00:00
2017-08-08 16:12:02 +00:00
def get_current(module):
2018-04-13 10:17:32 +00:00
cmd = ('show running-config | inc ntp')
2016-09-15 09:28:59 +00:00
2018-04-13 10:17:32 +00:00
master = False
logging = False
stratum = None
2016-09-15 09:28:59 +00:00
2018-04-13 10:17:32 +00:00
output = run_commands(module, ({'command': cmd, 'output': 'text'}))[0]
2016-09-15 09:28:59 +00:00
2018-04-13 10:17:32 +00:00
if output:
match = re.search(r"^ntp master(?: (\d+))", output, re.M)
if match:
master = True
stratum = match.group(1)
logging = 'ntp logging' in output.lower()
2016-09-15 09:28:59 +00:00
2017-08-08 16:12:02 +00:00
return {'master': master, 'stratum': stratum, 'logging': logging}
2016-09-15 09:28:59 +00:00
def main():
argument_spec = dict(
master=dict(required=False, type='bool'),
2018-04-13 10:17:32 +00:00
stratum=dict(required=False, type='str'),
logging=dict(required=False, type='bool'),
state=dict(choices=['absent', 'present'], default='present'),
2016-09-15 09:28:59 +00:00
)
argument_spec.update(nxos_argument_spec)
module = AnsibleModule(argument_spec=argument_spec,
2017-08-08 16:12:02 +00:00
supports_check_mode=True)
2016-09-15 09:28:59 +00:00
warnings = list()
check_args(module, warnings)
2016-09-15 09:28:59 +00:00
master = module.params['master']
stratum = module.params['stratum']
logging = module.params['logging']
state = module.params['state']
2018-04-13 10:17:32 +00:00
if stratum and master is False:
if stratum != 8:
module.fail_json(msg='master MUST be True when stratum is changed')
2016-09-15 09:28:59 +00:00
2017-08-08 16:12:02 +00:00
current = get_current(module)
2016-09-15 09:28:59 +00:00
2017-08-08 16:12:02 +00:00
result = {'changed': False}
2016-09-15 09:28:59 +00:00
2017-08-08 16:12:02 +00:00
commands = list()
2016-09-15 09:28:59 +00:00
2017-08-08 16:12:02 +00:00
if state == 'absent':
if current['master']:
commands.append('no ntp master')
if current['logging']:
commands.append('no ntp logging')
2016-09-15 09:28:59 +00:00
2017-08-08 16:12:02 +00:00
elif state == 'present':
2018-04-13 10:17:32 +00:00
if master and not current['master']:
commands.append('ntp master')
elif master is False and current['master']:
commands.append('no ntp master')
if stratum and stratum != current['stratum']:
2017-08-08 16:12:02 +00:00
commands.append('ntp master %s' % stratum)
2018-04-13 10:17:32 +00:00
if logging and not current['logging']:
commands.append('ntp logging')
elif logging is False and current['logging']:
commands.append('no ntp logging')
2017-08-08 16:12:02 +00:00
result['commands'] = commands
result['updates'] = commands
if commands:
if not module.check_mode:
load_config(module, commands)
result['changed'] = True
result['warnings'] = warnings
module.exit_json(**result)
2016-09-15 09:28:59 +00:00
if __name__ == '__main__':
main()