Debian Codegraph @

ansible_collections/cisco/nxos/plugins/action/nxos_feature.py

raw
1 #
2 # (c) 2016 Red Hat Inc.
3 #
4 # This file is part of Ansible
5 #
6 # Ansible is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Ansible is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
18 #
19 from __future__ import absolute_import, division, print_function
20
21
22 __metaclass__ = type
23
24 from ansible.module_utils.connection import Connection
25 from ansible.utils.display import Display
26 from ansible_collections.ansible.netcommon.plugins.action.network import (
27 ActionModule as ActionNetworkModule,
28 )
29
30
31 display = Display()
32
33
34 class ActionModule(ActionNetworkModule):
35 def run(self, tmp=None, task_vars=None):
36 del tmp # tmp no longer has any effect
37
38 module_name = self._task.action.split(".")[-1]
39 self._config_module = True if module_name in ["nxos_config", "config"] else False
40 persistent_connection = self._play_context.connection.split(".")[-1]
41
42 if (self._play_context.connection == "httpapi") and module_name in (
43 "nxos_file_copy",
44 "nxos_nxapi",
45 ):
46 return {
47 "failed": True,
48 "msg": f"Connection httpapi is not valid for {module_name} module.",
49 }
50
51 if module_name == "nxos_file_copy":
52 # when file_pull is enabled, the file_pull_timeout and connect_ssh_port options
53 # will override persistent_command_timeout and port
54 # this has been kept for backwards compatibility till these options are removed
55 if persistent_connection != "network_cli":
56 return {
57 "failed": True,
58 "msg": (
59 f"Connection type must be fully qualified name for "
60 f"network_cli connection type, got {self._play_context.connection}"
61 ),
62 }
63
64 conn = Connection(self._connection.socket_path)
65 persistent_command_timeout = conn.get_option("persistent_command_timeout")
66 file_pull = self._task.args.get("file_pull", False)
67 file_pull_timeout = self._task.args.get("file_pull_timeout")
68 connect_ssh_port = self._task.args.get("connect_ssh_port", 22)
69
70 if file_pull:
71 # if file_pull_timeout is explicitly set, use that
72 if file_pull_timeout:
73 conn.set_option("persistent_command_timeout", file_pull_timeout)
74 # if file_pull_timeout is not set and command_timeout < 300s, bump to 300s.
75 elif persistent_command_timeout < 300:
76 conn.set_option("persistent_command_timeout", 300)
77 conn.set_option("port", connect_ssh_port)
78
79 if module_name == "nxos_install_os":
80 connection = self._connection
81 persistent_command_timeout = connection.get_option(
82 "persistent_command_timeout",
83 )
84 persistent_connect_timeout = connection.get_option(
85 "persistent_connect_timeout",
86 )
87
88 display.vvvv(
89 f"PERSISTENT_COMMAND_TIMEOUT is {persistent_command_timeout}",
90 self._play_context.remote_addr,
91 )
92 display.vvvv(
93 f"PERSISTENT_CONNECT_TIMEOUT is %s {persistent_connect_timeout}",
94 self._play_context.remote_addr,
95 )
96 if persistent_command_timeout < 600 or persistent_connect_timeout < 600:
97 msg = "PERSISTENT_COMMAND_TIMEOUT and PERSISTENT_CONNECT_TIMEOUT"
98 msg += " must be set to 600 seconds or higher when using nxos_install_os module."
99 msg += " Current persistent_command_timeout setting:" + str(
100 persistent_command_timeout,
101 )
102 msg += " Current persistent_connect_timeout setting:" + str(
103 persistent_connect_timeout,
104 )
105 return {"failed": True, "msg": msg}
106
107 if persistent_connection in ("network_cli", "httpapi"):
108 if module_name == "nxos_gir":
109 conn = Connection(self._connection.socket_path)
110 persistent_command_timeout = conn.get_option(
111 "persistent_command_timeout",
112 )
113 gir_timeout = 200
114 if persistent_command_timeout < gir_timeout:
115 conn.set_option("persistent_command_timeout", gir_timeout)
116 msg = f"timeout value extended to %ss for nxos_gir {gir_timeout}"
117 display.warning(msg)
118
119 else:
120 return {
121 "failed": True,
122 "msg": f"Connection type {self._play_context.connection} is not valid for this module",
123 }
124
125 return super(ActionModule, self).run(task_vars=task_vars)