Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ios_users - Replace old module ios_user #909

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
fc60ee7
os_user_global - Replace old module ios_user
earendilfr Aug 11, 2023
87d8030
ios_user_global - feature: Take in count no username question
earendilfr Aug 15, 2023
f58ffc6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 23, 2023
3e4ed5d
Merge branch 'main' into update_ios_user_global
KB-perByte Sep 1, 2023
49876d4
Rename module from ios_user_global to ios_users
earendilfr Sep 1, 2023
c9554fa
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 1, 2023
d13b119
Merge branch 'main' into update_ios_user_global
KB-perByte Sep 7, 2023
3564a89
Take in count the case where no user or enable password is present on
earendilfr Sep 19, 2023
c6185d1
sync upstream/main
earendilfr Nov 27, 2023
add2a9e
Merge branch 'main' into update_ios_user_global
roverflow Dec 4, 2023
beaef26
Merge branch 'main' into update_ios_user_global
roverflow Dec 12, 2023
2d645e8
os_user_global - Replace old module ios_user
earendilfr Aug 11, 2023
5b6504d
ios_user_global - feature: Take in count no username question
earendilfr Aug 15, 2023
4cc9bb7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 23, 2023
c2faee5
Rename module from ios_user_global to ios_users
earendilfr Sep 1, 2023
cef52e8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 1, 2023
af746b3
Take in count the case where no user or enable password is present on
earendilfr Sep 19, 2023
8b3767e
Merge branch 'update_ios_user_global' of github.com:earendilfr/cisco.…
earendilfr Mar 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ Name | Description
[cisco.ios.ios_snmp_server](https://github.com/ansible-collections/cisco.ios/blob/main/docs/cisco.ios.ios_snmp_server_module.rst)|Resource module to configure snmp server.
[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](https://github.com/ansible-collections/cisco.ios/blob/main/docs/cisco.ios.ios_user_module.rst)|(deprecated, removed after 2024-10-01) Module to manage the aggregates of local users.
[cisco.ios.ios_users](https://github.com/ansible-collections/cisco.ios/blob/main/docs/cisco.ios.ios_users_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.
[cisco.ios.ios_vxlan_vtep](https://github.com/ansible-collections/cisco.ios/blob/main/docs/cisco.ios.ios_vxlan_vtep_module.rst)|Resource module to configure VXLAN VTEP interface.
Expand Down
9 changes: 9 additions & 0 deletions changelogs/fragments/ios_users.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
minor_changes:
- ios_users - 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`)

deprecated_features:
- ios_user - Indicate as deprecated and replaced by the module ios_users with more functionnality
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;users&#x27;.</div>
</td>
</tr>
<tr>
Expand Down
13 changes: 12 additions & 1 deletion docs/cisco.ios.ios_user_module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
cisco.ios.ios_user
******************

**Module to manage the aggregates of local users.**
**(deprecated, removed after 2024-10-01) Module to manage the aggregates of local users.**


Version added: 1.0.0
Expand All @@ -14,6 +14,13 @@ Version added: 1.0.0
:local:
:depth: 1

DEPRECATED
----------
:Removed in collection release after 2024-10-01
:Why: Updated module released with more functionality.
:Alternative: ios_users



Synopsis
--------
Expand Down Expand Up @@ -872,6 +879,10 @@ Status
------


- This module will be removed in a release after 2024-10-01. *[deprecated]*
- For more information see `DEPRECATED`_.


Authors
~~~~~~~

Expand Down
9 changes: 9 additions & 0 deletions meta/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ plugin_routing:
deprecation:
removal_date: "2024-01-01"
warning_text: See the plugin documentation for more details
ios_user:
deprecation:
removal_date: "2024-10-01"
warning_text: See the plugin documentation for more details
l2_interfaces:
redirect: cisco.ios.ios_l2_interfaces
l3_interfaces:
Expand Down Expand Up @@ -85,7 +89,12 @@ plugin_routing:
system:
redirect: cisco.ios.ios_system
user:
deprecation:
removal_date: "2024-10-01"
warning_text: See the plugin documentation for more details
redirect: cisco.ios.ios_user
users:
redirect: cisco.ios.ios_users
vlans:
redirect: cisco.ios.ios_vlans
vrf:
Expand Down
1 change: 1 addition & 0 deletions plugins/action/users.py
Empty file.
115 changes: 115 additions & 0 deletions plugins/module_utils/network/ios/argspec/users/users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# -*- 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_users module
"""


class UsersArgs(object): # pylint: disable=R0903
"""The arg spec for the ios_users 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", "no_log": True},
},
"no_log": False,
},
"level": {"type": "int"},
},
"type": "list",
},
"users": {
"elements": "dict",
"options": {
"name": {"type": "str", "required": True},
"command": {
"choices": ["new", "old"],
"default": "old",
"type": "str",
},
"parameters": {
"type": "dict",
"options": {
"nopassword": {"type": "bool"},
"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", "no_log": True},
},
"no_log": False,
},
"privilege": {"type": "int"},
"view": {"type": "str"},
},
},
},
"type": "list",
},
},
},
"running_config": {"type": "str"},
"state": {
"choices": [
"merged",
"overridden",
"deleted",
"rendered",
"gathered",
"parsed",
],
"default": "merged",
"type": "str",
},
} # pylint: disable=C0301
Empty file.
174 changes: 174 additions & 0 deletions plugins/module_utils/network/ios/config/users/users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#
# -*- 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_users 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.users import (
UsersTemplate,
)


class Users(ResourceModule):
"""
The ios_users config class
"""

def __init__(self, module):
super(Users, self).__init__(
empty_fact_val={},
facts_module=Facts(module),
module=module,
resource="users",
tmplt=UsersTemplate(),
)
self.parsers = []
self.list_parsers = [
"enable",
]
self.list_complex_parsers = {
"old": "users",
"new": "user-name",
}

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 Users network resource.
"""
self.compare(parsers=self.parsers, want=want, have=have)
self._compare_lists_attrs(want, have)
self._compare_list_complex_parsers(want=want.get("users", {}), have=have.get("users", {}))

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 _compare_list_complex_parsers(self, want, have):
"""Compare complex dict users with two different parsers"""
for key, wanting in iteritems(want):
haveing = have.pop(key, {})
w_command = wanting.get("command", "old")
w_parser = self.list_complex_parsers[w_command]
h_command = haveing.get("command", "old")
h_parser = self.list_complex_parsers[h_command]
if wanting != haveing:
if haveing and (
w_command != h_command
or h_command == "old"
and self.state in ["overridden", "replaced"]
):
self.addcmd(
haveing,
h_parser + ".name" if h_command == "new" else h_parser,
negate=True,
)
if wanting["command"] == "new":
self.addcmd(wanting, w_parser + ".name")
for k in wanting["parameters"].keys():
self.addcmd(wanting, w_parser + "." + k)
else:
self.addcmd(wanting, w_parser)
elif haveing and w_command == h_command and w_command == "new":
self.addcmd(wanting, w_parser + ".name")
for k, k_wanting in wanting["parameters"].items():
k_haveing = haveing["parameters"].pop(k, None)
if not k_haveing or k_wanting != k_haveing:
self.addcmd(wanting, w_parser + "." + k)
if self.state in ["overridden", "replaced"]:
for k, k_haveing in haveing["parameters"].items():
self.addcmd(haveing, h_parser + "." + k, negate=True)
else:
if w_command == "new":
self.addcmd(wanting, w_parser + ".name")
for k in wanting["parameters"].keys():
self.addcmd(wanting, w_parser + "." + k)
else:
self.addcmd(wanting, w_parser)
for key, haveing in have.items():
h_command = haveing.get("command", "old")
h_parser = self.list_complex_parsers[h_command]
self.addcmd(
haveing,
h_parser + ".name" if h_command == "new" else h_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
Loading
Loading