@ -4,8 +4,29 @@
from __future__ import ( absolute_import , division , print_function )
from __future__ import ( absolute_import , division , print_function )
__metaclass__ = type
__metaclass__ = type
DOCUMENTATION = """
- - -
author : Ansible Networking Team
httpapi : eos
short_description : Use eAPI to run command on eos platform
description :
- This eos plugin provides low level abstraction api ' s for
sending and receiving CLI commands with eos network devices .
version_added : " 2.6 "
options :
eos_use_sessions :
type : int
default : 1
description :
- Specifies if sessions should be used on remote host or not
env :
- name : ANSIBLE_EOS_USE_SESSIONS
vars :
- name : ansible_eos_use_sessions
version_added : ' 2.8 '
"""
import json
import json
import time
from ansible . module_utils . _text import to_text
from ansible . module_utils . _text import to_text
from ansible . module_utils . connection import ConnectionError
from ansible . module_utils . connection import ConnectionError
@ -16,7 +37,39 @@ from ansible.utils.display import Display
display = Display ( )
display = Display ( )
OPTIONS = {
' format ' : [ ' text ' , ' json ' ] ,
' diff_match ' : [ ' line ' , ' strict ' , ' exact ' , ' none ' ] ,
' diff_replace ' : [ ' line ' , ' block ' , ' config ' ] ,
' output ' : [ ' text ' , ' json ' ]
}
class HttpApi ( HttpApiBase ) :
class HttpApi ( HttpApiBase ) :
def __init__ ( self , * args , * * kwargs ) :
super ( HttpApi , self ) . __init__ ( * args , * * kwargs )
self . _device_info = None
self . _session_support = None
@property
def supports_sessions ( self ) :
use_session = self . get_option ( ' eos_use_sessions ' )
try :
use_session = int ( use_session )
except ValueError :
pass
if not bool ( use_session ) :
self . _session_support = False
else :
if self . _session_support :
return self . _session_support
response = self . send_request ( ' show configuration sessions ' )
self . _session_support = ' error ' not in response
return self . _session_support
def send_request ( self , data , * * message_kwargs ) :
def send_request ( self , data , * * message_kwargs ) :
data = to_list ( data )
data = to_list ( data )
if self . _become :
if self . _become :
@ -45,117 +98,51 @@ class HttpApi(HttpApiBase):
return results
return results
def get_prompt ( self ) :
def get_device_info ( self ) :
# Fake a prompt for @enable_mode
if self . _device_info :
if self . _become :
return self . _device_info
return ' # '
return ' > '
# Imported from module_utils
def edit_config ( self , config , commit = False , replace = False ) :
""" Loads the configuration onto the remote devices
If the device doesn ' t support configuration sessions, this will
fallback to using configure ( ) to load the commands . If that happens ,
there will be no returned diff or session values
"""
session = ' ansible_ %s ' % int ( time . time ( ) )
result = { ' session ' : session }
banner_cmd = None
banner_input = [ ]
commands = [ ' configure session %s ' % session ]
if replace :
commands . append ( ' rollback clean-config ' )
for command in config :
if command . startswith ( ' banner ' ) :
banner_cmd = command
banner_input = [ ]
elif banner_cmd :
if command == ' EOF ' :
command = { ' cmd ' : banner_cmd , ' input ' : ' \n ' . join ( banner_input ) }
banner_cmd = None
commands . append ( command )
else :
banner_input . append ( command )
continue
else :
commands . append ( command )
try :
device_info = { }
response = self . send_request ( commands )
except Exception :
device_info [ ' network_os ' ] = ' eos '
commands = [ ' configure session %s ' % session , ' abort ' ]
reply = self . send_request ( ' show version | json ' )
response = self . send_request ( commands , output = ' text ' )
data = json . loads ( reply )
raise
device_info [ ' network_os_version ' ] = data [ ' version ' ]
commands = [ ' configure session %s ' % session , ' show session-config diffs ' ]
device_info [ ' network_os_model ' ] = data [ ' modelName ' ]
if commit :
commands . append ( ' commit ' )
reply = self . send_request ( ' show hostname | json ' )
else :
data = json . loads ( reply )
commands . append ( ' abort ' )
device_info [ ' network_os_hostname ' ] = data [ ' hostname ' ]
response = self . send_request ( commands , output = ' text ' )
diff = response [ 1 ] . strip ( )
self . _device_info = device_info
if diff :
return self . _device_info
result [ ' diff ' ] = diff
def get_device_operations ( self ) :
return result
return {
' supports_diff_replace ' : True ,
def run_commands ( self , commands , check_rc = True ) :
' supports_commit ' : bool ( self . supports_sessions ) ,
""" Runs list of commands on remote device and returns results
' supports_rollback ' : False ,
"""
' supports_defaults ' : False ,
output = None
' supports_onbox_diff ' : bool ( self . supports_sessions ) ,
queue = list ( )
' supports_commit_comment ' : False ,
responses = list ( )
' supports_multiline_delimiter ' : False ,
' supports_diff_match ' : True ,
def run_queue ( queue , output ) :
' supports_diff_ignore_lines ' : True ,
try :
' supports_generate_diff ' : not bool ( self . supports_sessions ) ,
response = to_list ( self . send_request ( queue , output = output ) )
' supports_replace ' : bool ( self . supports_sessions ) ,
except Exception as exc :
}
if check_rc :
raise
def get_capabilities ( self ) :
return to_text ( exc )
result = { }
result [ ' rpc ' ] = [ ]
if output == ' json ' :
result [ ' device_info ' ] = self . get_device_info ( )
response = [ json . loads ( item ) for item in response ]
result [ ' device_operations ' ] = self . get_device_operations ( )
return response
result . update ( OPTIONS )
result [ ' network_api ' ] = ' eapi '
for item in to_list ( commands ) :
cmd_output = ' text '
return json . dumps ( result )
if isinstance ( item , dict ) :
command = item [ ' command ' ]
if ' output ' in item :
cmd_output = item [ ' output ' ]
else :
command = item
# Emulate '| json' from CLI
if command . endswith ( ' | json ' ) :
command = command . rsplit ( ' | ' , 1 ) [ 0 ]
cmd_output = ' json '
if output and output != cmd_output :
responses . extend ( run_queue ( queue , output ) )
queue = list ( )
output = cmd_output
queue . append ( command )
if queue :
responses . extend ( run_queue ( queue , output ) )
return responses
def load_config ( self , config , commit = False , replace = False ) :
""" Loads the configuration onto the remote devices
If the device doesn ' t support configuration sessions, this will
fallback to using configure ( ) to load the commands . If that happens ,
there will be no returned diff or session values
"""
return self . edit_config ( config , commit , replace )
def handle_response ( response ) :
def handle_response ( response ) :
@ -170,6 +157,7 @@ def handle_response(response):
raise ConnectionError ( error_text , code = error [ ' code ' ] )
raise ConnectionError ( error_text , code = error [ ' code ' ] )
results = [ ]
results = [ ]
for result in response [ ' result ' ] :
for result in response [ ' result ' ] :
if ' messages ' in result :
if ' messages ' in result :
results . append ( result [ ' messages ' ] [ 0 ] )
results . append ( result [ ' messages ' ] [ 0 ] )