|
| 1 | +from ansible.module_utils.basic import AnsibleModule, return_values |
| 2 | + |
| 3 | + |
| 4 | +DOCUMENTATION = ''' |
| 5 | +--- |
| 6 | +module: napalm_cli |
| 7 | +author: "Charlie Allom - based on napalm_ping Jason Edelman (@jedelman8)" |
| 8 | +version_added: "2.2" |
| 9 | +short_description: "Executes CLI commands and returns response using NAPALM" |
| 10 | +description: |
| 11 | + - "This module logs into the device, issues a ping request, and returns the response" |
| 12 | +requirements: |
| 13 | + - napalm |
| 14 | +options: |
| 15 | + args: |
| 16 | + description: |
| 17 | + - Keyword arguments to pass to the `cli` method |
| 18 | + required: True |
| 19 | +''' |
| 20 | + |
| 21 | +EXAMPLES = ''' |
| 22 | +vars: |
| 23 | + napalm_provider: |
| 24 | + hostname: "{{ inventory_hostname }}" |
| 25 | + username: "napalm" |
| 26 | + password: "napalm" |
| 27 | + dev_os: "eos" |
| 28 | +- napalm_cli: |
| 29 | + provider: "{{ napalm_provider }}" |
| 30 | + args: |
| 31 | + commands: |
| 32 | + - show version |
| 33 | + - show snmp chassis |
| 34 | +''' |
| 35 | + |
| 36 | +RETURN = ''' |
| 37 | +changed: |
| 38 | + description: ALWAYS RETURNS FALSE |
| 39 | + returned: always |
| 40 | + type: bool |
| 41 | + sample: True |
| 42 | +results: |
| 43 | + description: string of command output |
| 44 | + returned: always |
| 45 | + type: dict |
| 46 | + sample: |
| 47 | + { |
| 48 | + "show snmp chassis": "Chassis: 1234\n", |
| 49 | + "show version": "Arista vEOS\nHardware version: \nSerial number: \nSystem MAC address: 0800.27c3.5f28\n\nSoftware image version: 4.17.5M\nArchitecture: i386\nInternal build version: 4.17.5M-4414219.4175M\nInternal build ID: d02143c6-e42b-4fc3-99b6-97063bddb6b8\n\nUptime: 1 hour and 21 minutes\nTotal memory: 1893416 kB\nFree memory: 956488 kB\n\n" # noqa |
| 50 | + } |
| 51 | +''' |
| 52 | + |
| 53 | +try: |
| 54 | + from napalm_base import get_network_driver |
| 55 | +except ImportError: |
| 56 | + napalm_found = False |
| 57 | +else: |
| 58 | + napalm_found = True |
| 59 | + |
| 60 | + |
| 61 | +def main(): |
| 62 | + os_choices = ['eos', 'junos', 'iosxr', 'fortios', |
| 63 | + 'ios', 'mock', 'nxos', 'panos', 'vyos', 'ros'] |
| 64 | + module = AnsibleModule( |
| 65 | + argument_spec=dict( |
| 66 | + hostname=dict(type='str', required=False, aliases=['host']), |
| 67 | + username=dict(type='str', required=False), |
| 68 | + password=dict(type='str', required=False, no_log=True), |
| 69 | + provider=dict(type='dict', required=False), |
| 70 | + timeout=dict(type='int', required=False, default=60), |
| 71 | + dev_os=dict(type='str', required=False, choices=os_choices), |
| 72 | + optional_args=dict(required=False, type='dict', default=None), |
| 73 | + args=dict(required=True, type='dict', default=None), |
| 74 | + ), |
| 75 | + supports_check_mode=False |
| 76 | + ) |
| 77 | + |
| 78 | + if not napalm_found: |
| 79 | + module.fail_json(msg="the python module napalm is required") |
| 80 | + |
| 81 | + provider = module.params['provider'] or {} |
| 82 | + |
| 83 | + no_log = ['password', 'secret'] |
| 84 | + for param in no_log: |
| 85 | + if provider.get(param): |
| 86 | + module.no_log_values.update(return_values(provider[param])) |
| 87 | + if provider.get('optional_args') and provider['optional_args'].get(param): |
| 88 | + module.no_log_values.update(return_values(provider['optional_args'].get(param))) |
| 89 | + if module.params.get('optional_args') and module.params['optional_args'].get(param): |
| 90 | + module.no_log_values.update(return_values(module.params['optional_args'].get(param))) |
| 91 | + |
| 92 | + # allow host or hostname |
| 93 | + provider['hostname'] = provider.get('hostname', None) or provider.get('host', None) |
| 94 | + # allow local params to override provider |
| 95 | + for param, pvalue in provider.items(): |
| 96 | + if module.params.get(param) is not False: |
| 97 | + module.params[param] = module.params.get(param) or pvalue |
| 98 | + |
| 99 | + hostname = module.params['hostname'] |
| 100 | + username = module.params['username'] |
| 101 | + dev_os = module.params['dev_os'] |
| 102 | + password = module.params['password'] |
| 103 | + timeout = module.params['timeout'] |
| 104 | + args = module.params['args'] |
| 105 | + |
| 106 | + argument_check = {'hostname': hostname, 'username': username, |
| 107 | + 'dev_os': dev_os, 'password': password} |
| 108 | + for key, val in argument_check.items(): |
| 109 | + if val is None: |
| 110 | + module.fail_json(msg=str(key) + " is required") |
| 111 | + |
| 112 | + # use checks outside of ansible defined checks, since params come can come from provider |
| 113 | + if dev_os not in os_choices: |
| 114 | + module.fail_json(msg="dev_os is not set to " + str(os_choices)) |
| 115 | + |
| 116 | + if module.params['optional_args'] is None: |
| 117 | + optional_args = {} |
| 118 | + else: |
| 119 | + optional_args = module.params['optional_args'] |
| 120 | + |
| 121 | + try: |
| 122 | + network_driver = get_network_driver(dev_os) |
| 123 | + device = network_driver(hostname=hostname, |
| 124 | + username=username, |
| 125 | + password=password, |
| 126 | + timeout=timeout, |
| 127 | + optional_args=optional_args) |
| 128 | + device.open() |
| 129 | + except Exception, e: |
| 130 | + module.fail_json(msg="cannot connect to device: " + str(e)) |
| 131 | + |
| 132 | + try: |
| 133 | + cli_response = device.cli(**args) |
| 134 | + except Exception as e: |
| 135 | + module.fail_json(msg="{}".format(e)) |
| 136 | + |
| 137 | + try: |
| 138 | + device.close() |
| 139 | + except Exception, e: |
| 140 | + module.fail_json(msg="cannot close device connection: " + str(e)) |
| 141 | + |
| 142 | + module.exit_json(changed=False, results=cli_response) |
| 143 | + |
| 144 | + |
| 145 | +if __name__ == '__main__': |
| 146 | + main() |
0 commit comments