| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Abhijeet Kasurde <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = r''' |
| 13 |
--- |
| 14 |
module: vmware_host_service_manager |
| 15 |
short_description: Manage services on a given ESXi host |
| 16 |
description: |
| 17 |
- This module can be used to manage (start, stop, restart) services on a given ESXi host. |
| 18 |
- If cluster_name is provided, specified service will be managed on all ESXi host belonging to that cluster. |
| 19 |
- If specific esxi_hostname is provided, then specified service will be managed on given ESXi host only. |
| 20 |
author: |
| 21 |
- Abhijeet Kasurde (@Akasurde) |
| 22 |
options: |
| 23 |
cluster_name: |
| 24 |
description: |
| 25 |
- Name of the cluster. |
| 26 |
- Service settings are applied to every ESXi host system/s in given cluster. |
| 27 |
- If O(esxi_hostname) is not given, this parameter is required. |
| 28 |
type: str |
| 29 |
esxi_hostname: |
| 30 |
description: |
| 31 |
- ESXi hostname. |
| 32 |
- Service settings are applied to this ESXi host system. |
| 33 |
- If O(cluster_name) is not given, this parameter is required. |
| 34 |
type: str |
| 35 |
state: |
| 36 |
description: |
| 37 |
- Desired state of service. |
| 38 |
- V(start) and V(present) has same effect. |
| 39 |
- V(stop) and V(absent) has same effect. |
| 40 |
- V(unchanged) allows defining O(service_policy) without defining or changing service state. |
| 41 |
choices: [ absent, present, restart, start, stop, unchanged ] |
| 42 |
type: str |
| 43 |
default: 'start' |
| 44 |
service_policy: |
| 45 |
description: |
| 46 |
- Set of valid service policy strings. |
| 47 |
- If set V(on), then service should be started when the host starts up. |
| 48 |
- If set V(automatic), then service should run if and only if it has open firewall ports. |
| 49 |
- If set V(off), then Service should not be started when the host starts up. |
| 50 |
choices: [ 'automatic', 'off', 'on' ] |
| 51 |
type: str |
| 52 |
service_name: |
| 53 |
description: |
| 54 |
- Name of Service to be managed. This is a brief identifier for the service, for example, ntpd, vxsyslogd etc. |
| 55 |
- This value should be a valid ESXi service name. |
| 56 |
required: true |
| 57 |
type: str |
| 58 |
extends_documentation_fragment: |
| 59 |
- vmware.vmware.base_options |
| 60 |
|
| 61 |
''' |
| 62 |
|
| 63 |
EXAMPLES = r''' |
| 64 |
- name: Start ntpd service setting for all ESXi Host in given Cluster |
| 65 |
community.vmware.vmware_host_service_manager: |
| 66 |
hostname: '{{ vcenter_hostname }}' |
| 67 |
username: '{{ vcenter_username }}' |
| 68 |
password: '{{ vcenter_password }}' |
| 69 |
cluster_name: '{{ cluster_name }}' |
| 70 |
service_name: ntpd |
| 71 |
state: present |
| 72 |
delegate_to: localhost |
| 73 |
|
| 74 |
- name: Start ntpd setting for an ESXi Host |
| 75 |
community.vmware.vmware_host_service_manager: |
| 76 |
hostname: '{{ vcenter_hostname }}' |
| 77 |
username: '{{ vcenter_username }}' |
| 78 |
password: '{{ vcenter_password }}' |
| 79 |
esxi_hostname: '{{ esxi_hostname }}' |
| 80 |
service_name: ntpd |
| 81 |
state: present |
| 82 |
delegate_to: localhost |
| 83 |
|
| 84 |
- name: Start ntpd setting for an ESXi Host with Service policy |
| 85 |
community.vmware.vmware_host_service_manager: |
| 86 |
hostname: '{{ vcenter_hostname }}' |
| 87 |
username: '{{ vcenter_username }}' |
| 88 |
password: '{{ vcenter_password }}' |
| 89 |
esxi_hostname: '{{ esxi_hostname }}' |
| 90 |
service_name: ntpd |
| 91 |
service_policy: 'on' |
| 92 |
state: present |
| 93 |
delegate_to: localhost |
| 94 |
|
| 95 |
- name: Stop ntpd setting for an ESXi Host |
| 96 |
community.vmware.vmware_host_service_manager: |
| 97 |
hostname: '{{ vcenter_hostname }}' |
| 98 |
username: '{{ vcenter_username }}' |
| 99 |
password: '{{ vcenter_password }}' |
| 100 |
esxi_hostname: '{{ esxi_hostname }}' |
| 101 |
service_name: ntpd |
| 102 |
state: absent |
| 103 |
delegate_to: localhost |
| 104 |
''' |
| 105 |
|
| 106 |
RETURN = r'''# |
| 107 |
''' |
| 108 |
|
| 109 |
try: |
| 110 |
from pyVmomi import vim, vmodl |
| 111 |
except ImportError: |
| 112 |
pass |
| 113 |
|
| 114 |
from ansible.module_utils.basic import AnsibleModule |
| 115 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 116 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 117 |
from ansible.module_utils.common.text.converters import to_native |
| 118 |
|
| 119 |
|
| 120 |
class VmwareServiceManager(PyVmomi): |
| 121 |
def __init__(self, module): |
| 122 |
super(VmwareServiceManager, self).__init__(module) |
| 123 |
cluster_name = self.params.get('cluster_name', None) |
| 124 |
esxi_host_name = self.params.get('esxi_hostname', None) |
| 125 |
self.options = self.params.get('options', dict()) |
| 126 |
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name) |
| 127 |
self.desired_state = self.params.get('state') |
| 128 |
self.desired_policy = self.params.get('service_policy', None) |
| 129 |
self.service_name = self.params.get('service_name') |
| 130 |
self.results = {} |
| 131 |
|
| 132 |
def service_ctrl(self): |
| 133 |
changed = False |
| 134 |
host_service_state = [] |
| 135 |
for host in self.hosts: |
| 136 |
actual_service_state, actual_service_policy = self.check_service_state(host=host, service_name=self.service_name) |
| 137 |
host_service_system = host.configManager.serviceSystem |
| 138 |
if host_service_system: |
| 139 |
changed_state = False |
| 140 |
self.results[host.name] = dict(service_name=self.service_name, |
| 141 |
actual_service_state='running' if actual_service_state else 'stopped', |
| 142 |
actual_service_policy=actual_service_policy, |
| 143 |
desired_service_policy=self.desired_policy, |
| 144 |
desired_service_state=self.desired_state, |
| 145 |
error='', |
| 146 |
) |
| 147 |
try: |
| 148 |
if self.desired_state in ['start', 'present']: |
| 149 |
if not actual_service_state: |
| 150 |
if not self.module.check_mode: |
| 151 |
host_service_system.StartService(id=self.service_name) |
| 152 |
changed_state = True |
| 153 |
elif self.desired_state in ['stop', 'absent']: |
| 154 |
if actual_service_state: |
| 155 |
if not self.module.check_mode: |
| 156 |
host_service_system.StopService(id=self.service_name) |
| 157 |
changed_state = True |
| 158 |
elif self.desired_state == 'restart': |
| 159 |
if not self.module.check_mode: |
| 160 |
host_service_system.RestartService(id=self.service_name) |
| 161 |
changed_state = True |
| 162 |
|
| 163 |
if self.desired_policy: |
| 164 |
if actual_service_policy != self.desired_policy: |
| 165 |
if not self.module.check_mode: |
| 166 |
host_service_system.UpdateServicePolicy(id=self.service_name, |
| 167 |
policy=self.desired_policy) |
| 168 |
changed_state = True |
| 169 |
|
| 170 |
host_service_state.append(changed_state) |
| 171 |
self.results[host.name].update(changed=changed_state) |
| 172 |
except (vim.fault.InvalidState, vim.fault.NotFound, |
| 173 |
vim.fault.HostConfigFault, vmodl.fault.InvalidArgument) as e: |
| 174 |
self.results[host.name].update(changed=False, |
| 175 |
error=to_native(e.msg)) |
| 176 |
|
| 177 |
if any(host_service_state): |
| 178 |
changed = True |
| 179 |
self.module.exit_json(changed=changed, host_service_status=self.results) |
| 180 |
|
| 181 |
def check_service_state(self, host, service_name): |
| 182 |
host_service_system = host.configManager.serviceSystem |
| 183 |
if host_service_system: |
| 184 |
services = host_service_system.serviceInfo.service |
| 185 |
for service in services: |
| 186 |
if service.key == service_name: |
| 187 |
return service.running, service.policy |
| 188 |
|
| 189 |
msg = "Failed to find '%s' service on host system '%s'" % (service_name, host.name) |
| 190 |
cluster_name = self.params.get('cluster_name', None) |
| 191 |
if cluster_name: |
| 192 |
msg += " located on cluster '%s'" % cluster_name |
| 193 |
msg += ", please check if you have specified a valid ESXi service name." |
| 194 |
self.module.fail_json(msg=msg) |
| 195 |
|
| 196 |
|
| 197 |
def main(): |
| 198 |
argument_spec = base_argument_spec() |
| 199 |
argument_spec.update( |
| 200 |
cluster_name=dict(type='str', required=False), |
| 201 |
esxi_hostname=dict(type='str', required=False), |
| 202 |
state=dict(type='str', default='start', choices=['absent', 'present', 'restart', 'start', 'stop', 'unchanged']), |
| 203 |
service_name=dict(type='str', required=True), |
| 204 |
service_policy=dict(type='str', choices=['automatic', 'off', 'on']), |
| 205 |
) |
| 206 |
|
| 207 |
module = AnsibleModule( |
| 208 |
argument_spec=argument_spec, |
| 209 |
required_one_of=[ |
| 210 |
['cluster_name', 'esxi_hostname'], |
| 211 |
], |
| 212 |
supports_check_mode=True |
| 213 |
) |
| 214 |
|
| 215 |
vmware_host_service = VmwareServiceManager(module) |
| 216 |
vmware_host_service.service_ctrl() |
| 217 |
|
| 218 |
|
| 219 |
if __name__ == "__main__": |
| 220 |
main() |