-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ios_service: Initiale commit to create the module
ios_service
(#818)
* ios_service: Initiale commit to create the module `ios_service` in collection `cisco.ios` Has been tested again Cisco IOS-XE version 17.6.5 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Take in count the return: - config/service/service.py: - remove the list_parsers (only one list so not required) - facts/service/service.py - remove the `sort_list_dicts` as indicated - plugins/modules/ios_service.py - add samples commands I have forgotten - fixe some type - remove the `overridden` not present in this module * - add the unit test for the module ios_service - fixe some errors after the previous commit --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sagar Paul <[email protected]>
- Loading branch information
1 parent
4689ca4
commit ee4a40c
Showing
14 changed files
with
1,936 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
minor_changes: | ||
- ios_service - Create module to manage service configuration on IOS switches |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ios.py |
Empty file.
121 changes: 121 additions & 0 deletions
121
plugins/module_utils/network/ios/argspec/service/service.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2023 Red Hat | ||
# GNU General Public License v3.0+ | ||
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
from __future__ import absolute_import, division, print_function | ||
|
||
|
||
__metaclass__ = type | ||
|
||
############################################# | ||
# WARNING # | ||
############################################# | ||
# | ||
# This file is auto generated by the | ||
# ansible.content_builder. | ||
# | ||
# Manually editing this file is not advised. | ||
# | ||
# To update the argspec make the desired changes | ||
# in the documentation in the module file and re-run | ||
# ansible.content_builder commenting out | ||
# the path to external 'docstring' in build.yaml. | ||
# | ||
############################################## | ||
|
||
""" | ||
The arg spec for the ios_service module | ||
""" | ||
|
||
|
||
class ServiceArgs(object): # pylint: disable=R0903 | ||
"""The arg spec for the ios_service module""" | ||
|
||
argument_spec = { | ||
"config": { | ||
"options": { | ||
"call_home": {"type": "bool"}, | ||
"compress_config": {"type": "bool"}, | ||
"config": {"type": "bool"}, | ||
"counters": {"type": "int", "default": 0}, | ||
"dhcp": {"type": "bool", "default": True}, | ||
"disable_ip_fast_frag": {"type": "bool"}, | ||
"exec_callback": {"type": "bool"}, | ||
"exec_wait": {"type": "bool"}, | ||
"hide_telnet_addresses": {"type": "bool"}, | ||
"internal": {"type": "bool"}, | ||
"linenumber": {"type": "bool"}, | ||
"log": {"type": "bool"}, | ||
"log_hidden": {"type": "bool"}, | ||
"nagle": {"type": "bool"}, | ||
"old_slip_prompts": {"type": "bool"}, | ||
"pad": {"type": "bool"}, | ||
"pad_cmns": {"type": "bool"}, | ||
"pad_from_xot": {"type": "bool"}, | ||
"pad_to_xot": {"type": "bool"}, | ||
"password_encryption": {"type": "bool"}, | ||
"password_recovery": {"type": "bool", "default": True}, | ||
"prompt": {"type": "bool", "default": True}, | ||
"private_config_encryption": {"type": "bool"}, | ||
"pt_vty_logging": {"type": "bool"}, | ||
"scripting": {"type": "bool"}, | ||
"sequence_numbers": {"type": "bool"}, | ||
"slave_coredump": {"type": "bool"}, | ||
"slave_log": {"type": "bool", "default": True}, | ||
"tcp_keepalives_in": {"type": "bool"}, | ||
"tcp_keepalives_out": {"type": "bool"}, | ||
"tcp_small_servers": { | ||
"options": { | ||
"enable": {"type": "bool"}, | ||
"max_servers": {"type": "str"}, | ||
}, | ||
"type": "dict", | ||
}, | ||
"telnet_zeroidle": {"type": "bool"}, | ||
"timestamps": { | ||
"elements": "dict", | ||
"options": { | ||
"msg": {"choices": ["debug", "log"], "type": "str"}, | ||
"enable": {"type": "bool"}, | ||
"timestamp": { | ||
"choices": ["datetime", "uptime"], | ||
"type": "str", | ||
}, | ||
"datetime_options": { | ||
"options": { | ||
"localtime": {"type": "bool"}, | ||
"msec": {"type": "bool"}, | ||
"show_timezone": {"type": "bool"}, | ||
"year": {"type": "bool"}, | ||
}, | ||
"type": "dict", | ||
}, | ||
}, | ||
"type": "list", | ||
}, | ||
"udp_small_servers": { | ||
"options": { | ||
"enable": {"type": "bool"}, | ||
"max_servers": {"type": "str"}, | ||
}, | ||
"type": "dict", | ||
}, | ||
"unsupported_transceiver": {"type": "bool"}, | ||
}, | ||
"type": "dict", | ||
}, | ||
"running_config": {"type": "str"}, | ||
"state": { | ||
"choices": [ | ||
"merged", | ||
"replaced", | ||
"deleted", | ||
"gathered", | ||
"rendered", | ||
"parsed", | ||
], | ||
"default": "merged", | ||
"type": "str", | ||
}, | ||
} # pylint: disable=C0301 |
Empty file.
152 changes: 152 additions & 0 deletions
152
plugins/module_utils/network/ios/config/service/service.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
# | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2019 Red Hat Inc. | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
""" | ||
The ios_service class | ||
It is in this file where the current configuration (as dict) | ||
is compared to the provided configuration (as dict) and the command set | ||
necessary to bring the current configuration to its desired end-state is | ||
created | ||
""" | ||
|
||
from __future__ import absolute_import, division, print_function | ||
|
||
|
||
__metaclass__ = type | ||
|
||
from copy import deepcopy | ||
|
||
from ansible.module_utils.six import iteritems | ||
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module import ( | ||
ResourceModule, | ||
) | ||
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( | ||
dict_merge, | ||
) | ||
|
||
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.facts.facts import Facts | ||
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.rm_templates.service import ( | ||
ServiceTemplate, | ||
) | ||
|
||
|
||
class Service(ResourceModule): | ||
""" | ||
The ios_service class | ||
""" | ||
|
||
def __init__(self, module): | ||
super(Service, self).__init__( | ||
empty_fact_val={}, | ||
facts_module=Facts(module), | ||
module=module, | ||
resource="service", | ||
tmplt=ServiceTemplate(), | ||
) | ||
self.parsers = [ | ||
"call_home", | ||
"compress_config", | ||
"config", | ||
"counters", | ||
"dhcp", | ||
"disable_ip_fast_frag", | ||
"exec_callback", | ||
"exec_wait", | ||
"hide_telnet_addresses", | ||
"internal", | ||
"linenumber", | ||
"log", | ||
"log_hidden", | ||
"nagle", | ||
"old_slip_prompts", | ||
"pad", | ||
"pad_cmns", | ||
"pad_from_xot", | ||
"pad_to_xot", | ||
"password_encryption", | ||
"password_recovery", | ||
"private_config_encryption", | ||
"prompt", | ||
"pt_vty_logging", | ||
"scripting", | ||
"sequence_numbers", | ||
"slave_coredump", | ||
"slave_log", | ||
"tcp_keepalives_in", | ||
"tcp_keepalives_out", | ||
"telnet_zeroidle", | ||
"unsupported_transceiver", | ||
] | ||
|
||
def execute_module(self): | ||
"""Execute the module | ||
:rtype: A dictionary | ||
:returns: The result from module execution | ||
""" | ||
if self.state not in ["parsed", "gathered"]: | ||
self.generate_commands() | ||
self.run_commands() | ||
return self.result | ||
|
||
def generate_commands(self): | ||
"""Generate configuration commands to send based on | ||
want, have and desired state | ||
""" | ||
wantd = self._service_list_to_dict(self.want) | ||
haved = self._service_list_to_dict(self.have) | ||
|
||
service_default = { | ||
"counters": 0, | ||
"dhcp": True, | ||
"prompt": True, | ||
"slave_log": True, | ||
"password_recovery": True, | ||
"private_config_encryption": True, | ||
} | ||
|
||
# if state is merged, merge want onto have and then compare | ||
if self.state == "merged": | ||
wantd = dict_merge(haved, wantd) | ||
|
||
# if state is deleted, empty out wantd and set haved to wantd | ||
elif self.state == "deleted": | ||
wantd = self._service_list_to_dict(service_default) | ||
|
||
# if state is replaced | ||
elif self.state == "replaced": | ||
wantd = dict_merge(self._service_list_to_dict(service_default), wantd) | ||
|
||
self._compare(want=wantd, have=haved) | ||
|
||
def _compare(self, want, have): | ||
"""Leverages the base class `compare()` method and | ||
populates the list of commands to be run by comparing | ||
the `want` and `have` data with the `parsers` defined | ||
for the Service network resource. | ||
""" | ||
self.compare(parsers=self.parsers, want=want, have=have) | ||
self._compare_lists_attrs(want, have) | ||
|
||
def _compare_lists_attrs(self, want, have): | ||
"""Compare list of dict""" | ||
i_want = want.get("timestamps", {}) | ||
i_have = have.get("timestamps", {}) | ||
for key, wanting in iteritems(i_want): | ||
haveing = i_have.pop(key, {}) | ||
if wanting != haveing: | ||
self.addcmd(wanting, "timestamps") | ||
for key, haveing in iteritems(i_have): | ||
self.addcmd(haveing, "timestamps", negate=True) | ||
|
||
def _service_list_to_dict(self, data): | ||
"""Convert all list of dicts to dicts of dicts""" | ||
p_key = { | ||
"timestamps": "msg", | ||
} | ||
tmp_data = deepcopy(data) | ||
for k, _v in p_key.items(): | ||
if k in tmp_data: | ||
tmp_data[k] = {str(i[p_key.get(k)]): i for i in tmp_data[k]} | ||
return tmp_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2021 Red Hat | ||
# GNU General Public License v3.0+ | ||
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
from __future__ import absolute_import, division, print_function | ||
|
||
|
||
__metaclass__ = type | ||
|
||
""" | ||
The ios_service fact class | ||
It is in this file the configuration is collected from the device | ||
for a given resource, parsed, and the facts tree is populated | ||
based on the configuration. | ||
""" | ||
|
||
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common import utils | ||
|
||
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.argspec.service.service import ( | ||
ServiceArgs, | ||
) | ||
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.rm_templates.service import ( | ||
ServiceTemplate, | ||
) | ||
|
||
|
||
class ServiceFacts(object): | ||
"""The ios service facts class""" | ||
|
||
def __init__(self, module, subspec="config", options="options"): | ||
self._module = module | ||
self.argument_spec = ServiceArgs.argument_spec | ||
|
||
def get_service_data(self, connection): | ||
return connection.get("show running-config all | section ^service ") | ||
|
||
def populate_facts(self, connection, ansible_facts, data=None): | ||
"""Populate the facts for Service network resource | ||
:param connection: the device connection | ||
:param ansible_facts: Facts dictionary | ||
:param data: previously collected conf | ||
:rtype: dictionary | ||
:returns: facts | ||
""" | ||
facts = {} | ||
objs = [] | ||
params = {} | ||
|
||
if not data: | ||
data = self.get_service_data(connection) | ||
|
||
# parse native config using the ServiceTemplate | ||
service_parser = ServiceTemplate(lines=data.splitlines(), module=self._module) | ||
objs = service_parser.parse() | ||
|
||
ansible_facts["ansible_network_resources"].pop("service", None) | ||
|
||
params = utils.remove_empties( | ||
service_parser.validate_config(self.argument_spec, {"config": objs}, redact=True), | ||
) | ||
|
||
facts["service"] = params.get("config", {}) | ||
ansible_facts["ansible_network_resources"].update(facts) | ||
|
||
return ansible_facts |
Oops, something went wrong.