Advertisement
Guest User

Untitled

a guest
Aug 1st, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. DOCUMENTATION = '''
  2. ---
  3. module: napalm_cli
  4. author: "Charlie Allom - based on napalm_ping Jason Edelman (@jedelman8)"
  5. version_added: "2.2"
  6. short_description: "Executes CLI commands and returns response using NAPALM"
  7. description:
  8. - "This module logs into the device, issues a ping request, and returns the response"
  9. requirements:
  10. - napalm
  11. options:
  12. cli_args:
  13. description:
  14. - Command to run on the CLI
  15. required: True
  16. '''
  17.  
  18. EXAMPLES = '''
  19. vars:
  20. napalm_provider:
  21. hostname: "{{ inventory_hostname }}"
  22. username: "napalm"
  23. password: "napalm"
  24. dev_os: "eos"
  25. - napalm_cli:
  26. provider: "{{ napalm_provider }}"
  27. cli_args:
  28. - show version
  29. - show snmp chassis
  30. '''
  31.  
  32. RETURN = '''
  33. changed:
  34. description: ALWAYS RETURNS FALSE
  35. returned: always
  36. type: bool
  37. sample: True
  38. results:
  39. description: string of command output
  40. returned: always
  41. type: dict
  42. sample:
  43. {
  44. "show snmp chassis": "Chassis: 1234\n",
  45. "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"
  46. }
  47. '''
  48.  
  49. try:
  50. from napalm_base import get_network_driver
  51. except ImportError:
  52. napalm_found = False
  53. else:
  54. napalm_found = True
  55.  
  56. def main():
  57. os_choices = ['eos', 'junos', 'ios', 'vyos', 'ros']
  58. module = AnsibleModule(
  59. argument_spec=dict(
  60. hostname=dict(type='str', required=False, aliases=['host']),
  61. username=dict(type='str', required=False),
  62. password=dict(type='str', required=False, no_log=True),
  63. provider=dict(type='dict', required=False),
  64. timeout=dict(type='int', required=False, default=60),
  65. dev_os=dict(type='str', required=False, choices=os_choices),
  66. optional_args=dict(required=False, type='dict', default=None),
  67. cli_args=dict(required=True, type='list', default=None),
  68. ),
  69. supports_check_mode=True
  70. )
  71.  
  72. if not napalm_found:
  73. module.fail_json(msg="the python module napalm is required")
  74.  
  75. provider = module.params['provider'] or {}
  76.  
  77. no_log = ['password', 'secret']
  78. for param in no_log:
  79. if provider.get(param):
  80. module.no_log_values.update(return_values(provider[param]))
  81. if provider.get('optional_args') and provider['optional_args'].get(param):
  82. module.no_log_values.update(return_values(provider['optional_args'].get(param)))
  83. if module.params.get('optional_args') and module.params['optional_args'].get(param):
  84. module.no_log_values.update(return_values(module.params['optional_args'].get(param)))
  85.  
  86. # allow host or hostname
  87. provider['hostname'] = provider.get('hostname', None) or provider.get('host', None)
  88. # allow local params to override provider
  89. for param, pvalue in provider.items():
  90. if module.params.get(param) != False:
  91. module.params[param] = module.params.get(param) or pvalue
  92.  
  93. hostname = module.params['hostname']
  94. username = module.params['username']
  95. dev_os = module.params['dev_os']
  96. password = module.params['password']
  97. timeout = module.params['timeout']
  98. cli_args = module.params['cli_args']
  99.  
  100. for param, pvalue in module.params.items():
  101. if param in cli_args and pvalue is not None:
  102. cli_args[param] = pvalue
  103.  
  104. argument_check = { 'hostname': hostname, 'username': username, 'dev_os': dev_os, 'password': password }
  105. for key, val in argument_check.items():
  106. if val is None:
  107. module.fail_json(msg=str(key) + " is required")
  108.  
  109. # use checks outside of ansible defined checks, since params come can come from provider
  110. if dev_os not in os_choices:
  111. module.fail_json(msg="dev_os is not set to " + str(os_choices))
  112.  
  113. if module.params['optional_args'] is None:
  114. optional_args = {}
  115. else:
  116. optional_args = module.params['optional_args']
  117.  
  118. try:
  119. network_driver = get_network_driver(dev_os)
  120. device = network_driver(hostname=hostname,
  121. username=username,
  122. password=password,
  123. timeout=timeout,
  124. optional_args=optional_args)
  125. device.open()
  126. except Exception, e:
  127. module.fail_json(msg="cannot connect to device: " + str(e))
  128.  
  129. cli_response = device.cli(cli_args)
  130.  
  131. try:
  132. device.close()
  133. except Exception, e:
  134. module.fail_json(msg="cannot close device connection: " + str(e))
  135.  
  136. module.exit_json(changed=False, results=cli_response)
  137.  
  138. # standard ansible module imports
  139. from ansible.module_utils.basic import *
  140.  
  141. if __name__ == '__main__':
  142. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement