1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

VMware: Improve module vmware_host_config_manager (#46264)

* Improve module description
* Add check mode support and change message
This commit is contained in:
Christian Kotte 2018-10-12 11:46:12 +02:00 committed by Abhijeet Kasurde
parent 739a129322
commit ade5d938c3
2 changed files with 68 additions and 17 deletions

View file

@ -16,9 +16,9 @@ ANSIBLE_METADATA = {
DOCUMENTATION = r''' DOCUMENTATION = r'''
--- ---
module: vmware_host_config_manager module: vmware_host_config_manager
short_description: Manage advance configurations about an ESXi host short_description: Manage advanced system settings of an ESXi host
description: description:
- This module can be used to manage advance configuration information about an ESXi host when ESXi hostname or Cluster name is given. - This module can be used to manage advanced system settings of an ESXi host when ESXi hostname or Cluster name is given.
version_added: '2.5' version_added: '2.5'
author: author:
- Abhijeet Kasurde (@Akasurde) - Abhijeet Kasurde (@Akasurde)
@ -31,23 +31,23 @@ options:
cluster_name: cluster_name:
description: description:
- Name of the cluster. - Name of the cluster.
- Settings are applied to every ESXi host system in given cluster. - Settings are applied to every ESXi host in given cluster.
- If C(esxi_hostname) is not given, this parameter is required. - If C(esxi_hostname) is not given, this parameter is required.
esxi_hostname: esxi_hostname:
description: description:
- ESXi hostname. - ESXi hostname.
- Settings are applied to this ESXi host system. - Settings are applied to this ESXi host.
- If C(cluster_name) is not given, this parameter is required. - If C(cluster_name) is not given, this parameter is required.
options: options:
description: description:
- A dictionary of advance configuration parameters. - A dictionary of advanced system settings.
- Invalid options will cause module to error. - Invalid options will cause module to error.
default: {} default: {}
extends_documentation_fragment: vmware.documentation extends_documentation_fragment: vmware.documentation
''' '''
EXAMPLES = r''' EXAMPLES = r'''
- name: Manage Log level setting for all ESXi Host in given Cluster - name: Manage Log level setting for all ESXi hosts in given Cluster
vmware_host_config_manager: vmware_host_config_manager:
hostname: '{{ vcenter_hostname }}' hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}' username: '{{ vcenter_username }}'
@ -57,7 +57,7 @@ EXAMPLES = r'''
'Config.HostAgent.log.level': 'info' 'Config.HostAgent.log.level': 'info'
delegate_to: localhost delegate_to: localhost
- name: Manage Log level setting for an ESXi Host - name: Manage Log level setting for an ESXi host
vmware_host_config_manager: vmware_host_config_manager:
hostname: '{{ vcenter_hostname }}' hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}' username: '{{ vcenter_username }}'
@ -67,7 +67,7 @@ EXAMPLES = r'''
'Config.HostAgent.log.level': 'verbose' 'Config.HostAgent.log.level': 'verbose'
delegate_to: localhost delegate_to: localhost
- name: Manage multiple settings for an ESXi Host - name: Manage multiple settings for an ESXi host
vmware_host_config_manager: vmware_host_config_manager:
hostname: '{{ vcenter_hostname }}' hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}' username: '{{ vcenter_username }}'
@ -123,6 +123,7 @@ class VmwareConfigManager(PyVmomi):
return False return False
def set_host_configuration_facts(self): def set_host_configuration_facts(self):
changed_list = []
changed = False changed = False
for host in self.hosts: for host in self.hosts:
option_manager = host.configManager.advancedOption option_manager = host.configManager.advancedOption
@ -164,9 +165,22 @@ class VmwareConfigManager(PyVmomi):
if option_value != host_facts[option_key]['value']: if option_value != host_facts[option_key]['value']:
change_option_list.append(vim.option.OptionValue(key=option_key, value=option_value)) change_option_list.append(vim.option.OptionValue(key=option_key, value=option_value))
changed = True changed = True
changed_list.append(option_key)
else: # Don't silently drop unknown options. This prevents typos from falling through the cracks. else: # Don't silently drop unknown options. This prevents typos from falling through the cracks.
self.module.fail_json(msg="Unknown option %s" % option_key) self.module.fail_json(msg="Unknown option %s" % option_key)
if changed: if changed:
if self.module.check_mode:
changed_suffix = ' would be changed.'
else:
changed_suffix = ' changed.'
if len(changed_list) > 2:
message = ', '.join(changed_list[:-1]) + ', and ' + str(changed_list[-1])
elif len(changed_list) == 2:
message = ' and '.join(changed_list)
elif len(changed_list) == 1:
message = changed_list[0]
message += changed_suffix
if self.module.check_mode is False:
try: try:
option_manager.UpdateOptions(changedValue=change_option_list) option_manager.UpdateOptions(changedValue=change_option_list)
except (vmodl.fault.SystemError, vmodl.fault.InvalidArgument) as e: except (vmodl.fault.SystemError, vmodl.fault.InvalidArgument) as e:
@ -175,8 +189,10 @@ class VmwareConfigManager(PyVmomi):
except vim.fault.InvalidName as e: except vim.fault.InvalidName as e:
self.module.fail_json(msg="Failed to update option/s as one or more OptionValue " self.module.fail_json(msg="Failed to update option/s as one or more OptionValue "
"objects refers to a non-existent option : %s" % to_native(e.msg)) "objects refers to a non-existent option : %s" % to_native(e.msg))
else:
message = 'All settings are already configured.'
self.module.exit_json(changed=changed) self.module.exit_json(changed=changed, msg=message)
def main(): def main():
@ -189,6 +205,7 @@ def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec=argument_spec, argument_spec=argument_spec,
supports_check_mode=True,
required_one_of=[ required_one_of=[
['cluster_name', 'esxi_hostname'], ['cluster_name', 'esxi_hostname'],
] ]

View file

@ -86,3 +86,37 @@
assert: assert:
that: that:
- all_hosts_result.changed - all_hosts_result.changed
- name: Change facts about all hosts in given cluster in check mode
vmware_host_config_manager:
hostname: "{{ vcsim }}"
username: "{{ vcsim_instance.json.username }}"
password: "{{ vcsim_instance.json.password }}"
cluster_name: "{{ ccr1 }}"
options:
'Config.HostAgent.log.level': 'verbose'
validate_certs: no
register: all_hosts_result_check_mode
check_mode: yes
- name: ensure changes are done to all hosts
assert:
that:
- all_hosts_result_check_mode.changed
- name: Change facts about a given host in check mode
vmware_host_config_manager:
hostname: "{{ vcsim }}"
username: "{{ vcsim_instance.json.username }}"
password: "{{ vcsim_instance.json.password }}"
esxi_hostname: "{{ host1 }}"
options:
'Config.HostAgent.log.level': 'info'
validate_certs: no
register: host_result_check_mode
check_mode: yes
- name: ensure changes are done to given hosts
assert:
that:
- all_hosts_result_check_mode.changed