Skip to content

Commit

Permalink
vmware_host_snmp: Add code to support SNMP values (#1103)
Browse files Browse the repository at this point in the history
vmware_host_snmp: Add code to support SNMP values

Depends-On: #1102
SUMMARY
Added code to support setting SNMP syscontact and syslocation.
Fixes #1044
ISSUE TYPE

Feature Pull Request

COMPONENT NAME
vmware_host_snmp.py
community.vmware.vmware_host_snmp_module.rst
ADDITIONAL INFORMATION

Reviewed-by: Mario Lenz <m@riolenz.de>
Reviewed-by: Abhijeet Kasurde <None>
Reviewed-by: Gonéri Le Bouder <goneri@lebouder.net>
Reviewed-by: None <None>
Reviewed-by: Tejaswi Bachu <teja.bachu@gmail.com>
  • Loading branch information
tejaswi-bachu committed Nov 17, 2021
1 parent 7964aff commit 3061ee7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- vmware_host_snmp - implement setting syscontact and syslocation (https://github.com/ansible-collections/community.vmware/issues/1044).
55 changes: 54 additions & 1 deletion plugins/modules/vmware_host_snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@
type: str
choices: [ debug, info, warning, error ]
default: info
sys_contact:
description:
- System contact who manages the system.
type: str
version_added: '1.17.0'
sys_location:
description:
- System location.
type: str
version_added: '1.17.0'
extends_documentation_fragment:
- community.vmware.vmware.documentation
Expand Down Expand Up @@ -107,6 +117,16 @@
state: enabled
delegate_to: localhost
- name: Enable and configure SNMP system contact and location
community.vmware.vmware_host_snmp:
hostname: '{{ esxi_hostname }}'
username: '{{ esxi_username }}'
password: '{{ esxi_password }}'
sys_contact: "admin@testemail.com"
sys_location: "Austin, USA"
state: enabled
delegate_to: localhost
- name: Disable SNMP
community.vmware.vmware_host_snmp:
hostname: '{{ esxi_hostname }}'
Expand Down Expand Up @@ -171,6 +191,8 @@ def ensure(self):
log_level = self.params.get("log_level")
send_trap = self.params.get("send_trap")
trap_filter = self.params.get("trap_filter")
sys_contact = self.params.get("sys_contact")
sys_location = self.params.get("sys_location")
event_filter = None
if trap_filter:
event_filter = ';'.join(trap_filter)
Expand Down Expand Up @@ -207,6 +229,10 @@ def ensure(self):
results['log_level_previous'] = option.value
if option.key == 'EventFilter' and option.value != hw_source:
results['trap_filter_previous'] = option.value.split(';')
if option.key == 'syscontact' and option.value != hw_source:
results['syscontact_previous'] = option.value
if option.key == 'syslocation' and option.value != hw_source:
results['syslocation_previous'] = option.value
# Build factory default config
destination = vim.host.SnmpSystem.SnmpConfigSpec.Destination()
destination.hostName = ""
Expand Down Expand Up @@ -315,6 +341,8 @@ def ensure(self):
results['log_level'] = log_level
results['trap_filter'] = trap_filter
event_filter_found = False
sys_contact_found = False
sys_location_found = False
if snmp_config_spec.option:
for option in snmp_config_spec.option:
if option.key == 'EnvEventSource' and option.value != hw_source:
Expand All @@ -334,6 +362,20 @@ def ensure(self):
changed_list.append("trap filter")
results['trap_filter_previous'] = option.value.split(';')
option.value = event_filter
if option.key == 'syscontact':
sys_contact_found = True
if sys_contact is not None and option.value != sys_contact:
changed = True
changed_list.append("sys contact")
results['sys_contact_previous'] = option.value
option.value = sys_contact
if option.key == 'syslocation':
sys_location_found = True
if sys_location is not None and option.value != sys_location:
changed = True
changed_list.append("sys location")
results['sys_location_previous'] = option.value
option.value = sys_location
if trap_filter and not event_filter_found:
changed = True
changed_list.append("trap filter")
Expand All @@ -351,7 +393,16 @@ def ensure(self):
# Doesn't work. Need to reset config instead
# snmp_config_spec.option = options
reset_hint = True

if sys_contact and not sys_contact_found:
changed = True
changed_list.append("sys contact")
results['sys_contact_previous'] = ''
snmp_config_spec.option.append(self.create_option('syscontact', sys_contact))
if sys_location and not sys_location_found:
changed = True
changed_list.append("sys location")
results['sys_location_previous'] = ''
snmp_config_spec.option.append(self.create_option('syslocation', sys_location))
if changed:
if snmp_state == 'reset':
if self.module.check_mode:
Expand Down Expand Up @@ -472,6 +523,8 @@ def main():
hw_source=dict(type='str', default='indications', choices=['indications', 'sensors']),
log_level=dict(type='str', default='info', choices=['debug', 'info', 'warning', 'error']),
send_trap=dict(type='bool', default=False),
sys_contact=dict(type='str'),
sys_location=dict(type='str')
)

module = AnsibleModule(
Expand Down
22 changes: 19 additions & 3 deletions tests/integration/targets/vmware_host_snmp/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@
- snmp_enabled is defined
- snmp_enabled.changed

- name: Enable and configure SNMP system contact and location
vmware_host_snmp:
hostname: '{{ esxi1 }}'
username: '{{ esxi_user }}'
password: '{{ esxi_password }}'
sys_contact: "admin@testemail.com"
sys_location: "Austin, USA"
state: enabled
validate_certs: false
register: snmp_enabled_sys_options
- debug: var=snmp_enabled_sys_options
- assert:
that:
- snmp_enabled_sys_options is defined
- snmp_enabled_sys_options.changed

- name: Disable SNMP
vmware_host_snmp:
hostname: '{{ esxi1 }}'
Expand All @@ -49,8 +65,8 @@
state: disabled
validate_certs: false
register: snmp_disabled
- debug: var=snmp_enabled
- debug: var=snmp_disabled
- assert:
that:
- snmp_enabled is defined
- snmp_enabled.changed
- snmp_disabled is defined
- snmp_disabled.changed

0 comments on commit 3061ee7

Please sign in to comment.