Skip to content

Commit

Permalink
os_user_global - Replace old module ios_user
Browse files Browse the repository at this point in the history
Replace the module ios_user with a more standard file coverage.
And also take correctly some parts of the commands (by example, the
current module doesn't take correctly the `privilege`part of the
username command
  • Loading branch information
earendilfr committed Aug 14, 2023
1 parent dcc812d commit baa3daf
Show file tree
Hide file tree
Showing 18 changed files with 1,241 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,6 @@ venv.bak/
*.code-workspace
.vscode/
.DS_Store
*.swp

changelogs/.plugin-cache.yaml
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Name | Description
[cisco.ios.ios_static_routes](https://github.com/ansible-collections/cisco.ios/blob/main/docs/cisco.ios.ios_static_routes_module.rst)|Resource module to configure static routes.
[cisco.ios.ios_system](https://github.com/ansible-collections/cisco.ios/blob/main/docs/cisco.ios.ios_system_module.rst)|Module to manage the system attributes.
[cisco.ios.ios_user](https://github.com/ansible-collections/cisco.ios/blob/main/docs/cisco.ios.ios_user_module.rst)|Module to manage the aggregates of local users.
[cisco.ios.ios_user_global](https://github.com/ansible-collections/cisco.ios/blob/main/docs/cisco.ios.ios_user_global_module.rst)|Resource module to configure user and enable
[cisco.ios.ios_vlans](https://github.com/ansible-collections/cisco.ios/blob/main/docs/cisco.ios.ios_vlans_module.rst)|Resource module to configure VLANs.
[cisco.ios.ios_vrf](https://github.com/ansible-collections/cisco.ios/blob/main/docs/cisco.ios.ios_vrf_module.rst)|Module to configure VRF definitions.

Expand Down
6 changes: 6 additions & 0 deletions changelogs/fragments/ios_user_global.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
minor_changes:
- ios_user_global - Put a module to replace the module ios_user with a files covered by standards (construct with `resource_module_models`)

bugfixes:
- gitignore - Add the line to not commit temporary files from VIM (`*.swp`)
2 changes: 1 addition & 1 deletion docs/cisco.ios.ios_facts_module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Parameters
<td>
</td>
<td>
<div>When supplied, this argument will restrict the facts collected to a given subset. Possible values for this argument include all and the resources like interfaces, vlans etc. Can specify a list of values to include a larger subset. Values can also be used with an initial <code>!</code> to specify that a specific subset should not be collected. Valid subsets are &#x27;bgp_global&#x27;, &#x27;l3_interfaces&#x27;, &#x27;lag_interfaces&#x27;, &#x27;ntp_global&#x27;, &#x27;acls&#x27;, &#x27;hostname&#x27;, &#x27;interfaces&#x27;, &#x27;lldp_interfaces&#x27;, &#x27;logging_global&#x27;, &#x27;ospf_interfaces&#x27;, &#x27;ospfv2&#x27;, &#x27;prefix_lists&#x27;, &#x27;static_routes&#x27;, &#x27;acl_interfaces&#x27;, &#x27;all&#x27;, &#x27;bgp_address_family&#x27;, &#x27;l2_interfaces&#x27;, &#x27;lacp&#x27;, &#x27;lacp_interfaces&#x27;, &#x27;lldp_global&#x27;, &#x27;ospfv3&#x27;, &#x27;snmp_server&#x27;, &#x27;vlans&#x27;, &#x27;service&#x27;.</div>
<div>When supplied, this argument will restrict the facts collected to a given subset. Possible values for this argument include all and the resources like interfaces, vlans etc. Can specify a list of values to include a larger subset. Values can also be used with an initial <code>!</code> to specify that a specific subset should not be collected. Valid subsets are &#x27;bgp_global&#x27;, &#x27;l3_interfaces&#x27;, &#x27;lag_interfaces&#x27;, &#x27;ntp_global&#x27;, &#x27;acls&#x27;, &#x27;hostname&#x27;, &#x27;interfaces&#x27;, &#x27;lldp_interfaces&#x27;, &#x27;logging_global&#x27;, &#x27;ospf_interfaces&#x27;, &#x27;ospfv2&#x27;, &#x27;prefix_lists&#x27;, &#x27;static_routes&#x27;, &#x27;acl_interfaces&#x27;, &#x27;all&#x27;, &#x27;bgp_address_family&#x27;, &#x27;l2_interfaces&#x27;, &#x27;lacp&#x27;, &#x27;lacp_interfaces&#x27;, &#x27;lldp_global&#x27;, &#x27;ospfv3&#x27;, &#x27;snmp_server&#x27;, &#x27;vlans&#x27;, &#x27;service&#x27;, &#x27;user_global&#x27;.</div>
</td>
</tr>
<tr>
Expand Down
6 changes: 6 additions & 0 deletions meta/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ plugin_routing:
redirect: cisco.ios.ios
ios_system:
redirect: cisco.ios.ios
ios_user_global:
redirect: cisco.ios.ios
l2_interfaces:
redirect: cisco.ios.ios
l3_interfaces:
Expand Down Expand Up @@ -107,6 +109,8 @@ plugin_routing:
redirect: cisco.ios.ios
user:
redirect: cisco.ios.ios
user_global:
redirect: cisco.ios.ios
vlans:
redirect: cisco.ios.ios
vrf:
Expand Down Expand Up @@ -202,6 +206,8 @@ plugin_routing:
redirect: cisco.ios.ios_system
user:
redirect: cisco.ios.ios_user
user_global:
redirect: cisco.ios.ios_user_global
vlans:
redirect: cisco.ios.ios_vlans
vrf:
Expand Down
1 change: 1 addition & 0 deletions plugins/action/user_global.py
Empty file.
104 changes: 104 additions & 0 deletions plugins/module_utils/network/ios/argspec/user_global/user_global.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# -*- 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_user_global module
"""


class User_globalArgs(object): # pylint: disable=R0903
"""The arg spec for the ios_user_global module"""

argument_spec = {
"config": {
"type": "dict",
"options": {
"enable": {
"elements": "dict",
"options": {
"password": {
"type": "dict",
"options": {
"type": {
"choices": ["password", "secret"],
"default": "secret",
"type": "str",
},
"hash": {
"choices": [0, 5, 6, 7, 8, 9],
"default": 0,
"type": "int",
},
"value": {"type": "str", "required": True},
},
"no_log": True,
},
"level": {"type": "int"},
},
"type": "list",
},
"users": {
"elements": "dict",
"options": {
"name": {"type": "str", "required": True},
"description": {"type": "str"},
"password": {
"type": "dict",
"options": {
"type": {
"choices": ["password", "secret"],
"default": "secret",
"type": "str",
},
"hash": {
"choices": [0, 5, 6, 7, 8, 9],
"default": 0,
"type": "int",
},
"value": {"type": "str", "required": True},
},
"no_log": True,
},
"privilege": {"type": "int"},
},
"type": "list",
},
},
},
"running_config": {"type": "str"},
"state": {
"choices": [
"merged",
"overridden",
"deleted",
"rendered",
"gathered",
"parsed",
],
"default": "merged",
"type": "str",
},
} # pylint: disable=C0301
Empty file.
120 changes: 120 additions & 0 deletions plugins/module_utils/network/ios/config/user_global/user_global.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#
# -*- 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

"""
The ios_user_global config file.
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 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.user_global import (
User_globalTemplate,
)


class User_global(ResourceModule):
"""
The ios_user_global config class
"""

def __init__(self, module):
super(User_global, self).__init__(
empty_fact_val={},
facts_module=Facts(module),
module=module,
resource="user_global",
tmplt=User_globalTemplate(),
)
self.parsers = []
self.list_parsers = [
"enable",
"users",
]

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._users_list_to_dict(self.want)
haved = self._users_list_to_dict(self.have)

# 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
if self.state == "deleted":
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 User_global 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"""
for _parser in self.list_parsers:
i_want = want.get(_parser, {})
i_have = have.get(_parser, {})
for key, wanting in iteritems(i_want):
haveing = i_have.pop(key, {})
if wanting != haveing:
if haveing and self.state in ["overridden", "replaced"]:
self.addcmd(haveing, _parser, negate=True)
self.addcmd(wanting, _parser)
for key, haveing in iteritems(i_have):
self.addcmd(haveing, _parser, negate=True)

def _users_list_to_dict(self, data):
"""Convert all list of dicts to dicts of dicts"""
p_key = {
"enable": "level",
"users": "name",
}
tmp_data = deepcopy(data)
for k, _v in p_key.items():
if k in tmp_data:
if k == "enable":
tmp_data[k] = {str(i.get(_v, 15)): i for i in tmp_data[k]}
else:
tmp_data[k] = {str(i[_v]): i for i in tmp_data[k]}
return tmp_data
4 changes: 4 additions & 0 deletions plugins/module_utils/network/ios/facts/facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.facts.static_routes.static_routes import (
Static_routesFacts,
)
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.facts.user_global.user_global import (
User_globalFacts,
)
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.facts.vlans.vlans import (
VlansFacts,
)
Expand Down Expand Up @@ -122,6 +125,7 @@
service=ServiceFacts,
snmp_server=Snmp_serverFacts,
hostname=HostnameFacts,
user_global=User_globalFacts,
)


Expand Down
Empty file.
84 changes: 84 additions & 0 deletions plugins/module_utils/network/ios/facts/user_global/user_global.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# -*- 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

"""
The ios user_global 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.user_global.user_global import (
User_globalArgs,
)
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.rm_templates.user_global import (
User_globalTemplate,
)


class User_globalFacts(object):
"""The ios user_global facts class"""

def __init__(self, module, subspec="config", options="options"):
self._module = module
self.argument_spec = User_globalArgs.argument_spec

def get_users_data(self, connection):
return connection.get("show running-config | section ^username|^enable")

def sort_list_dicts(self, objs):
p_key = {
"enable": "level",
"users": "name",
}
for k, _v in p_key.items():
if k in objs:
if k == "enable":
objs[k] = sorted(objs[k], key=lambda _k: str(_k.get(_v, 15)))
else:
objs[k] = sorted(objs[k], key=lambda _k: str(_k[_v]))
return objs

def populate_facts(self, connection, ansible_facts, data=None):
"""Populate the facts for User_global 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_users_data(connection)

# parse native config using the User_global template
user_global_parser = User_globalTemplate(lines=data.splitlines(), module=self._module)
objs = user_global_parser.parse()

if objs:
self.sort_list_dicts(objs)

ansible_facts["ansible_network_resources"].pop("user_global", None)

params = utils.remove_empties(
user_global_parser.validate_config(self.argument_spec, {"config": objs}, redact=True),
)

facts["user_global"] = params["config"]
ansible_facts["ansible_network_resources"].update(facts)

return ansible_facts
Loading

0 comments on commit baa3daf

Please sign in to comment.