def Enable_Set_NTP(logger, si, hostList, ntpServers):
    enable_ntp = []
    content = si.RetrieveContent()
    searchIndex = content.searchIndex
    for esxHost in hostList:
        try:
            host = searchIndex.FindByDnsName(dnsName=esxHost, vmSearch=False)
            # NTP manager on ESXi host
            logger.info(
                "THREAD - Enable_Set_NTP - Configuring NTP Server on Host %s" %
                esxHost)
            dateTimeManager = host.configManager.dateTimeSystem

            # configure NTP Servers if not configured
            ntpConfig = vim.HostNtpConfig(server=ntpServers)
            dateConfig = vim.HostDateTimeConfig(ntpConfig=ntpConfig)
            dateTimeManager.UpdateDateTimeConfig(config=dateConfig)

            # start ntpd service
            serviceManager = host.configManager.serviceSystem
            if dateTimeManager.dateTimeInfo.ntpConfig.server != []:
                logger.info(
                    "THREAD - Enable_Set_NTP - Starting ntpd service on " +
                    esxHost)
                serviceManager.StartService(id='ntpd')
                logger.info(
                    "THREAD - Enable_Set_NTP - NTP Service started on " +
                    esxHost)
            enable_ntp.append(esxHost)
        except Exception, e:
            print "THREAD - Enable_Set_NTP - Error while configurimg services %s" % str(
                e)
Beispiel #2
0
def add_ntp_server(host_obj,
                   ntp_servers,
                   policy_state=vim.host.Service.Policy.on,
                   restart_service=False):
    #NTP manager on ESXi host
    dateTimeManager = host_obj.configManager.dateTimeSystem

    # configure NTP Servers if not configured
    #ntpServers = ['192.168.1.100','192.168.1.200']
    if ntp_servers:
        ntpConfig = vim.HostNtpConfig(server=ntp_servers)
        dateConfig = vim.HostDateTimeConfig(ntpConfig=ntpConfig)
        dateTimeManager.UpdateDateTimeConfig(config=dateConfig)

        # start ntpd service
        serviceManager = host_obj.configManager.serviceSystem
        serviceManager.UpdateServicePolicy('ntpd', policy_state)

        #print "Starting ntpd service on " + args.vihost
        if restart_service:
            serviceManager.RestartService(id='ntpd')
        else:
            serviceManager.StartService(id='ntpd')

    else:
        print "Error: Unable to start NTP Service because no NTP servers have not been configured"
def CreateHostConfigProfile(ntpServer, lockdownMode):
   ntpServers = [ntpServer]
   ntpConfig = vim.HostNtpConfig(server=ntpServers)
   dateTimeConfig = vim.HostDateTimeConfig(ntpConfig=ntpConfig)
   hostConfigProfile = \
       vim.ClusterComputeResource.\
       HostConfigurationProfile(dateTimeConfig=dateTimeConfig,
                                lockdownMode=lockdownMode)
   return hostConfigProfile
 def enable_ntp(self, ntp):
     # Config NTP servers
     ntp_spec = vim.host.NtpConfig(server=ntp)
     time_config = vim.HostDateTimeConfig(ntpConfig=ntp_spec)
     time_manager = self.host_system.configManager.dateTimeSystem
     time_manager.UpdateDateTimeConfig(config=time_config)
     # Start NTP service
     service_manager = self.host_system.configManager.serviceSystem
     print "Starting ntpd on " + self.host_system.name
     service_manager.StartService(id='ntpd')
def configure_datetime(module, host_system, ntp_servers, ntpd_state, timezone):
    changed = False
    host_config_manager = host_system.configManager
    host_datetime_system = host_config_manager.dateTimeSystem

    # Configure NTP servers
    current_ntp_servers = host_datetime_system.dateTimeInfo.ntpConfig.server
    if current_ntp_servers != ntp_servers:
        ntpConfig = vim.HostNtpConfig(server=ntp_servers)
        dateConfig = vim.HostDateTimeConfig(ntpConfig=ntpConfig)
        host_datetime_system.UpdateDateTimeConfig(config=dateConfig)
        # Explicitly not restarting the service to give the user full control
        # over when it happens (with handlers).
        changed = True

    # Manage the NTP service
    host_service_system = host_config_manager.serviceSystem
    services = host_service_system.serviceInfo.service

    ntpd_service = [service for service in services
                    if service.key == 'ntpd'][0]

    if ntpd_state == 'restarted':
        host_service_system.RestartService('ntpd')
        changed = True
    elif ntpd_state == 'running' and not ntpd_service.running:
        host_service_system.StartService('ntpd')
        changed = True
    elif ntpd_state == 'stopped' and ntpd_service.running:
        host_service_system.StopService('ntpd')
        changed = True

    # Configure the timezone (making sure the requested timezone
    # is listed as a valid option).
    current_timezone = host_datetime_system.dateTimeInfo.timeZone
    if timezone and (current_timezone.name != timezone):
        timezones = host_datetime_system.QueryAvailableTimeZones()

        if len(list(filter(lambda t: t.name == timezone, timezones))) < 1:
            module.fail_json(msg='Invalid timezone requested')

        dateConfig = vim.HostDataTimeConfig(timeZone=timezone)
        host_datetime_system.UpdateDateTimeConfig(config=dateConfig)
        changed = True

    return changed