Blame roles/ipaclient/library/ipaclient_setup_ntp.py

Packit 8cb997
#!/usr/bin/python
Packit 8cb997
# -*- coding: utf-8 -*-
Packit 8cb997
Packit 8cb997
# Authors:
Packit 8cb997
#   Thomas Woerner <twoerner@redhat.com>
Packit 8cb997
#
Packit 8cb997
# Based on ipa-client-install code
Packit 8cb997
#
Packit 8cb997
# Copyright (C) 2017  Red Hat
Packit 8cb997
# see file 'COPYING' for use and warranty information
Packit 8cb997
#
Packit 8cb997
# This program is free software; you can redistribute it and/or modify
Packit 8cb997
# it under the terms of the GNU General Public License as published by
Packit 8cb997
# the Free Software Foundation, either version 3 of the License, or
Packit 8cb997
# (at your option) any later version.
Packit 8cb997
#
Packit 8cb997
# This program is distributed in the hope that it will be useful,
Packit 8cb997
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8cb997
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8cb997
# GNU General Public License for more details.
Packit 8cb997
#
Packit 8cb997
# You should have received a copy of the GNU General Public License
Packit 8cb997
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 8cb997
Packit 8cb997
ANSIBLE_METADATA = {
Packit 8cb997
    'metadata_version': '1.0',
Packit 8cb997
    'supported_by': 'community',
Packit 8cb997
    'status': ['preview'],
Packit 8cb997
}
Packit 8cb997
Packit 8cb997
DOCUMENTATION = '''
Packit 8cb997
---
Packit 8cb997
module: ipaclient_setup_ntp
Packit 8cb997
short description: Setup NTP for IPA client
Packit 8cb997
description:
Packit 8cb997
  Setup NTP for IPA client
Packit 8cb997
options:
Packit 8cb997
  ntp_servers:
Packit 8cb997
    description: ntp servers to use
Packit 8cb997
    required: yes
Packit 8cb997
  ntp_pool:
Packit 8cb997
    description: ntp server pool to use
Packit 8cb997
    required: yes
Packit 8cb997
  no_ntp:
Packit 8cb997
    description: Do not configure ntp
Packit 8cb997
    required: yes
Packit 8cb997
  on_master:
Packit 8cb997
    description: Whether the configuration is done on the master or not
Packit 8cb997
    required: yes
Packit 8cb997
  servers:
Packit 8cb997
    description: Fully qualified name of IPA servers to enroll to
Packit 8cb997
    required: yes
Packit 8cb997
  domain:
Packit 8cb997
    description: Primary DNS domain of the IPA deployment
Packit 8cb997
    required: yes
Packit 8cb997
author:
Packit 8cb997
    - Thomas Woerner
Packit 8cb997
'''
Packit 8cb997
Packit 8cb997
EXAMPLES = '''
Packit 8cb997
'''
Packit 8cb997
Packit 8cb997
RETURN = '''
Packit 8cb997
'''
Packit 8cb997
Packit 8cb997
import inspect
Packit 8cb997
Packit 8cb997
from ansible.module_utils.basic import AnsibleModule
Packit 8cb997
from ansible.module_utils.ansible_ipa_client import (
Packit 8cb997
    options, sysrestore, paths, sync_time, logger, ipadiscovery,
Packit 8cb997
    timeconf
Packit 8cb997
)
Packit 8cb997
Packit 8cb997
Packit 8cb997
def main():
Packit 8cb997
    module = AnsibleModule(
Packit 8cb997
        argument_spec=dict(
Packit 8cb997
            # basic
Packit 8cb997
            ntp_servers=dict(required=False, type='list', default=None),
Packit 8cb997
            ntp_pool=dict(required=False, default=None),
Packit 8cb997
            no_ntp=dict(required=False, type='bool', default=False),
Packit 8cb997
            # force_ntpd=dict(required=False, type='bool', default=False),
Packit 8cb997
            on_master=dict(required=False, type='bool', default=False),
Packit 8cb997
            # additional
Packit 8cb997
            servers=dict(required=False, type='list', default=None),
Packit 8cb997
            domain=dict(required=False, default=None),
Packit 8cb997
        ),
Packit 8cb997
        supports_check_mode=True,
Packit 8cb997
    )
Packit 8cb997
Packit 8cb997
    # module._ansible_debug = True
Packit 8cb997
    options.ntp_servers = module.params.get('ntp_servers')
