From 3667197efb7b18ec842efd504785965911f8ac4b Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 5 Jun 2024 18:18:34 +0200 Subject: Adding upstream version 10.0.0+dfsg. Signed-off-by: Daniel Baumann --- .../junos/plugins/action/acl_interfaces.py | 168 +++++++++ .../junipernetworks/junos/plugins/action/acls.py | 168 +++++++++ .../junipernetworks/junos/plugins/action/banner.py | 168 +++++++++ .../junos/plugins/action/bgp_address_family.py | 168 +++++++++ .../junos/plugins/action/bgp_global.py | 168 +++++++++ .../junos/plugins/action/command.py | 168 +++++++++ .../junipernetworks/junos/plugins/action/config.py | 168 +++++++++ .../junipernetworks/junos/plugins/action/facts.py | 168 +++++++++ .../junos/plugins/action/hostname.py | 168 +++++++++ .../junos/plugins/action/interfaces.py | 168 +++++++++ .../junos/plugins/action/l2_interfaces.py | 168 +++++++++ .../junos/plugins/action/l3_intefaces.py | 168 +++++++++ .../junipernetworks/junos/plugins/action/lacp.py | 168 +++++++++ .../junos/plugins/action/lacp_intefaces.py | 168 +++++++++ .../junos/plugins/action/lag_interfaces.py | 168 +++++++++ .../junos/plugins/action/lldp_global.py | 168 +++++++++ .../junos/plugins/action/lldp_interfaces.py | 168 +++++++++ .../junos/plugins/action/logging.py | 168 +++++++++ .../junos/plugins/action/logging_global.py | 168 +++++++++ .../junos/plugins/action/netconf.py | 168 +++++++++ .../junos/plugins/action/ntp_global.py | 168 +++++++++ .../junos/plugins/action/ospf_interfaces.py | 168 +++++++++ .../junipernetworks/junos/plugins/action/ospfv2.py | 168 +++++++++ .../junipernetworks/junos/plugins/action/ospfv3.py | 168 +++++++++ .../junos/plugins/action/package.py | 168 +++++++++ .../junipernetworks/junos/plugins/action/ping.py | 168 +++++++++ .../junos/plugins/action/prefix_lists.py | 168 +++++++++ .../junos/plugins/action/routing_instances.py | 168 +++++++++ .../junos/plugins/action/routing_options.py | 168 +++++++++ .../junipernetworks/junos/plugins/action/rpc.py | 168 +++++++++ .../junipernetworks/junos/plugins/action/scp.py | 168 +++++++++ .../junos/plugins/action/security_policies.py | 168 +++++++++ .../plugins/action/security_policies_global.py | 168 +++++++++ .../junos/plugins/action/security_zones.py | 168 +++++++++ .../junos/plugins/action/snmp_server.py | 168 +++++++++ .../junos/plugins/action/static_routes.py | 168 +++++++++ .../junipernetworks/junos/plugins/action/system.py | 168 +++++++++ .../junipernetworks/junos/plugins/action/user.py | 168 +++++++++ .../junipernetworks/junos/plugins/action/vlans.py | 168 +++++++++ .../junipernetworks/junos/plugins/action/vrf.py | 168 +++++++++ .../junipernetworks/junos/plugins/cliconf/junos.py | 9 + .../junos/config/lldp_global/lldp_global.py | 55 +-- .../config/ospf_interfaces/ospf_interfaces.py | 2 +- .../module_utils/network/junos/facts/acls/acls.py | 5 +- .../network/junos/facts/lldp_global/lldp_global.py | 2 +- .../junos/facts/lldp_interfaces/lldp_interfaces.py | 2 +- .../junos/facts/logging_global/logging_global.py | 2 +- .../network/junos/facts/ntp_global/ntp_global.py | 3 +- .../junos/facts/ospf_interfaces/ospf_interfaces.py | 5 +- .../network/junos/facts/ospfv2/ospfv2.py | 3 +- .../network/junos/facts/ospfv3/ospfv3.py | 3 +- .../junos/facts/prefix_lists/prefix_lists.py | 3 +- .../facts/routing_instances/routing_instances.py | 3 +- .../junos/facts/routing_options/routing_options.py | 3 +- .../facts/security_policies/security_policies.py | 3 +- .../security_policies_global.py | 3 +- .../junos/facts/security_zones/security_zones.py | 3 +- .../network/junos/facts/snmp_server/snmp_server.py | 3 +- .../junos/facts/static_routes/static_routes.py | 3 +- .../network/junos/facts/vlans/vlans.py | 3 +- .../junos/plugins/modules/junos_config.py | 15 +- .../junos/plugins/modules/junos_l2_interfaces.py | 30 +- .../junos/plugins/modules/junos_l3_interfaces.py | 50 +-- .../junos/plugins/modules/junos_logging.py | 403 --------------------- 64 files changed, 6829 insertions(+), 507 deletions(-) create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/acl_interfaces.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/acls.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/banner.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/bgp_address_family.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/bgp_global.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/command.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/config.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/facts.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/hostname.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/interfaces.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/l2_interfaces.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/l3_intefaces.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/lacp.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/lacp_intefaces.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/lag_interfaces.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/lldp_global.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/lldp_interfaces.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/logging.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/logging_global.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/netconf.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/ntp_global.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/ospf_interfaces.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/ospfv2.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/ospfv3.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/package.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/ping.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/prefix_lists.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/routing_instances.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/routing_options.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/rpc.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/scp.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/security_policies.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/security_policies_global.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/security_zones.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/snmp_server.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/static_routes.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/system.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/user.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/vlans.py create mode 100644 ansible_collections/junipernetworks/junos/plugins/action/vrf.py delete mode 100644 ansible_collections/junipernetworks/junos/plugins/modules/junos_logging.py (limited to 'ansible_collections/junipernetworks/junos/plugins') diff --git a/ansible_collections/junipernetworks/junos/plugins/action/acl_interfaces.py b/ansible_collections/junipernetworks/junos/plugins/action/acl_interfaces.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/acl_interfaces.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/acls.py b/ansible_collections/junipernetworks/junos/plugins/action/acls.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/acls.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/banner.py b/ansible_collections/junipernetworks/junos/plugins/action/banner.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/banner.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/bgp_address_family.py b/ansible_collections/junipernetworks/junos/plugins/action/bgp_address_family.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/bgp_address_family.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/bgp_global.py b/ansible_collections/junipernetworks/junos/plugins/action/bgp_global.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/bgp_global.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/command.py b/ansible_collections/junipernetworks/junos/plugins/action/command.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/command.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/config.py b/ansible_collections/junipernetworks/junos/plugins/action/config.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/config.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/facts.py b/ansible_collections/junipernetworks/junos/plugins/action/facts.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/facts.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/hostname.py b/ansible_collections/junipernetworks/junos/plugins/action/hostname.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/hostname.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/interfaces.py b/ansible_collections/junipernetworks/junos/plugins/action/interfaces.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/interfaces.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/l2_interfaces.py b/ansible_collections/junipernetworks/junos/plugins/action/l2_interfaces.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/l2_interfaces.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/l3_intefaces.py b/ansible_collections/junipernetworks/junos/plugins/action/l3_intefaces.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/l3_intefaces.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/lacp.py b/ansible_collections/junipernetworks/junos/plugins/action/lacp.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/lacp.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/lacp_intefaces.py b/ansible_collections/junipernetworks/junos/plugins/action/lacp_intefaces.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/lacp_intefaces.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/lag_interfaces.py b/ansible_collections/junipernetworks/junos/plugins/action/lag_interfaces.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/lag_interfaces.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/lldp_global.py b/ansible_collections/junipernetworks/junos/plugins/action/lldp_global.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/lldp_global.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/lldp_interfaces.py b/ansible_collections/junipernetworks/junos/plugins/action/lldp_interfaces.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/lldp_interfaces.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/logging.py b/ansible_collections/junipernetworks/junos/plugins/action/logging.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/logging.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/logging_global.py b/ansible_collections/junipernetworks/junos/plugins/action/logging_global.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/logging_global.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/netconf.py b/ansible_collections/junipernetworks/junos/plugins/action/netconf.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/netconf.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/ntp_global.py b/ansible_collections/junipernetworks/junos/plugins/action/ntp_global.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/ntp_global.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/ospf_interfaces.py b/ansible_collections/junipernetworks/junos/plugins/action/ospf_interfaces.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/ospf_interfaces.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/ospfv2.py b/ansible_collections/junipernetworks/junos/plugins/action/ospfv2.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/ospfv2.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/ospfv3.py b/ansible_collections/junipernetworks/junos/plugins/action/ospfv3.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/ospfv3.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/package.py b/ansible_collections/junipernetworks/junos/plugins/action/package.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/package.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/ping.py b/ansible_collections/junipernetworks/junos/plugins/action/ping.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/ping.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/prefix_lists.py b/ansible_collections/junipernetworks/junos/plugins/action/prefix_lists.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/prefix_lists.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/routing_instances.py b/ansible_collections/junipernetworks/junos/plugins/action/routing_instances.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/routing_instances.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/routing_options.py b/ansible_collections/junipernetworks/junos/plugins/action/routing_options.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/routing_options.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/rpc.py b/ansible_collections/junipernetworks/junos/plugins/action/rpc.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/rpc.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/scp.py b/ansible_collections/junipernetworks/junos/plugins/action/scp.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/scp.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/security_policies.py b/ansible_collections/junipernetworks/junos/plugins/action/security_policies.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/security_policies.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/security_policies_global.py b/ansible_collections/junipernetworks/junos/plugins/action/security_policies_global.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/security_policies_global.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/security_zones.py b/ansible_collections/junipernetworks/junos/plugins/action/security_zones.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/security_zones.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/snmp_server.py b/ansible_collections/junipernetworks/junos/plugins/action/snmp_server.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/snmp_server.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/static_routes.py b/ansible_collections/junipernetworks/junos/plugins/action/static_routes.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/static_routes.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/system.py b/ansible_collections/junipernetworks/junos/plugins/action/system.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/system.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/user.py b/ansible_collections/junipernetworks/junos/plugins/action/user.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/user.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/vlans.py b/ansible_collections/junipernetworks/junos/plugins/action/vlans.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/vlans.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/action/vrf.py b/ansible_collections/junipernetworks/junos/plugins/action/vrf.py new file mode 100644 index 000000000..4c5d480af --- /dev/null +++ b/ansible_collections/junipernetworks/junos/plugins/action/vrf.py @@ -0,0 +1,168 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import copy +import sys + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + load_provider, +) + +from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( + junos_provider_spec, +) + + +display = Display() + +CLI_SUPPORTED_MODULES = ["junos_netconf", "junos_ping", "junos_command"] + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["junos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if self._play_context.connection == "local": + provider = load_provider(junos_provider_spec, self._task.args) + pc = copy.deepcopy(self._play_context) + pc.network_os = "junipernetworks.junos.junos" + pc.remote_addr = provider["host"] or self._play_context.remote_addr + + if provider["transport"] == "cli" and module_name not in CLI_SUPPORTED_MODULES: + return { + "failed": True, + "msg": "Transport type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (provider["transport"], module_name), + } + + if module_name == "junos_netconf" or ( + provider["transport"] == "cli" and module_name == "junos_command" + ): + pc.connection = "ansible.netcommon.network_cli" + pc.port = int( + provider["port"] or self._play_context.port or 22, + ) + else: + pc.connection = "ansible.netcommon.netconf" + pc.port = int( + provider["port"] or self._play_context.port or 830, + ) + + pc.remote_user = provider["username"] or self._play_context.connection_user + pc.password = provider["password"] or self._play_context.password + pc.private_key_file = provider["ssh_keyfile"] or self._play_context.private_key_file + + connection = self._shared_loader_obj.connection_loader.get( + "ansible.netcommon.persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + # TODO: Remove below code after ansible minimal is cut out + if connection is None: + pc.network_os = "junos" + if pc.connection.split(".")[-1] == "netconf": + pc.connection = "netconf" + else: + pc.connection = "network_cli" + + connection = self._shared_loader_obj.connection_loader.get( + "persistent", + pc, + sys.stdin, + task_uuid=self._task._uuid, + ) + + display.vvv( + "using connection plugin %s (was local)" % pc.connection, + pc.remote_addr, + ) + + command_timeout = ( + int(provider["timeout"]) + if provider["timeout"] + else connection.get_option("persistent_command_timeout") + ) + connection.set_options( + direct={"persistent_command_timeout": command_timeout}, + ) + + socket_path = connection.run() + display.vvvv("socket_path: %s" % socket_path, pc.remote_addr) + if not socket_path: + return { + "failed": True, + "msg": "unable to open shell. Please see: " + + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", + } + + task_vars["ansible_socket"] = socket_path + warnings.append( + [ + "connection local support for this module is deprecated and will be removed in version 2.14, use connection %s" + % pc.connection, + ], + ) + elif persistent_connection in ("netconf", "network_cli"): + provider = self._task.args.get("provider", {}) + if any(provider.values()): + # for legacy reasons provider value is required for junos_facts(optional) and junos_package + # modules as it uses junos_eznc library to connect to remote host + if not ( + module_name == "junos_facts" + or module_name == "junos_package" + or module_name == "junos_scp" + ): + display.warning( + "provider is unnecessary when using %s and will be ignored" + % self._play_context.connection, + ) + del self._task.args["provider"] + + if ( + persistent_connection == "network_cli" and module_name not in CLI_SUPPORTED_MODULES + ) or (persistent_connection == "netconf" and module_name in CLI_SUPPORTED_MODULES[0:2]): + return { + "failed": True, + "msg": "Connection type '%s' is not valid for '%s' module. " + "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" + % (self._play_context.connection, module_name), + } + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/ansible_collections/junipernetworks/junos/plugins/cliconf/junos.py b/ansible_collections/junipernetworks/junos/plugins/cliconf/junos.py index 75a2e23bd..4461c9cb0 100644 --- a/ansible_collections/junipernetworks/junos/plugins/cliconf/junos.py +++ b/ansible_collections/junipernetworks/junos/plugins/cliconf/junos.py @@ -272,6 +272,15 @@ class Cliconf(CliconfBase): self.discard_changes() return resp + @configure + def restore(self, filename=None, path=""): + if not filename: + raise ValueError("'file_name' value is required for restore") + cmd = f"load override {path}{filename}" + resp = self.send_command(cmd) + self.commit() + return resp + def get_diff(self, rollback_id=None): diff = {"config_diff": None} response = self.compare_configuration(rollback_id=rollback_id) diff --git a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/config/lldp_global/lldp_global.py b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/config/lldp_global/lldp_global.py index 5b0d5c78f..a92c6399c 100644 --- a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/config/lldp_global/lldp_global.py +++ b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/config/lldp_global/lldp_global.py @@ -239,31 +239,32 @@ class Lldp_global(ConfigBase): """ lldp_xml = [] - lldp_root = build_root_xml_node("lldp") - build_child_xml_node( - lldp_root, - "management-address", - None, - {"delete": "delete"}, - ) - build_child_xml_node( - lldp_root, - "advertisement-interval", - None, - {"delete": "delete"}, - ) - build_child_xml_node( - lldp_root, - "transmit-delay", - None, - {"delete": "delete"}, - ) - build_child_xml_node( - lldp_root, - "hold-multiplier", - None, - {"delete": "delete"}, - ) - build_child_xml_node(lldp_root, "disable", None, {"delete": "delete"}) - lldp_xml.append(lldp_root) + if have: + lldp_root = build_root_xml_node("lldp") + build_child_xml_node( + lldp_root, + "management-address", + None, + {"delete": "delete"}, + ) + build_child_xml_node( + lldp_root, + "advertisement-interval", + None, + {"delete": "delete"}, + ) + build_child_xml_node( + lldp_root, + "transmit-delay", + None, + {"delete": "delete"}, + ) + build_child_xml_node( + lldp_root, + "hold-multiplier", + None, + {"delete": "delete"}, + ) + build_child_xml_node(lldp_root, "disable", None, {"delete": "delete"}) + lldp_xml.append(lldp_root) return lldp_xml diff --git a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/config/ospf_interfaces/ospf_interfaces.py b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/config/ospf_interfaces/ospf_interfaces.py index 990cae77e..31df42c10 100644 --- a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/config/ospf_interfaces/ospf_interfaces.py +++ b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/config/ospf_interfaces/ospf_interfaces.py @@ -64,7 +64,7 @@ class Ospf_interfaces(ConfigBase): data=data, ) ospf_interfaces_facts = facts["ansible_network_resources"].get( - "junos_ospf_interfaces", + "ospf_interfaces", ) if not ospf_interfaces_facts: return [] diff --git a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/acls/acls.py b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/acls/acls.py index 7ff8fb2c3..65c81fe58 100644 --- a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/acls/acls.py +++ b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/acls/acls.py @@ -85,7 +85,7 @@ class AclsFacts(object): objs = [] for resource in resources: - if resource: + if resource is not None: xml = self._get_xml_dict(resource) for family, sub_dict in xml["firewall"]["family"].items(): sub_dict["family"] = family @@ -96,9 +96,8 @@ class AclsFacts(object): if obj: objs.append(obj) - facts = {} + facts = {"acls": []} if objs: - facts["acls"] = [] params = utils.validate_config( self.argument_spec, {"config": objs}, diff --git a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/lldp_global/lldp_global.py b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/lldp_global/lldp_global.py index 3b48e9720..c69a4cedd 100644 --- a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/lldp_global/lldp_global.py +++ b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/lldp_global/lldp_global.py @@ -80,7 +80,7 @@ class Lldp_globalFacts(object): to_bytes(data, errors="surrogate_then_replace"), ) - facts = {} + facts = {"lldp_global": {}} config = deepcopy(self.generated_spec) resources = data.xpath("configuration/protocols/lldp") if resources: diff --git a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/lldp_interfaces/lldp_interfaces.py b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/lldp_interfaces/lldp_interfaces.py index a5d1af467..7f6e48215 100644 --- a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/lldp_interfaces/lldp_interfaces.py +++ b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/lldp_interfaces/lldp_interfaces.py @@ -87,7 +87,7 @@ class Lldp_interfacesFacts(object): obj = self.render_config(self.generated_spec, resource) if obj: objs.append(obj) - facts = {} + facts = {"lldp_interfaces": []} if objs: facts["lldp_interfaces"] = [] params = utils.validate_config( diff --git a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/logging_global/logging_global.py b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/logging_global/logging_global.py index 2affbde19..0ca196bb9 100644 --- a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/logging_global/logging_global.py +++ b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/logging_global/logging_global.py @@ -99,7 +99,7 @@ class Logging_globalFacts(object): xml = self._get_xml_dict(resource) objs = self.render_config(self.generated_spec, xml) - facts = {} + facts = {"logging_global": {}} if objs: facts["logging_global"] = {} params = utils.validate_config( diff --git a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/ntp_global/ntp_global.py b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/ntp_global/ntp_global.py index a7dc37b57..82d1e0603 100644 --- a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/ntp_global/ntp_global.py +++ b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/ntp_global/ntp_global.py @@ -99,9 +99,8 @@ class Ntp_globalFacts(object): xml = self._get_xml_dict(resource) objs = self.render_config(self.generated_spec, xml) - facts = {} + facts = {"ntp_global": {}} if objs: - facts["ntp_global"] = {} params = utils.validate_config( self.argument_spec, {"config": objs}, diff --git a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/ospf_interfaces/ospf_interfaces.py b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/ospf_interfaces/ospf_interfaces.py index 55c162d25..bb37c5150 100644 --- a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/ospf_interfaces/ospf_interfaces.py +++ b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/ospf_interfaces/ospf_interfaces.py @@ -116,9 +116,8 @@ class Ospf_interfacesFacts(object): xml = self._get_xml_dict(resource) objs = self.render_config(self.generated_spec, xml) - facts = {} + facts = {"ospf_interfaces": []} if objs: - facts["junos_ospf_interfaces"] = [] params = _validate_config( self._module, self.argument_spec, @@ -127,7 +126,7 @@ class Ospf_interfacesFacts(object): ) for cfg in params["config"]: - facts["junos_ospf_interfaces"].append(remove_empties(cfg)) + facts["ospf_interfaces"].append(remove_empties(cfg)) ansible_facts["ansible_network_resources"].update(facts) return ansible_facts diff --git a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/ospfv2/ospfv2.py b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/ospfv2/ospfv2.py index 44bbb9732..70fdf40d2 100644 --- a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/ospfv2/ospfv2.py +++ b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/ospfv2/ospfv2.py @@ -122,9 +122,8 @@ class Ospfv2Facts(object): if obj: objs.append(obj) - facts = {} + facts = {"ospfv2": []} if objs is not None: - facts["ospfv2"] = [] params = utils.validate_config( self.argument_spec, {"config": objs}, diff --git a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/ospfv3/ospfv3.py b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/ospfv3/ospfv3.py index 8bbd04fcd..79bb61bd7 100644 --- a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/ospfv3/ospfv3.py +++ b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/ospfv3/ospfv3.py @@ -123,9 +123,8 @@ class Ospfv3Facts(object): if obj: objs.append(obj) - facts = {} + facts = {"ospfv3": []} if objs: - facts["ospfv3"] = [] params = utils.validate_config( self.argument_spec, {"config": objs}, diff --git a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/prefix_lists/prefix_lists.py b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/prefix_lists/prefix_lists.py index 4d3ec81c0..0f26ece3f 100644 --- a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/prefix_lists/prefix_lists.py +++ b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/prefix_lists/prefix_lists.py @@ -101,9 +101,8 @@ class Prefix_listsFacts(object): xml = self._get_xml_dict(resource) objs = self.render_config(self.generated_spec, xml) - facts = {} + facts = {"prefix_lists": []} if objs: - facts["prefix_lists"] = [] params = utils.validate_config( self.argument_spec, {"config": objs}, diff --git a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/routing_instances/routing_instances.py b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/routing_instances/routing_instances.py index d571e4e53..1766e4836 100644 --- a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/routing_instances/routing_instances.py +++ b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/routing_instances/routing_instances.py @@ -97,9 +97,8 @@ class Routing_instancesFacts(object): xml = self._get_xml_dict(resource) objs = self.render_config(self.generated_spec, xml) - facts = {} + facts = {"routing_instances": []} if objs: - facts["routing_instances"] = [] params = utils.validate_config( self.argument_spec, {"config": objs}, diff --git a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/routing_options/routing_options.py b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/routing_options/routing_options.py index 78664fea8..b0e45a82a 100644 --- a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/routing_options/routing_options.py +++ b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/routing_options/routing_options.py @@ -97,9 +97,8 @@ class Routing_optionsFacts(object): xml = self._get_xml_dict(resource) objs = self.render_config(self.generated_spec, xml) - facts = {} + facts = {"routing_options": []} if objs: - facts["routing_options"] = {} params = utils.validate_config( self.argument_spec, {"config": objs}, diff --git a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/security_policies/security_policies.py b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/security_policies/security_policies.py index 0c4d25e5e..c40c0f973 100644 --- a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/security_policies/security_policies.py +++ b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/security_policies/security_policies.py @@ -109,9 +109,8 @@ class Security_policiesFacts(object): xml = self._get_xml_dict(resource) objs = self.render_config(self.generated_spec, xml) - facts = {} + facts = {"security_policies": {}} if objs: - facts["security_policies"] = {} params = utils.validate_config( self.argument_spec, {"config": objs}, diff --git a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/security_policies_global/security_policies_global.py b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/security_policies_global/security_policies_global.py index 692a07b60..8fdb7e016 100644 --- a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/security_policies_global/security_policies_global.py +++ b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/security_policies_global/security_policies_global.py @@ -109,9 +109,8 @@ class Security_policies_globalFacts(object): xml = self._get_xml_dict(resource) objs = self.render_config(self.generated_spec, xml) - facts = {} + facts = {"security_policies_global": {}} if objs: - facts["security_policies_global"] = {} params = utils.validate_config( self.argument_spec, {"config": objs}, diff --git a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/security_zones/security_zones.py b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/security_zones/security_zones.py index 1e77168ac..61ca3f898 100644 --- a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/security_zones/security_zones.py +++ b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/security_zones/security_zones.py @@ -109,9 +109,8 @@ class Security_zonesFacts(object): xml = self._get_xml_dict(resource) objs = self.render_config(self.generated_spec, xml) - facts = {} + facts = {"security_zones": {}} if objs: - facts["security_zones"] = {} params = utils.validate_config( self.argument_spec, {"config": objs}, diff --git a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/snmp_server/snmp_server.py b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/snmp_server/snmp_server.py index b3c35bfec..52d228a16 100644 --- a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/snmp_server/snmp_server.py +++ b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/snmp_server/snmp_server.py @@ -97,9 +97,8 @@ class Snmp_serverFacts(object): xml = self._get_xml_dict(resource) objs = self.render_config(self.generated_spec, xml) - facts = {} + facts = {"snmp_server": {}} if objs: - facts["snmp_server"] = {} params = utils.validate_config( self.argument_spec, {"config": objs}, diff --git a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/static_routes/static_routes.py b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/static_routes/static_routes.py index 0be6f032d..e2a21e24b 100644 --- a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/static_routes/static_routes.py +++ b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/static_routes/static_routes.py @@ -95,9 +95,8 @@ class Static_routesFacts(object): if obj: objs.append(obj) - facts = {} + facts = {"static_routes": []} if objs: - facts["static_routes"] = [] params = utils.validate_config( self.argument_spec, {"config": objs}, diff --git a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/vlans/vlans.py b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/vlans/vlans.py index f4d35e635..43058f023 100644 --- a/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/vlans/vlans.py +++ b/ansible_collections/junipernetworks/junos/plugins/module_utils/network/junos/facts/vlans/vlans.py @@ -94,9 +94,8 @@ class VlansFacts(object): obj = self.render_config(self.generated_spec, resource) if obj: objs.append(obj) - facts = {} + facts = {"vlans": []} if objs: - facts["vlans"] = [] params = utils.validate_config( self.argument_spec, {"config": objs}, diff --git a/ansible_collections/junipernetworks/junos/plugins/modules/junos_config.py b/ansible_collections/junipernetworks/junos/plugins/modules/junos_config.py index 5b5151d39..c019e9c2b 100644 --- a/ansible_collections/junipernetworks/junos/plugins/modules/junos_config.py +++ b/ansible_collections/junipernetworks/junos/plugins/modules/junos_config.py @@ -66,9 +66,8 @@ options: confirm: description: - The C(confirm) argument will configure a time out value in minutes for the commit - to be confirmed before it is automatically rolled back. If the C(confirm) argument - is set to False, this argument is silently ignored. If the value for this argument - is set to 0, the commit is confirmed immediately. + to be confirmed before it is automatically rolled back. If the value for this argument + is set to 0, the commit is confirmed immediately which is also the default behaviour. type: int default: 0 comment: @@ -238,6 +237,16 @@ EXAMPLES = """ backup_options: filename: backup.cfg dir_path: /home/user + +- name: Set description with timer to confirm commit + junipernetworks.junos.junos_config: + lines: + - set interfaces fxp0 description "wait for a commit confirmation for 3 minutes; otherwise, it will be rolled back." + confirm: 3 + +- name: Perform confirm commit + junipernetworks.junos.junos_config: + confirm_commit: true """ RETURN = """ diff --git a/ansible_collections/junipernetworks/junos/plugins/modules/junos_l2_interfaces.py b/ansible_collections/junipernetworks/junos/plugins/modules/junos_l2_interfaces.py index bbd5f25b5..6a8424b40 100644 --- a/ansible_collections/junipernetworks/junos/plugins/modules/junos_l2_interfaces.py +++ b/ansible_collections/junipernetworks/junos/plugins/modules/junos_l2_interfaces.py @@ -330,11 +330,11 @@ EXAMPLES = """ - name: Override provided configuration with device configuration junipernetworks.junos.junos_l2_interfaces: config: - - name: ge-0/0/4 - trunk: - allowed_vlans: - - v101 - native_vlan: 30 + - name: ge-0/0/4 + trunk: + allowed_vlans: + - v101 + native_vlan: 30 state: overridden # Task Output @@ -494,14 +494,14 @@ EXAMPLES = """ - name: Replace provided configuration with device configuration junipernetworks.junos.junos_l2_interfaces: config: - - name: ge-0/0/3 - access: - vlan: v101 - - name: ge-0/0/4 - trunk: - allowed_vlans: - - vlan30 - native_vlan: 50 + - name: ge-0/0/3 + access: + vlan: v101 + - name: ge-0/0/4 + trunk: + allowed_vlans: + - vlan30 + native_vlan: 50 state: replaced # Task Output @@ -674,8 +674,8 @@ EXAMPLES = """ interface itself)." junipernetworks.junos.junos_l2_interfaces: config: - - name: ge-0/0/1 - - name: ge-0/0/2 + - name: ge-0/0/1 + - name: ge-0/0/2 state: deleted # Task Output diff --git a/ansible_collections/junipernetworks/junos/plugins/modules/junos_l3_interfaces.py b/ansible_collections/junipernetworks/junos/plugins/modules/junos_l3_interfaces.py index 3e9fd57b7..f57ffe0c8 100644 --- a/ansible_collections/junipernetworks/junos/plugins/modules/junos_l3_interfaces.py +++ b/ansible_collections/junipernetworks/junos/plugins/modules/junos_l3_interfaces.py @@ -132,14 +132,14 @@ EXAMPLES = """ - name: Merge provided configuration with device configuration junipernetworks.junos.junos_l3_interfaces: config: - - name: ge-0/0/1 - ipv4: - - address: 192.168.1.10/24 - ipv6: - - address: 8d8d:8d01::1/64 - - name: ge-0/0/2 - ipv4: - - address: dhcp + - name: ge-0/0/1 + ipv4: + - address: 192.168.1.10/24 + ipv6: + - address: 8d8d:8d01::1/64 + - name: ge-0/0/2 + ipv4: + - address: dhcp state: merged # Task Output @@ -265,13 +265,13 @@ EXAMPLES = """ - name: Override provided configuration with device configuration junipernetworks.junos.junos_l3_interfaces: config: - - name: ge-0/0/1 - ipv4: - - address: 192.168.1.10/24 - - ipv4: - - address: dhcp - name: fxp0 - unit: '0' + - name: ge-0/0/1 + ipv4: + - address: 192.168.1.10/24 + - ipv4: + - address: dhcp + name: fxp0 + unit: '0' state: overridden # Task Output @@ -434,14 +434,14 @@ EXAMPLES = """ - name: Replace provided configuration with device configuration junipernetworks.junos.junos_l3_interfaces: config: - - name: ge-0/0/1 - ipv4: - - address: 192.168.1.10/24 - ipv6: - - address: 8d8d:8d01::1/64 - - name: ge-0/0/2 - ipv4: - - address: dhcp + - name: ge-0/0/1 + ipv4: + - address: 192.168.1.10/24 + ipv6: + - address: 8d8d:8d01::1/64 + - name: ge-0/0/2 + ipv4: + - address: dhcp state: replaced # Task Output @@ -603,8 +603,8 @@ EXAMPLES = """ - name: Delete L3 logical interface junipernetworks.junos.junos_l3_interfaces: config: - - name: ge-0/0/1 - - name: ge-0/0/2 + - name: ge-0/0/1 + - name: ge-0/0/2 state: deleted # Task Output diff --git a/ansible_collections/junipernetworks/junos/plugins/modules/junos_logging.py b/ansible_collections/junipernetworks/junos/plugins/modules/junos_logging.py deleted file mode 100644 index 8f736d054..000000000 --- a/ansible_collections/junipernetworks/junos/plugins/modules/junos_logging.py +++ /dev/null @@ -1,403 +0,0 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- - -# (c) 2017, Ansible by Red Hat, inc -# 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 - - -DOCUMENTATION = """ -module: junos_logging -author: Ganesh Nalawade (@ganeshrn) -short_description: Manage logging on network devices -description: -- This module provides declarative management of logging on Juniper JUNOS devices. -version_added: 1.0.0 -extends_documentation_fragment: -- junipernetworks.junos.junos -deprecated: - alternative: junos_logging_global - why: Updated module released with more functionality. - removed_at_date: '2023-08-01' -options: - dest: - description: - - Destination of the logs. - choices: - - console - - host - - file - - user - type: str - name: - description: - - If value of C(dest) is I(file) it indicates file-name, for I(user) it indicates - username and for I(host) indicates the host name to be notified. - type: str - facility: - description: - - Set logging facility. - type: str - level: - description: - - Set logging severity levels. - type: str - aggregate: - description: - - List of logging definitions. - type: list - elements: dict - suboptions: - dest: - description: - - Destination of the logs. - choices: - - console - - host - - file - - user - type: str - name: - description: - - If value of C(dest) is I(file) it indicates file-name, for I(user) it indicates - username and for I(host) indicates the host name to be notified. - type: str - facility: - description: - - Set logging facility. - type: str - level: - description: - - Set logging severity levels. - type: str - state: - description: - - State of the logging configuration. - type: str - choices: - - present - - absent - active: - description: - - Specifies whether or not the configuration is active or deactivated - type: bool - rotate_frequency: - description: - - Rotate log frequency in minutes, this is applicable if value of I(dest) is C(file). - The acceptable value is in range of 1 to 59. This controls the frequency after - which log file is rotated. - type: int - required: false - size: - description: - - Size of the file in archive, this is applicable if value of I(dest) is C(file). - The acceptable value is in range from 65536 to 1073741824 bytes. - type: int - required: false - files: - description: - - Number of files to be archived, this is applicable if value of I(dest) is C(file). - The acceptable value is in range from 1 to 1000. - type: int - required: false - state: - description: - - State of the logging configuration. - default: present - type: str - choices: - - present - - absent - active: - description: - - Specifies whether or not the configuration is active or deactivated - default: true - type: bool - rotate_frequency: - description: - - Rotate log frequency in minutes, this is applicable if value of I(dest) is C(file). - The acceptable value is in range of 1 to 59. This controls the frequency after - which log file is rotated. - type: int - required: false - size: - description: - - Size of the file in archive, this is applicable if value of I(dest) is C(file). - The acceptable value is in range from 65536 to 1073741824 bytes. - required: false - type: int - files: - description: - - Number of files to be archived, this is applicable if value of I(dest) is C(file). - The acceptable value is in range from 1 to 1000. - type: int - required: false -requirements: -- ncclient (>=v0.5.2) -notes: -- This module requires the netconf system service be enabled on the remote device - being managed. -- Tested against vSRX JUNOS version 15.1X49-D15.4, vqfx-10000 JUNOS Version 15.1X53-D60.4. -- Recommended connection is C(netconf). See L(the Junos OS Platform Options,../network/user_guide/platform_junos.html). -- This module also works with C(local) connections for legacy playbooks. -""" - -EXAMPLES = """ -- name: configure console logging - junipernetworks.junos.junos_logging: - dest: console - facility: any - level: critical - -- name: remove console logging configuration - junipernetworks.junos.junos_logging: - dest: console - state: absent - -- name: configure file logging - junipernetworks.junos.junos_logging: - dest: file - name: test - facility: pfe - level: error - -- name: configure logging parameter - junipernetworks.junos.junos_logging: - files: 30 - size: 65536 - rotate_frequency: 10 - -- name: Configure file logging using aggregate - junipernetworks.junos.junos_logging: - dest: file - aggregate: - - name: test-1 - facility: pfe - level: critical - - name: test-2 - facility: kernel - level: emergency - active: true - -- name: Delete file logging using aggregate - junipernetworks.junos.junos_logging: - aggregate: - - {dest: file, name: test-1, facility: pfe, level: critical} - - {dest: file, name: test-2, facility: kernel, level: emergency} - state: absent -""" - -RETURN = """ -diff.prepared: - description: Configuration difference before and after applying change. - returned: when configuration is changed and diff option is enabled. - type: str - sample: > - [edit system syslog] - + [edit system syslog] - file interactive-commands { ... } - + file test { - + pfe critical; - + } -""" -import collections - -from copy import deepcopy - -from ansible.module_utils._text import to_text -from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.common.validation import check_required_if -from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( - remove_default_spec, -) - -from ansible_collections.junipernetworks.junos.plugins.module_utils.network.junos.junos import ( - commit_configuration, - discard_changes, - load_config, - locked_config, - map_obj_to_ele, - map_params_to_obj, - to_param_list, - tostring, -) - - -USE_PERSISTENT_CONNECTION = True - - -def validate_files(value, module): - if value and not 1 <= value <= 1000: - module.fail_json(msg="files must be between 1 and 1000") - - -def validate_size(value, module): - if value and not 65536 <= value <= 1073741824: - module.fail_json(msg="size must be between 65536 and 1073741824") - - -def validate_rotate_frequency(value, module): - if value and not 1 <= value <= 59: - module.fail_json(msg="rotate_frequency must be between 1 and 59") - - -def validate_param_values(module, obj, param=None): - if not param: - param = module.params - for key in obj: - # validate the param value (if validator func exists) - validator = globals().get("validate_%s" % key) - if callable(validator): - validator(param.get(key), module) - - -def main(): - """main entry point for module execution""" - element_spec = dict( - dest=dict(choices=["console", "host", "file", "user"]), - name=dict(), - facility=dict(), - level=dict(), - rotate_frequency=dict(type="int"), - size=dict(type="int"), - files=dict(type="int"), - state=dict(default="present", choices=["present", "absent"]), - active=dict(default=True, type="bool"), - ) - - aggregate_spec = deepcopy(element_spec) - - # remove default in aggregate spec, to handle common arguments - remove_default_spec(aggregate_spec) - - argument_spec = dict( - aggregate=dict(type="list", elements="dict", options=aggregate_spec), - ) - - argument_spec.update(element_spec) - - required_if = [ - ("dest", "host", ["name", "facility", "level"]), - ("dest", "file", ["name", "facility", "level"]), - ("dest", "user", ["name", "facility", "level"]), - ("dest", "console", ["facility", "level"]), - ] - - module = AnsibleModule( - argument_spec=argument_spec, - required_if=required_if, - supports_check_mode=True, - ) - - warnings = list() - result = {"changed": False} - - if warnings: - result["warnings"] = warnings - - params = to_param_list(module) - - requests = list() - for param in params: - # if key doesn't exist in the item, get it from module.params - for key in param: - if param.get(key) is None: - param[key] = module.params[key] - - try: - check_required_if(required_if, param) - except TypeError as exc: - module.fail_json(to_text(exc)) - - item = param.copy() - dest = item.get("dest") - if dest == "console" and item.get("name"): - module.fail_json( - msg="%s and %s are mutually exclusive" % ("console", "name"), - ) - - top = "system/syslog" - is_facility_key = False - field_top = None - if dest: - if dest == "console": - field_top = dest - is_facility_key = True - else: - field_top = dest + "/contents" - is_facility_key = False - - param_to_xpath_map = collections.OrderedDict() - param_to_xpath_map.update( - [ - ("name", {"xpath": "name", "is_key": True, "top": dest}), - ( - "facility", - { - "xpath": "name", - "is_key": is_facility_key, - "top": field_top, - }, - ), - ( - "size", - { - "xpath": "size", - "leaf_only": True, - "is_key": True, - "top": "archive", - }, - ), - ( - "files", - { - "xpath": "files", - "leaf_only": True, - "is_key": True, - "top": "archive", - }, - ), - ( - "rotate_frequency", - {"xpath": "log-rotate-frequency", "leaf_only": True}, - ), - ], - ) - - if item.get("level"): - param_to_xpath_map["level"] = { - "xpath": item.get("level"), - "tag_only": True, - "top": field_top, - } - - validate_param_values(module, param_to_xpath_map, param=item) - - want = map_params_to_obj(module, param_to_xpath_map, param=item) - requests.append(map_obj_to_ele(module, want, top, param=item)) - - diff = None - with locked_config(module): - for req in requests: - diff = load_config(module, tostring(req), warnings, action="merge") - - commit = not module.check_mode - if diff: - if commit: - commit_configuration(module) - else: - discard_changes(module) - result["changed"] = True - - if module._diff: - result["diff"] = {"prepared": diff} - - module.exit_json(**result) - - -if __name__ == "__main__": - main() -- cgit v1.2.3