@ -25,6 +25,7 @@
# 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.
#
#
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 exec_command
@ -43,18 +44,19 @@ vyos_argument_spec = {
' provider ' : dict ( type = ' dict ' ) ,
' provider ' : dict ( type = ' dict ' ) ,
}
}
def check_args ( module , warnings ) :
def check_args ( module , warnings ) :
provider = module . params [ ' provider ' ] or { }
provider = module . params [ ' provider ' ] or { }
for key in vyos_argument_spec :
for key in vyos_argument_spec :
if key != ' provider ' and module . params [ key ] :
if key != ' provider ' and module . params [ key ] :
warnings . append ( ' argument %s has been deprecated and will be '
warnings . append ( ' argument %s has been deprecated and will be removed in a future version ' % key )
' removed in a future version ' % key )
if provider :
if provider :
for param in ( ' password ' , ) :
for param in ( ' password ' , ) :
if provider . get ( param ) :
if provider . get ( param ) :
module . no_log_values . update ( return_values ( provider [ param ] ) )
module . no_log_values . update ( return_values ( provider [ param ] ) )
def get_config ( module , target = ' commands ' ) :
def get_config ( module , target = ' commands ' ) :
cmd = ' ' . join ( [ ' show configuration ' , target ] )
cmd = ' ' . join ( [ ' show configuration ' , target ] )
@ -63,24 +65,26 @@ def get_config(module, target='commands'):
except KeyError :
except KeyError :
rc , out , err = exec_command ( module , cmd )
rc , out , err = exec_command ( module , cmd )
if rc != 0 :
if rc != 0 :
module . fail_json ( msg = ' unable to retrieve current config ' , stderr = err)
module . fail_json ( msg = ' unable to retrieve current config ' , stderr = to_text( err, errors = ' surrogate_or_strict ' ) )
cfg = str ( out ) . strip ( )
cfg = to_text ( out , errors = ' surrogate_or_strict ' ) . strip ( )
_DEVICE_CONFIGS [ cmd ] = cfg
_DEVICE_CONFIGS [ cmd ] = 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 ( )
for cmd in to_list ( commands ) :
for cmd in to_list ( commands ) :
rc , out , err = exec_command ( module , cmd )
rc , out , err = exec_command ( module , cmd )
if check_rc and rc != 0 :
if check_rc and rc != 0 :
module . fail_json ( msg = err, rc = rc )
module . fail_json ( msg = to_text( err, errors = ' surrogate_or_strict ' ) , rc = rc )
responses . append ( out)
responses . append ( to_text( out, errors = ' surrogate_or_strict ' ) )
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 ' )
rc , out , err = exec_command ( module , ' configure ' )
if rc != 0 :
if rc != 0 :
module . fail_json ( msg = ' unable to enter configuration mode ' , output = err)
module . fail_json ( msg = ' unable to enter configuration mode ' , output = to_text( err, errors = ' surrogate_or_strict ' ) )
for cmd in to_list ( commands ) :
for cmd in to_list ( commands ) :
rc , out , err = exec_command ( module , cmd )
rc , out , err = exec_command ( module , cmd )
@ -92,9 +96,10 @@ def load_config(module, commands, commit=False, comment=None):
diff = None
diff = None
if module . _diff :
if module . _diff :
rc , out , err = exec_command ( module , ' compare ' )
rc , out , err = exec_command ( module , ' compare ' )
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 ' )
rc , out , err = exec_command ( module , ' show ' )
diff = str ( out ) . strip ( )
diff = to_text ( out , errors = ' surrogate_or_strict ' ) . strip ( )
if commit :
if commit :
cmd = ' commit '
cmd = ' commit '