Blame roles/ipaserver/library/ipaserver_setup_ntp.py

Packit Service 0a38ef
# -*- coding: utf-8 -*-
Packit Service 0a38ef
Packit Service 0a38ef
# Authors:
Packit Service 0a38ef
#   Thomas Woerner <twoerner@redhat.com>
Packit Service 0a38ef
#
Packit Service 0a38ef
# Based on ipa-client-install code
Packit Service 0a38ef
#
Packit Service 0a38ef
# Copyright (C) 2017  Red Hat
Packit Service 0a38ef
# see file 'COPYING' for use and warranty information
Packit Service 0a38ef
#
Packit Service 0a38ef
# This program is free software; you can redistribute it and/or modify
Packit Service 0a38ef
# it under the terms of the GNU General Public License as published by
Packit Service 0a38ef
# the Free Software Foundation, either version 3 of the License, or
Packit Service 0a38ef
# (at your option) any later version.
Packit Service 0a38ef
#
Packit Service 0a38ef
# This program is distributed in the hope that it will be useful,
Packit Service 0a38ef
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 0a38ef
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 0a38ef
# GNU General Public License for more details.
Packit Service 0a38ef
#
Packit Service 0a38ef
# You should have received a copy of the GNU General Public License
Packit Service 0a38ef
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit Service 0a38ef
Packit Service 0a38ef
from __future__ import print_function
Packit Service 0a38ef
Packit Service 0a38ef
ANSIBLE_METADATA = {
Packit Service 0a38ef
    'metadata_version': '1.0',
Packit Service 0a38ef
    'supported_by': 'community',
Packit Service 0a38ef
    'status': ['preview'],
Packit Service 0a38ef
}
Packit Service 0a38ef
Packit Service 0a38ef
DOCUMENTATION = '''
Packit Service 0a38ef
---
Packit Service 0a38ef
module: ipaserver_setup_ntp
Packit Service 0a38ef
short description: Setup NTP
Packit Service 0a38ef
description: Setup NTP
Packit Service 0a38ef
options:
Packit Service 0a38ef
  ntp_servers:
Packit Service 0a38ef
    description: ntp servers to use
Packit Service 0a38ef
    required: yes
Packit Service 0a38ef
  ntp_pool:
Packit Service 0a38ef
    description: ntp server pool to use
Packit Service 0a38ef
    required: yes
Packit Service 0a38ef
author:
Packit Service 0a38ef
    - Thomas Woerner
Packit Service 0a38ef
'''
Packit Service 0a38ef
Packit Service 0a38ef
EXAMPLES = '''
Packit Service 0a38ef
'''
Packit Service 0a38ef
Packit Service 0a38ef
RETURN = '''
Packit Service 0a38ef
'''
Packit Service 0a38ef
Packit Service 0a38ef
import inspect
Packit Service 0a38ef
Packit Service 0a38ef
from ansible.module_utils.basic import AnsibleModule
Packit Service 0a38ef
from ansible.module_utils.ansible_ipa_server import (
Packit Service 0a38ef
    AnsibleModuleLog, setup_logging, options, sysrestore, paths,
Packit Service 0a38ef
    redirect_stdout, time_service, sync_time, ntpinstance, timeconf
Packit Service 0a38ef
)
Packit Service 0a38ef
Packit Service 0a38ef
Packit Service 0a38ef
def main():
Packit Service 0a38ef
    ansible_module = AnsibleModule(
Packit Service 0a38ef
        argument_spec=dict(
Packit Service 0a38ef
            ntp_servers=dict(required=False, type='list', default=None),
Packit Service 0a38ef
            ntp_pool=dict(required=False, default=None),
Packit Service 0a38ef
        ),
Packit Service 0a38ef
    )
Packit Service 0a38ef
Packit Service 0a38ef
    ansible_module._ansible_debug = True
Packit Service 0a38ef
    setup_logging()
Packit Service 0a38ef
    ansible_log = AnsibleModuleLog(ansible_module)
Packit Service 0a38ef
Packit Service 0a38ef
    # set values ############################################################
Packit Service 0a38ef
Packit Service 0a38ef
    options.ntp_servers = ansible_module.params.get('ntp_servers')
Packit Service 0a38ef
    options.ntp_pool = ansible_module.params.get('ntp_pool')
Packit Service 0a38ef
Packit Service 0a38ef
    # init ##########################################################
Packit Service 0a38ef
Packit Service 0a38ef
    fstore = sysrestore.FileStore(paths.SYSRESTORE)
Packit Service 0a38ef
    sstore = sysrestore.StateFile(paths.SYSRESTORE)
Packit Service 0a38ef
Packit Service 0a38ef
    # setup NTP #####################################################
Packit Service 0a38ef
Packit Service 0a38ef
    if time_service == "chronyd":
Packit Service 0a38ef
        # We have to sync time before certificate handling on master.
Packit Service 0a38ef
        # As chrony configuration is moved from client here, unconfiguration of
Packit Service 0a38ef
        # chrony will be handled here in uninstall() method as well by invoking
Packit Service 0a38ef
        # the ipa-server-install --uninstall
Packit Service 0a38ef
        ansible_module.log("Synchronizing time")
Packit Service 0a38ef
Packit Service 0a38ef
        argspec = inspect.getargspec(sync_time)
Packit Service 0a38ef
        if "options" not in argspec.args:
Packit Service 0a38ef
            synced_ntp = sync_time(options.ntp_servers, options.ntp_pool,
Packit Service 0a38ef
                                   fstore, sstore)
Packit Service 0a38ef
        else:
Packit Service 0a38ef
            synced_ntp = sync_time(options, fstore, sstore)
Packit Service 0a38ef
        if not synced_ntp:
Packit Service 0a38ef
            ansible_module.log(
Packit Service 0a38ef
                "Warning: IPA was unable to sync time with chrony!")
Packit Service 0a38ef
            ansible_module.log(
Packit Service 0a38ef
                "         Time synchronization is required for IPA "
Packit Service 0a38ef
                "to work correctly")
Packit Service 0a38ef
    else:
Packit Service 0a38ef
        # Configure ntpd
Packit Service 0a38ef
        timeconf.force_ntpd(sstore)
Packit Service 0a38ef
        ntp = ntpinstance.NTPInstance(fstore)
Packit Service 0a38ef
        ntp.set_output(ansible_log)
Packit Service 0a38ef
        with redirect_stdout(ansible_log):
Packit Service 0a38ef
            if not ntp.is_configured():
Packit Service 0a38ef
                ntp.create_instance()
Packit Service 0a38ef
Packit Service 0a38ef
    # done ##########################################################
Packit Service 0a38ef
Packit Service 0a38ef
    ansible_module.exit_json(changed=True)
Packit Service 0a38ef
Packit Service 0a38ef
Packit Service 0a38ef
if __name__ == '__main__':
Packit Service 0a38ef
    main()