Packit 8cb997
    options.ntp_pool = module.params.get('ntp_pool')
Packit 8cb997
    options.no_ntp = module.params.get('no_ntp')
Packit 8cb997
    # options.force_ntpd = module.params.get('force_ntpd')
Packit 8cb997
    options.on_master = module.params.get('on_master')
Packit 8cb997
    cli_server = module.params.get('servers')
Packit 8cb997
    cli_domain = module.params.get('domain')
Packit 8cb997
Packit 8cb997
    options.conf_ntp = not options.no_ntp
Packit 8cb997
    options.debug = False
Packit 8cb997
Packit 8cb997
    fstore = sysrestore.FileStore(paths.IPA_CLIENT_SYSRESTORE)
Packit 8cb997
    statestore = sysrestore.StateFile(paths.IPA_CLIENT_SYSRESTORE)
Packit 8cb997
Packit 8cb997
    synced_ntp = False
Packit 8cb997
    if sync_time is not None:
Packit 8cb997
        if options.conf_ntp:
Packit 8cb997
            # Attempt to configure and sync time with NTP server (chrony).
Packit 8cb997
            argspec = inspect.getargspec(sync_time)
Packit 8cb997
            if "options" not in argspec.args:
Packit 8cb997
                synced_ntp = sync_time(options.ntp_servers, options.ntp_pool,
Packit 8cb997
                                       fstore, statestore)
Packit 8cb997
            else:
Packit 8cb997
                synced_ntp = sync_time(options, fstore, statestore)
Packit 8cb997
        elif options.on_master:
Packit 8cb997
            # If we're on master skipping the time sync here because it was
Packit 8cb997
            # done in ipa-server-install
Packit 8cb997
            logger.info(
Packit 8cb997
                "Skipping attempt to configure and synchronize time with"
Packit 8cb997
                " chrony server as it has been already done on master.")
Packit 8cb997
        else:
Packit 8cb997
            logger.info("Skipping chrony configuration")
Packit 8cb997
Packit 8cb997
    else:
Packit 8cb997
        ntp_srv_servers = []
Packit 8cb997
        if not options.on_master and options.conf_ntp:
Packit 8cb997
            # Attempt to sync time with IPA server.
Packit 8cb997
            # If we're skipping NTP configuration, we also skip the time sync
Packit 8cb997
            # here.
Packit 8cb997
            # We assume that NTP servers are discoverable through SRV records
Packit 8cb997
            # in the DNS.
Packit 8cb997
            # If that fails, we try to sync directly with IPA server,
Packit 8cb997
            # assuming it runs NTP
Packit 8cb997
            logger.info('Synchronizing time with KDC...')
Packit 8cb997
            ds = ipadiscovery.IPADiscovery()
Packit 8cb997
            ntp_srv_servers = ds.ipadns_search_srv(cli_domain, '_ntp._udp',
Packit 8cb997
                                                   None, break_on_first=False)
Packit 8cb997
            synced_ntp = False
Packit 8cb997
            ntp_servers = ntp_srv_servers
Packit 8cb997
Packit 8cb997
            # use user specified NTP servers if there are any
Packit 8cb997
            if options.ntp_servers:
Packit 8cb997
                ntp_servers = options.ntp_servers
Packit 8cb997
Packit 8cb997
            for s in ntp_servers:
Packit 8cb997
                synced_ntp = timeconf.synconce_ntp(s, options.debug)
Packit 8cb997
                if synced_ntp:
Packit 8cb997
                    break
Packit 8cb997
Packit 8cb997
            if not synced_ntp and not options.ntp_servers:
Packit 8cb997
                synced_ntp = timeconf.synconce_ntp(cli_server[0],
Packit 8cb997
                                                   options.debug)
Packit 8cb997
            if not synced_ntp:
Packit 8cb997
                module.warn(
Packit 8cb997
                    "Unable to sync time with NTP "
Packit 8cb997
                    "server, assuming the time is in sync. Please check "
Packit 8cb997
                    "that 123 UDP port is opened.")
Packit 8cb997
        else:
Packit 8cb997
            logger.info('Skipping synchronizing time with NTP server.')
Packit 8cb997
Packit 8cb997
    # Done
Packit 8cb997
    module.exit_json(changed=synced_ntp)
Packit 8cb997
Packit 8cb997
Packit 8cb997
if __name__ == '__main__':
Packit 8cb997
    main()