mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-24 22:00:22 -07:00
Refactoring to persistence connection BGP, factory, reload, save, showrun modules (#43534)
* Refactoring to persistence connection BGP, factory, reload, save, showrun modules * Refactoring methods from Util to module file * Removing BGP Utility methods * Adding to errors that need to be ignored
This commit is contained in:
parent
32c01644d4
commit
119376a685
8 changed files with 1072 additions and 1114 deletions
File diff suppressed because it is too large
Load diff
|
@ -33,22 +33,26 @@ DOCUMENTATION = '''
|
|||
---
|
||||
module: cnos_factory
|
||||
author: "Anil Kumar Muraleedharan (@amuraleedhar)"
|
||||
short_description: Reset the switch's startup configuration to default (factory) on devices running Lenovo CNOS
|
||||
short_description: Reset the switch startup configuration to default (factory)
|
||||
on devices running Lenovo CNOS.
|
||||
description:
|
||||
- This module allows you to reset a switch's startup configuration. The method provides a way to reset the
|
||||
startup configuration to its factory settings. This is helpful when you want to move the switch to another
|
||||
topology as a new network device.
|
||||
This module uses SSH to manage network device configuration.
|
||||
The results of the operation can be viewed in results directory.
|
||||
For more information about this module from Lenovo and customizing it usage for your
|
||||
use cases, please visit U(http://systemx.lenovofiles.com/help/index.jsp?topic=%2Fcom.lenovo.switchmgt.ansible.doc%2Fcnos_factory.html)
|
||||
- This module allows you to reset a switch's startup configuration. The
|
||||
method provides a way to reset the startup configuration to its factory
|
||||
settings. This is helpful when you want to move the switch to another
|
||||
topology as a new network device. This module uses SSH to manage network
|
||||
device configuration. The result of the operation can be viewed in results
|
||||
directory.
|
||||
For more information about this module and customizing it usage
|
||||
for your use cases, please visit
|
||||
U(http://systemx.lenovofiles.com/help/index.jsp?topic=%2Fcom.lenovo.switchmgt.ansible.doc%2Fcnos_factory.html)
|
||||
version_added: "2.3"
|
||||
extends_documentation_fragment: cnos
|
||||
options: {}
|
||||
|
||||
'''
|
||||
EXAMPLES = '''
|
||||
Tasks : The following are examples of using the module cnos_reload. These are written in the main.yml file of the tasks directory.
|
||||
Tasks : The following are examples of using the module cnos_reload. These are
|
||||
written in the main.yml file of the tasks directory.
|
||||
---
|
||||
- name: Test Reset to factory
|
||||
cnos_factory:
|
||||
|
@ -68,11 +72,6 @@ msg:
|
|||
'''
|
||||
|
||||
import sys
|
||||
try:
|
||||
import paramiko
|
||||
HAS_PARAMIKO = True
|
||||
except ImportError:
|
||||
HAS_PARAMIKO = False
|
||||
import time
|
||||
import socket
|
||||
import array
|
||||
|
@ -99,44 +98,11 @@ def main():
|
|||
deviceType=dict(required=True),),
|
||||
supports_check_mode=False)
|
||||
|
||||
username = module.params['username']
|
||||
password = module.params['password']
|
||||
enablePassword = module.params['enablePassword']
|
||||
cliCommand = "save erase \n"
|
||||
command = 'write erase'
|
||||
outputfile = module.params['outputfile']
|
||||
hostIP = module.params['host']
|
||||
deviceType = module.params['deviceType']
|
||||
output = ""
|
||||
if not HAS_PARAMIKO:
|
||||
module.fail_json(msg='paramiko is required for this module')
|
||||
|
||||
# Create instance of SSHClient object
|
||||
remote_conn_pre = paramiko.SSHClient()
|
||||
|
||||
# Automatically add untrusted hosts (make sure okay for security policy in your environment)
|
||||
remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
|
||||
# initiate SSH connection with the switch
|
||||
remote_conn_pre.connect(hostIP, username=username, password=password)
|
||||
time.sleep(2)
|
||||
|
||||
# Use invoke_shell to establish an 'interactive session'
|
||||
remote_conn = remote_conn_pre.invoke_shell()
|
||||
time.sleep(2)
|
||||
|
||||
# Enable and enter configure terminal then send command
|
||||
output = output + cnos.waitForDeviceResponse("\n", ">", 2, remote_conn)
|
||||
|
||||
output = output + cnos.enterEnableModeForDevice(enablePassword, 3, remote_conn)
|
||||
|
||||
# Make terminal length = 0
|
||||
output = output + cnos.waitForDeviceResponse("terminal length 0\n", "#", 2, remote_conn)
|
||||
|
||||
# cnos.debugOutput(cliCommand)
|
||||
# Send the CLi command
|
||||
output = output + cnos.waitForDeviceResponse(cliCommand, "[n]", 2, remote_conn)
|
||||
|
||||
output = output + cnos.waitForDeviceResponse("y" + "\n", "#", 2, remote_conn)
|
||||
output = ''
|
||||
cmd = [{'command': command, 'prompt': '[n]', 'answer': 'y'}]
|
||||
output = output + str(cnos.run_cnos_commands(module, cmd))
|
||||
|
||||
# Save it into the file
|
||||
file = open(outputfile, "a")
|
||||
|
@ -145,7 +111,8 @@ def main():
|
|||
|
||||
errorMsg = cnos.checkOutputForError(output)
|
||||
if(errorMsg is None):
|
||||
module.exit_json(changed=True, msg="Switch Startup Config is Reset to factory settings ")
|
||||
module.exit_json(changed=True,
|
||||
msg="Switch Startup Config is Reset to Factory settings")
|
||||
else:
|
||||
module.fail_json(msg=errorMsg)
|
||||
|
||||
|
|
|
@ -35,19 +35,22 @@ module: cnos_reload
|
|||
author: "Anil Kumar Muraleedharan (@amuraleedhar)"
|
||||
short_description: Perform switch restart on devices running Lenovo CNOS
|
||||
description:
|
||||
- This module allows you to restart the switch using the current startup configuration.
|
||||
The module is usually invoked after the running configuration has been saved over the startup configuration.
|
||||
- This module allows you to restart the switch using the current startup
|
||||
configuration. The module is usually invoked after the running
|
||||
configuration has been saved over the startup configuration.
|
||||
This module uses SSH to manage network device configuration.
|
||||
The results of the operation can be viewed in results directory.
|
||||
For more information about this module from Lenovo and customizing it usage for your
|
||||
use cases, please visit U(http://systemx.lenovofiles.com/help/index.jsp?topic=%2Fcom.lenovo.switchmgt.ansible.doc%2Fcnos_reload.html)
|
||||
For more information about this module and customizing it usage
|
||||
for your use cases, please visit
|
||||
U(http://systemx.lenovofiles.com/help/index.jsp?topic=%2Fcom.lenovo.switchmgt.ansible.doc%2Fcnos_reload.html)
|
||||
version_added: "2.3"
|
||||
extends_documentation_fragment: cnos
|
||||
options: {}
|
||||
|
||||
'''
|
||||
EXAMPLES = '''
|
||||
Tasks : The following are examples of using the module cnos_reload. These are written in the main.yml file of the tasks directory.
|
||||
Tasks : The following are examples of using the module cnos_reload. These are
|
||||
written in the main.yml file of the tasks directory.
|
||||
---
|
||||
- name: Test Reload
|
||||
cnos_reload:
|
||||
|
@ -67,11 +70,6 @@ msg:
|
|||
'''
|
||||
|
||||
import sys
|
||||
try:
|
||||
import paramiko
|
||||
HAS_PARAMIKO = True
|
||||
except ImportError:
|
||||
HAS_PARAMIKO = False
|
||||
import time
|
||||
import socket
|
||||
import array
|
||||
|
@ -98,44 +96,12 @@ def main():
|
|||
deviceType=dict(required=True),),
|
||||
supports_check_mode=False)
|
||||
|
||||
username = module.params['username']
|
||||
password = module.params['password']
|
||||
enablePassword = module.params['enablePassword']
|
||||
cliCommand = "reload \n"
|
||||
command = 'reload'
|
||||
outputfile = module.params['outputfile']
|
||||
hostIP = module.params['host']
|
||||
deviceType = module.params['deviceType']
|
||||
output = ""
|
||||
if not HAS_PARAMIKO:
|
||||
module.fail_json(msg='paramiko is required for this module')
|
||||
|
||||
# Create instance of SSHClient object
|
||||
remote_conn_pre = paramiko.SSHClient()
|
||||
|
||||
# Automatically add untrusted hosts (make sure okay for security policy in your environment)
|
||||
remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
|
||||
# initiate SSH connection with the switch
|
||||
remote_conn_pre.connect(hostIP, username=username, password=password)
|
||||
time.sleep(2)
|
||||
|
||||
# Use invoke_shell to establish an 'interactive session'
|
||||
remote_conn = remote_conn_pre.invoke_shell()
|
||||
time.sleep(2)
|
||||
|
||||
# Enable and then send command
|
||||
output = output + cnos.waitForDeviceResponse("\n", ">", 2, remote_conn)
|
||||
|
||||
output = output + cnos.enterEnableModeForDevice(enablePassword, 3, remote_conn)
|
||||
|
||||
# Make terminal length = 0
|
||||
output = output + cnos.waitForDeviceResponse("terminal length 0\n", "#", 2, remote_conn)
|
||||
|
||||
# Send the CLi command
|
||||
output = output + cnos.waitForDeviceResponse(cliCommand, "(y/n):", 2, remote_conn)
|
||||
|
||||
# Send the Confirmation y
|
||||
output = output + cnos.waitForDeviceResponse("y\n", "#", 2, remote_conn)
|
||||
output = ''
|
||||
cmd = [{'command': command, 'prompt': 'reboot system? (y/n): ',
|
||||
'answer': 'y'}]
|
||||
output = output + str(cnos.run_cnos_commands(module, cmd))
|
||||
|
||||
# Save it into the file
|
||||
file = open(outputfile, "a")
|
||||
|
@ -144,7 +110,8 @@ def main():
|
|||
|
||||
errorMsg = cnos.checkOutputForError(output)
|
||||
if(errorMsg in "Device Response Timed out"):
|
||||
module.exit_json(changed=True, msg="Device is Reloading. Please wait...")
|
||||
module.exit_json(changed=True,
|
||||
msg="Device is Reloading. Please wait...")
|
||||
else:
|
||||
module.fail_json(msg=errorMsg)
|
||||
|
||||
|
|
|
@ -33,22 +33,26 @@ DOCUMENTATION = '''
|
|||
---
|
||||
module: cnos_save
|
||||
author: "Anil Kumar Muraleedharan (@amuraleedhar)"
|
||||
short_description: Save the running configuration as the startup configuration on devices running Lenovo CNOS
|
||||
short_description: Save the running configuration as the startup configuration
|
||||
on devices running Lenovo CNOS
|
||||
description:
|
||||
- This module allows you to copy the running configuration of a switch over its startup configuration.
|
||||
It is recommended to use this module shortly after any major configuration changes so they persist after
|
||||
a switch restart. This module uses SSH to manage network device configuration.
|
||||
- This module allows you to copy the running configuration of a switch over
|
||||
its startup configuration. It is recommended to use this module shortly
|
||||
after any major configuration changes so they persist after a switch
|
||||
restart. This module uses SSH to manage network device configuration.
|
||||
The results of the operation will be placed in a directory named 'results'
|
||||
that must be created by the user in their local directory to where the playbook is run.
|
||||
For more information about this module from Lenovo and customizing it usage for your
|
||||
use cases, please visit U(http://systemx.lenovofiles.com/help/index.jsp?topic=%2Fcom.lenovo.switchmgt.ansible.doc%2Fcnos_save.html)
|
||||
that must be created by the user in their local directory to where the
|
||||
playbook is run. For more information about this module from Lenovo and
|
||||
customizing it usage for your use cases, please visit
|
||||
U(http://systemx.lenovofiles.com/help/index.jsp?topic=%2Fcom.lenovo.switchmgt.ansible.doc%2Fcnos_save.html)
|
||||
version_added: "2.3"
|
||||
extends_documentation_fragment: cnos
|
||||
options: {}
|
||||
|
||||
'''
|
||||
EXAMPLES = '''
|
||||
Tasks : The following are examples of using the module cnos_save. These are written in the main.yml file of the tasks directory.
|
||||
Tasks : The following are examples of using the module cnos_save. These are
|
||||
written in the main.yml file of the tasks directory.
|
||||
---
|
||||
- name: Test Save
|
||||
cnos_save:
|
||||
|
@ -68,11 +72,6 @@ msg:
|
|||
'''
|
||||
|
||||
import sys
|
||||
try:
|
||||
import paramiko
|
||||
HAS_PARAMIKO = True
|
||||
except ImportError:
|
||||
HAS_PARAMIKO = False
|
||||
import time
|
||||
import socket
|
||||
import array
|
||||
|
@ -99,42 +98,11 @@ def main():
|
|||
deviceType=dict(required=True),),
|
||||
supports_check_mode=False)
|
||||
|
||||
username = module.params['username']
|
||||
password = module.params['password']
|
||||
enablePassword = module.params['enablePassword']
|
||||
cliCommand = "save memory \n"
|
||||
command = 'write memory'
|
||||
outputfile = module.params['outputfile']
|
||||
hostIP = module.params['host']
|
||||
deviceType = module.params['deviceType']
|
||||
output = ""
|
||||
if not HAS_PARAMIKO:
|
||||
module.fail_json(msg='paramiko is required for this module')
|
||||
|
||||
# Create instance of SSHClient object
|
||||
remote_conn_pre = paramiko.SSHClient()
|
||||
|
||||
# Automatically add untrusted hosts (make sure okay for security policy in your environment)
|
||||
remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
|
||||
# initiate SSH connection with the switch
|
||||
remote_conn_pre.connect(hostIP, username=username, password=password)
|
||||
time.sleep(2)
|
||||
|
||||
# Use invoke_shell to establish an 'interactive session'
|
||||
remote_conn = remote_conn_pre.invoke_shell()
|
||||
time.sleep(2)
|
||||
|
||||
# Enable and then send command
|
||||
output = output + cnos.waitForDeviceResponse("\n", ">", 2, remote_conn)
|
||||
|
||||
output = output + cnos.enterEnableModeForDevice(enablePassword, 3, remote_conn)
|
||||
|
||||
# Make terminal length = 0
|
||||
output = output + cnos.waitForDeviceResponse("terminal length 0\n", "#", 2, remote_conn)
|
||||
|
||||
# cnos.debugOutput(cliCommand)
|
||||
# Send the CLi command
|
||||
output = output + cnos.waitForDeviceResponse(cliCommand, "#", 2, remote_conn)
|
||||
output = ''
|
||||
cmd = [{'command': command, 'prompt': None, 'answer': None}]
|
||||
output = output + str(cnos.run_cnos_commands(module, cmd))
|
||||
|
||||
# Save it into the file
|
||||
file = open(outputfile, "a")
|
||||
|
@ -143,7 +111,8 @@ def main():
|
|||
|
||||
errorMsg = cnos.checkOutputForError(output)
|
||||
if(errorMsg is None):
|
||||
module.exit_json(changed=True, msg="Switch Running Config is Saved to Startup Config ")
|
||||
module.exit_json(changed=True,
|
||||
msg="Switch Running Config is Saved to Startup Config ")
|
||||
else:
|
||||
module.fail_json(msg=errorMsg)
|
||||
|
||||
|
|
|
@ -33,22 +33,25 @@ DOCUMENTATION = '''
|
|||
---
|
||||
module: cnos_showrun
|
||||
author: "Anil Kumar Muraleedharan (@amuraleedhar)"
|
||||
short_description: Collect the current running configuration on devices running Lenovo CNOS
|
||||
short_description: Collect the current running configuration on devices running on CNOS
|
||||
description:
|
||||
- This module allows you to view the switch running configuration. It executes the display running-config CLI
|
||||
command on a switch and returns a file containing the current running configuration of the target network
|
||||
- This module allows you to view the switch running configuration. It
|
||||
executes the display running-config CLI command on a switch and returns a
|
||||
file containing the current running configuration of the target network
|
||||
device. This module uses SSH to manage network device configuration.
|
||||
The results of the operation will be placed in a directory named 'results'
|
||||
that must be created by the user in their local directory to where the playbook is run.
|
||||
For more information about this module from Lenovo and customizing it usage for your
|
||||
use cases, please visit U(http://systemx.lenovofiles.com/help/index.jsp?topic=%2Fcom.lenovo.switchmgt.ansible.doc%2Fcnos_showrun.html)
|
||||
that must be created by the user in their local directory to where the
|
||||
playbook is run. For more information about this module from Lenovo and
|
||||
customizing it usage for your use cases, please visit
|
||||
U(http://systemx.lenovofiles.com/help/index.jsp?topic=%2Fcom.lenovo.switchmgt.ansible.doc%2Fcnos_showrun.html)
|
||||
version_added: "2.3"
|
||||
extends_documentation_fragment: cnos
|
||||
options: {}
|
||||
|
||||
'''
|
||||
EXAMPLES = '''
|
||||
Tasks : The following are examples of using the module cnos_showrun. These are written in the main.yml file of the tasks directory.
|
||||
Tasks : The following are examples of using the module cnos_showrun. These are
|
||||
written in the main.yml file of the tasks directory.
|
||||
---
|
||||
- name: Run show running-config
|
||||
cnos_showrun:
|
||||
|
@ -69,11 +72,6 @@ msg:
|
|||
'''
|
||||
|
||||
import sys
|
||||
try:
|
||||
import paramiko
|
||||
HAS_PARAMIKO = True
|
||||
except ImportError:
|
||||
HAS_PARAMIKO = False
|
||||
import time
|
||||
import socket
|
||||
import array
|
||||
|
@ -99,41 +97,11 @@ def main():
|
|||
enablePassword=dict(required=False, no_log=True),),
|
||||
supports_check_mode=False)
|
||||
|
||||
username = module.params['username']
|
||||
password = module.params['password']
|
||||
enablePassword = module.params['enablePassword']
|
||||
cliCommand = "display running-config"
|
||||
command = 'show running-config'
|
||||
outputfile = module.params['outputfile']
|
||||
hostIP = module.params['host']
|
||||
output = ""
|
||||
if not HAS_PARAMIKO:
|
||||
module.fail_json(msg='paramiko is required for this module')
|
||||
|
||||
# Create instance of SSHClient object
|
||||
remote_conn_pre = paramiko.SSHClient()
|
||||
|
||||
# Automatically add untrusted hosts (make sure okay for security policy in your environment)
|
||||
remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
|
||||
# initiate SSH connection with the switch
|
||||
remote_conn_pre.connect(hostIP, username=username, password=password)
|
||||
time.sleep(2)
|
||||
|
||||
# Use invoke_shell to establish an 'interactive session'
|
||||
remote_conn = remote_conn_pre.invoke_shell()
|
||||
time.sleep(2)
|
||||
|
||||
# Enable and then send command
|
||||
output = output + cnos.waitForDeviceResponse("\n", ">", 2, remote_conn)
|
||||
|
||||
output = output + cnos.enterEnableModeForDevice(enablePassword, 3, remote_conn)
|
||||
|
||||
# Make terminal length = 0
|
||||
output = output + cnos.waitForDeviceResponse("terminal length 0\n", "#", 2, remote_conn)
|
||||
|
||||
# Send the CLi command
|
||||
output = output + cnos.waitForDeviceResponse(cliCommand + "\n", "#", 2, remote_conn)
|
||||
|
||||
output = ''
|
||||
cmd = [{'command': command, 'prompt': None, 'answer': None}]
|
||||
output = output + str(cnos.run_cnos_commands(module, cmd))
|
||||
# Save it into the file
|
||||
file = open(outputfile, "a")
|
||||
file.write(output)
|
||||
|
@ -141,7 +109,8 @@ def main():
|
|||
|
||||
errorMsg = cnos.checkOutputForError(output)
|
||||
if(errorMsg is None):
|
||||
module.exit_json(changed=True, msg="Running Configuration saved in file ")
|
||||
module.exit_json(changed=True,
|
||||
msg="Running Configuration saved in file ")
|
||||
else:
|
||||
module.fail_json(msg=errorMsg)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue