@@ -2412,6 +2412,21 @@ def down_connection(self):
24122412 cmd = [self .nmcli_bin , "con" , "down" , self .conn_name ]
24132413 return self .execute_command (cmd )
24142414
2415+ def get_connection_state (self ):
2416+ """Get the current state of the connection"""
2417+ cmd = [self .nmcli_bin , "--terse" , "--fields" , "GENERAL.STATE" , "con" , "show" , self .conn_name ]
2418+ (rc , out , err ) = self .execute_command (cmd )
2419+ if rc != 0 :
2420+ raise NmcliModuleError (err )
2421+
2422+ lines = [ll .strip () for ll in out .splitlines () if "GENERAL.STATE" in ll ]
2423+ return lines [0 ].split (":" )[1 ] if lines else None
2424+
2425+ def is_connection_active (self ):
2426+ """Check if the connection is currently active"""
2427+ state = self .get_connection_state ()
2428+ return state == "activated"
2429+
24152430 def up_connection (self ):
24162431 cmd = [self .nmcli_bin , "con" , "up" , self .conn_name ]
24172432 return self .execute_command (cmd )
@@ -2970,31 +2985,60 @@ def main():
29702985
29712986 elif nmcli .state == "up" :
29722987 if nmcli .connection_exists ():
2973- if module .check_mode :
2974- module .exit_json (changed = True )
2975- if nmcli .conn_reload :
2976- (rc , out , err ) = nmcli .reload_connection ()
2977- (rc , out , err ) = nmcli .up_connection ()
2978- if rc != 0 :
2979- module .fail_json (name = f"Error bringing up connection named { nmcli .conn_name } " , msg = err , rc = rc )
2988+ is_active = nmcli .is_connection_active ()
2989+
2990+ if is_active and not nmcli .conn_reload :
2991+ result ["changed" ] = False
2992+ result ["msg" ] = f"Connection { nmcli .conn_name } is already active"
2993+ module .exit_json (** result )
2994+ else :
2995+ if module .check_mode :
2996+ module .exit_json (changed = True , ** result )
2997+
2998+ if nmcli .conn_reload :
2999+ (rc , out , err ) = nmcli .reload_connection ()
3000+ if rc != 0 :
3001+ module .fail_json (msg = f"Error reloading connection named { nmcli .conn_name } : { err } " , rc = rc )
3002+
3003+ (rc , out , err ) = nmcli .up_connection ()
3004+ if rc != 0 :
3005+ module .fail_json (msg = f"Error bringing up connection named { nmcli .conn_name } : { err } " , rc = rc )
3006+ result ["changed" ] = True
3007+ else :
3008+ module .fail_json (
3009+ name = nmcli .conn_name ,
3010+ msg = "Connection does not exist" ,
3011+ )
29803012
29813013 elif nmcli .state == "down" :
29823014 if nmcli .connection_exists ():
2983- if module .check_mode :
2984- module .exit_json (changed = True )
2985- if nmcli .conn_reload :
2986- (rc , out , err ) = nmcli .reload_connection ()
2987- (rc , out , err ) = nmcli .down_connection ()
2988- if rc != 0 :
2989- module .fail_json (name = f"Error bringing down connection named { nmcli .conn_name } " , msg = err , rc = rc )
3015+ is_active = nmcli .is_connection_active ()
3016+
3017+ if not is_active and not nmcli .conn_reload :
3018+ result ["changed" ] = False
3019+ result ["msg" ] = f"Connection { nmcli .conn_name } is already inactive"
3020+ module .exit_json (** result )
3021+ else :
3022+ if module .check_mode :
3023+ module .exit_json (changed = True , ** result )
3024+
3025+ if nmcli .conn_reload :
3026+ (rc , out , err ) = nmcli .reload_connection ()
3027+ if rc != 0 :
3028+ module .fail_json (name = f"Error reloading connection { nmcli .conn_name } " , msg = err , rc = rc )
3029+
3030+ (rc , out , err ) = nmcli .down_connection ()
3031+ if rc != 0 :
3032+ module .fail_json (name = f"Error bringing down connection named { nmcli .conn_name } " , msg = err , rc = rc )
3033+ result ["changed" ] = True
3034+ else :
3035+ module .fail_json (msg = f"Connection { nmcli .conn_name } does not exist" )
29903036
29913037 except NmcliModuleError as e :
29923038 module .fail_json (name = nmcli .conn_name , msg = str (e ))
29933039
2994- if rc is None :
2995- result ["changed" ] = False
2996- else :
2997- result ["changed" ] = True
3040+ if "changed" not in result :
3041+ result ["changed" ] = rc is not None
29983042 if out :
29993043 result ["stdout" ] = out
30003044 if err :
0 commit comments