@ -25,10 +25,11 @@
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#
import json
from ansible . module_utils . _text import to_text
from ansible . module_utils . _text import to_text
from ansible . module_utils . basic import env_fallback , return_values
from ansible . module_utils . basic import env_fallback , return_values
from ansible . module_utils . network_common import to_list
from ansible . module_utils . network_common import to_list
from ansible . module_utils . connection import exec_command
from ansible . module_utils . connection import Connection
_DEVICE_CONFIGS = { }
_DEVICE_CONFIGS = { }
@ -62,64 +63,94 @@ def get_provider_argspec():
return vyos_provider_spec
return vyos_provider_spec
def get_config ( module , target = ' commands ' ) :
def get_connection ( module ) :
cmd = ' ' . join ( [ ' show configuration ' , target ] )
if hasattr ( module , ' _vyos_connection ' ) :
return module . _vyos_connection
try :
capabilities = get_capabilities ( module )
return _DEVICE_CONFIGS [ cmd ]
network_api = capabilities . get ( ' network_api ' )
except KeyError :
if network_api == ' cliconf ' :
rc , out , err = exec_command ( module , cmd )
module . _vyos_connection = Connection ( module . _socket_path )
if rc != 0 :
else :
module . fail_json ( msg = ' unable to retrieve current config ' , stderr = to_text ( err , errors = ' surrogate_or_strict ' ) )
module . fail_json ( msg = ' Invalid connection type %s ' % network_api )
cfg = to_text ( out , errors = ' surrogate_or_strict ' ) . strip ( )
_DEVICE_CONFIGS [ cmd ] = cfg
return module . _vyos_connection
def get_capabilities ( module ) :
if hasattr ( module , ' _vyos_capabilities ' ) :
return module . _vyos_capabilities
capabilities = Connection ( module . _socket_path ) . get_capabilities ( )
module . _vyos_capabilities = json . loads ( capabilities )
return module . _vyos_capabilities
def get_config ( module ) :
global _DEVICE_CONFIGS
if _DEVICE_CONFIGS != { } :
return _DEVICE_CONFIGS
else :
connection = get_connection ( module )
out = connection . get_config ( )
cfg = to_text ( out , errors = ' surrogate_then_replace ' ) . strip ( )
_DEVICE_CONFIGS = cfg
return cfg
return cfg
def run_commands ( module , commands , check_rc = True ) :
def run_commands ( module , commands , check_rc = True ) :
responses = list ( )
responses = list ( )
connection = get_connection ( module )
for cmd in to_list ( commands ) :
for cmd in to_list ( commands ) :
rc , out , err = exec_command ( module , cmd )
try :
if check_rc and rc != 0 :
cmd = json . loads ( cmd )
module . fail_json ( msg = to_text ( err , errors = ' surrogate_or_strict ' ) , rc = rc )
command = cmd [ ' command ' ]
responses . append ( to_text ( out , errors = ' surrogate_or_strict ' ) )
prompt = cmd [ ' prompt ' ]
answer = cmd [ ' answer ' ]
except :
command = cmd
prompt = None
answer = None
out = connection . get ( command , prompt , answer )
try :
out = to_text ( out , errors = ' surrogate_or_strict ' )
except UnicodeError :
module . fail_json ( msg = u ' Failed to decode output from %s : %s ' % ( cmd , to_text ( out ) ) )
responses . append ( out )
return responses
return responses
def load_config ( module , commands , commit = False , comment = None ) :
def load_config ( module , commands , commit = False , comment = None ) :
rc , out , err = exec_command ( module , ' configure ' )
connection = get_connection ( module )
if rc != 0 :
module . fail_json ( msg = ' unable to enter configuration mode ' , output = to_text ( err , errors = ' surrogate_or_strict ' ) )
for cmd in to_list ( commands ) :
out = connection . edit_config ( commands )
rc , out , err = exec_command ( module , cmd )
if rc != 0 :
# discard any changes in case of failure
exec_command ( module , ' exit discard ' )
module . fail_json ( msg = ' configuration failed ' )
diff = None
diff = None
if module . _diff :
if module . _diff :
rc, out , err = exec_command ( module , ' compare ' )
out = connection . get ( ' compare ' )
out = to_text ( out , errors = ' surrogate_or_strict ' )
out = to_text ( out , errors = ' surrogate_or_strict ' )
if not out . startswith ( ' No changes ' ) :
if not out . startswith ( ' No changes ' ) :
rc, out , err = exec_command ( module , ' show ' )
out = connection . get ( ' show ' )
diff = to_text ( out , errors = ' surrogate_or_strict ' ) . strip ( )
diff = to_text ( out , errors = ' surrogate_or_strict ' ) . strip ( )
if commit :
if commit :
cmd = ' commit '
try :
if comment :
out = connection . commit ( comment )
cmd + = ' comment " %s " ' % comment
except :
rc , out , err = exec_command ( module , cmd )
connection . discard_changes ( )
if rc != 0 :
module . fail_json ( msg = ' commit failed: %s ' % out )
# discard any changes in case of failure
exec_command ( module , ' exit discard ' )
module . fail_json ( msg = ' commit failed: %s ' % err )
if not commit :
if not commit :
exec_command( module , ' exit discard ' )
connection . discard_changes ( )
else :
else :
exec_command( module , ' exit ' )
connection. get ( ' exit ' )
if diff :
if diff :
return diff
return diff