@@ -2383,7 +2383,24 @@ def connection_exists(self):
23832383 def down_connection (self ):
23842384 cmd = [self .nmcli_bin , "con" , "down" , self .conn_name ]
23852385 return self .execute_command (cmd )
2386-
2386+ def get_connection_state (self ):
2387+ """Get the current state of the connection"""
2388+ cmd = [self .nmcli_bin , "--terse" , "--fields" , "GENERAL.STATE" , "con" , "show" , self .conn_name ]
2389+ (rc , out , err ) = self .execute_command (cmd )
2390+ if rc != 0 :
2391+ raise None
2392+
2393+ lines = out .strip ().split ('\n ' )
2394+ for line in lines :
2395+ if 'GENERAL.STATE' in line :
2396+ state = line .split (':' )[- 1 ].strip ()
2397+ return state
2398+ return None
2399+
2400+ def is_connection_active (self ):
2401+ """Check if the connection is currently active"""
2402+ state = self .get_connection_state ()
2403+ return state == "activated"
23872404 def up_connection (self ):
23882405 cmd = [self .nmcli_bin , "con" , "up" , self .conn_name ]
23892406 return self .execute_command (cmd )
@@ -2934,31 +2951,67 @@ def main():
29342951
29352952 elif nmcli .state == "up" :
29362953 if nmcli .connection_exists ():
2937- if module .check_mode :
2938- module .exit_json (changed = True )
2939- if nmcli .conn_reload :
2940- (rc , out , err ) = nmcli .reload_connection ()
2941- (rc , out , err ) = nmcli .up_connection ()
2942- if rc != 0 :
2943- module .fail_json (name = f"Error bringing up connection named { nmcli .conn_name } " , msg = err , rc = rc )
2954+ is_active = nmcli .is_connection_active ()
2955+
2956+ if is_active and not nmcli .conn_reload :
2957+ result ["changed" ] = False
2958+ result ["msg" ] = f"Connection { nmcli .conn_name } is already active"
2959+ if module .check_mode :
2960+ module .exit_json (changed = False , ** result )
2961+ else :
2962+ if module .check_mode :
2963+ module .exit_json (changed = True , ** result )
2964+
2965+ if nmcli .conn_reload :
2966+ (rc , out , err ) = nmcli .reload_connection ()
2967+ if rc != 0 :
2968+ module .fail_json (name = f"Error bringing up connection named { nmcli .conn_name } " , msg = err , rc = rc )
2969+
2970+ if not is_active or nmcli .conn_reload :
2971+ (rc , out , err ) = nmcli .up_connection ()
2972+ if rc != 0 :
2973+ module .fail_json (name = f"Error bringing up connection named { nmcli .conn_name } " , msg = err , rc = rc )
2974+ result ["changed" ] = True
2975+ else :
2976+ module .fail_json (
2977+ name = nmcli .conn_name ,
2978+ msg = "Connection does not exist" ,
2979+ )
29442980
29452981 elif nmcli .state == "down" :
29462982 if nmcli .connection_exists ():
2947- if module .check_mode :
2948- module .exit_json (changed = True )
2949- if nmcli .conn_reload :
2950- (rc , out , err ) = nmcli .reload_connection ()
2951- (rc , out , err ) = nmcli .down_connection ()
2952- if rc != 0 :
2953- module .fail_json (name = f"Error bringing down connection named { nmcli .conn_name } " , msg = err , rc = rc )
2983+ is_active = nmcli .is_connection_active ()
2984+
2985+ if not is_active and not nmcli .conn_reload :
2986+ result ["changed" ] = False
2987+ result ["msg" ] = f"Connection { nmcli .conn_name } is already inactive"
2988+ if module .check_mode :
2989+ module .exit_json (changed = False , ** result )
2990+ else :
2991+ if module .check_mode :
2992+ module .exit_json (changed = True , ** result )
2993+
2994+ if nmcli .conn_reload :
2995+ (rc , out , err ) = nmcli .reload_connection ()
2996+ if rc != 0 :
2997+ module .fail_json (name = f"Error reloading connection { nmcli .conn_name } " , msg = err , rc = rc )
2998+
2999+ if is_active or nmcli .conn_reload :
3000+ (rc , out , err ) = nmcli .down_connection ()
3001+ if rc != 0 :
3002+ module .fail_json (name = f"Error bringing down connection named { nmcli .conn_name } " , msg = err , rc = rc )
3003+ result ["changed" ] = True
3004+ else :
3005+ module .fail_json (msg = f"Connection { nmcli .conn_name } does not exist" )
29543006
29553007 except NmcliModuleError as e :
29563008 module .fail_json (name = nmcli .conn_name , msg = str (e ))
29573009
2958- if rc is None :
2959- result ["changed" ] = False
2960- else :
2961- result ["changed" ] = True
3010+ if "changed" not in result :
3011+ if rc is None :
3012+ result ["changed" ] = False
3013+ else :
3014+ result ["changed" ] = True
29623015 if out :
29633016 result ["stdout" ] = out
29643017 if err :
0 commit comments