-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preliminary module to test SSH connectivity
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#!/usr/bin/env python | ||
''' | ||
Ansible module to test that Netmiko SSH connection to remote device is successful | ||
By default, verifies that '#' exists in the remote device prompt. | ||
''' | ||
|
||
from ansible.module_utils.basic import * | ||
from netmiko import ConnectHandler | ||
|
||
try: | ||
# require netmiko >= 0.2.5 | ||
import netmiko | ||
version = netmiko.__version__ | ||
major_ver, minor_ver, revision = version.split('.') | ||
if major_ver >= 0 and minor_ver >= 2 and revision >= 5: | ||
MEETS_REQUIREMENTS = True | ||
else: | ||
MEETS_REQUIREMENTS = False | ||
except ImportError: | ||
MEETS_REQUIREMENTS = False | ||
|
||
|
||
def main(): | ||
''' | ||
Ansible module to test that Netmiko SSH connection to remote device is successful | ||
By default, verifies that '#' exists in the remote device prompt. | ||
''' | ||
|
||
module = AnsibleModule( | ||
argument_spec=dict( | ||
device_type=dict(required=False, default='cisco_ios'), | ||
host=dict(required=True), | ||
port=dict(required=False, default=22), | ||
username=dict(required=True), | ||
password=dict(required=True), | ||
secret=dict(required=False, default=''), | ||
enable_mode=dict(required=False, default=False), | ||
check_string=dict(required=False, default='#'), | ||
), | ||
supports_check_mode=False) | ||
|
||
if not MEETS_REQUIREMENTS: | ||
module.fail_json(msg='netmiko >= 0.2.5 is required for this module') | ||
|
||
net_device = { | ||
'device_type': module.params['device_type'], | ||
'ip': module.params['host'], | ||
'port': int(module.params['port']), | ||
'username': module.params['username'], | ||
'password': module.params['password'], | ||
'secret': module.params['secret'], | ||
'verbose': False, | ||
} | ||
|
||
ssh_conn = ConnectHandler(**net_device) | ||
if module.params['enable_mode']: | ||
ssh_conn.enable() | ||
output = ssh_conn.find_prompt() | ||
if module.params['check_string'] in output: | ||
module.exit_json(msg="SSH connection completed successfully") | ||
else: | ||
module.fail_json(msg="Failed to detect check_string in output", output=output) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |