@ -159,6 +159,7 @@ requirements:
- Docker API > = 1.25
- Docker API > = 1.25
author :
author :
- Thierry Bouvet ( @tbouvet )
- Thierry Bouvet ( @tbouvet )
- Piotr Wojciechowski ( @WojciechowskiPiotr )
'''
'''
EXAMPLES = '''
EXAMPLES = '''
@ -229,7 +230,7 @@ actions:
'''
'''
import json
import json
from time import sleep
try :
try :
from docker . errors import APIError
from docker . errors import APIError
except ImportError :
except ImportError :
@ -237,10 +238,12 @@ except ImportError:
pass
pass
from ansible . module_utils . docker . common import (
from ansible . module_utils . docker . common import (
AnsibleDockerClient ,
DockerBaseClass ,
DockerBaseClass ,
DifferenceTracker ,
DifferenceTracker ,
)
)
from ansible . module_utils . docker . swarm import AnsibleDockerSwarmClient
from ansible . module_utils . _text import to_native
from ansible . module_utils . _text import to_native
@ -253,6 +256,7 @@ class TaskParameters(DockerBaseClass):
self . force_new_cluster = None
self . force_new_cluster = None
self . remote_addrs = None
self . remote_addrs = None
self . join_token = None
self . join_token = None
self . force = None
# Spec
# Spec
self . snapshot_interval = None
self . snapshot_interval = None
@ -389,6 +393,11 @@ class SwarmManager(DockerBaseClass):
" inspect " : self . inspect_swarm
" inspect " : self . inspect_swarm
}
}
if self . state == ' inspect ' :
self . client . module . deprecate (
" The ' inspect ' state is deprecated, please use ' docker_swarm_facts ' to inspect swarm cluster " ,
version = ' 2.12 ' )
choice_map . get ( self . state ) ( )
choice_map . get ( self . state ) ( )
if self . client . module . _diff or self . parameters . debug :
if self . client . module . _diff or self . parameters . debug :
@ -396,15 +405,6 @@ class SwarmManager(DockerBaseClass):
diff [ ' before ' ] , diff [ ' after ' ] = self . differences . get_before_after ( )
diff [ ' before ' ] , diff [ ' after ' ] = self . differences . get_before_after ( )
self . results [ ' diff ' ] = diff
self . results [ ' diff ' ] = diff
def __isSwarmManager ( self ) :
try :
data = self . client . inspect_swarm ( )
json_str = json . dumps ( data , ensure_ascii = False )
self . swarm_info = json . loads ( json_str )
return True
except APIError :
return False
def inspect_swarm ( self ) :
def inspect_swarm ( self ) :
try :
try :
data = self . client . inspect_swarm ( )
data = self . client . inspect_swarm ( )
@ -416,7 +416,7 @@ class SwarmManager(DockerBaseClass):
return
return
def init_swarm ( self ) :
def init_swarm ( self ) :
if self . __isSwarmM anager( ) :
if self . client. check_if_swarm_m anager( ) :
self . __update_swarm ( )
self . __update_swarm ( )
return
return
@ -428,7 +428,10 @@ class SwarmManager(DockerBaseClass):
except APIError as exc :
except APIError as exc :
self . client . fail ( " Can not create a new Swarm Cluster: %s " % to_native ( exc ) )
self . client . fail ( " Can not create a new Swarm Cluster: %s " % to_native ( exc ) )
self . __isSwarmManager ( )
if not self . client . check_if_swarm_manager ( ) :
if not self . check_mode :
self . client . fail ( " Swarm not created or other error! " )
self . inspect_swarm ( )
self . results [ ' actions ' ] . append ( " New Swarm cluster created: %s " % ( self . swarm_info . get ( ' ID ' ) ) )
self . results [ ' actions ' ] . append ( " New Swarm cluster created: %s " % ( self . swarm_info . get ( ' ID ' ) ) )
self . differences . add ( ' state ' , parameter = ' absent ' , active = ' present ' )
self . differences . add ( ' state ' , parameter = ' absent ' , active = ' present ' )
self . results [ ' changed ' ] = True
self . results [ ' changed ' ] = True
@ -460,26 +463,15 @@ class SwarmManager(DockerBaseClass):
self . results [ ' actions ' ] . append ( " Swarm cluster updated " )
self . results [ ' actions ' ] . append ( " Swarm cluster updated " )
self . results [ ' changed ' ] = True
self . results [ ' changed ' ] = True
def __isSwarmNode ( self ) :
info = self . client . info ( )
if info :
json_str = json . dumps ( info , ensure_ascii = False )
self . swarm_info = json . loads ( json_str )
if self . swarm_info [ ' Swarm ' ] [ ' NodeID ' ] :
return True
if self . swarm_info [ ' Swarm ' ] [ ' LocalNodeState ' ] in ( ' active ' , ' pending ' , ' locked ' ) :
return True
return False
def join ( self ) :
def join ( self ) :
if self . __isSwarmN ode( ) :
if self . client . check_if_swarm_node ( ) :
self . results [ ' actions ' ] . append ( " This node is already part of a swarm. " )
self . results [ ' actions ' ] . append ( " This node is already part of a swarm. " )
return
return
if not self . check_mode :
if not self . check_mode :
try :
try :
self . client . join_swarm (
self . client . join_swarm (
remote_addrs = self . parameters . remote_addrs , join_token = self . parameters . join_token , listen_addr = self . parameters . listen_addr ,
remote_addrs = self . parameters . remote_addrs , join_token = self . parameters . join_token ,
advertise_addr= self . parameters . advertise_addr )
listen_addr = self . parameters . listen_addr , advertise_addr = self . parameters . advertise_addr )
except APIError as exc :
except APIError as exc :
self . client . fail ( " Can not join the Swarm Cluster: %s " % to_native ( exc ) )
self . client . fail ( " Can not join the Swarm Cluster: %s " % to_native ( exc ) )
self . results [ ' actions ' ] . append ( " New node is added to swarm cluster " )
self . results [ ' actions ' ] . append ( " New node is added to swarm cluster " )
@ -487,7 +479,7 @@ class SwarmManager(DockerBaseClass):
self . results [ ' changed ' ] = True
self . results [ ' changed ' ] = True
def leave ( self ) :
def leave ( self ) :
if not ( self . __isSwarmN ode( ) ) :
if not ( self . client. check_if_swarm_n ode( ) ) :
self . results [ ' actions ' ] . append ( " This node is not part of a swarm. " )
self . results [ ' actions ' ] . append ( " This node is not part of a swarm. " )
return
return
if not self . check_mode :
if not self . check_mode :
@ -499,33 +491,16 @@ class SwarmManager(DockerBaseClass):
self . differences . add ( ' joined ' , parameter = ' absent ' , active = ' present ' )
self . differences . add ( ' joined ' , parameter = ' absent ' , active = ' present ' )
self . results [ ' changed ' ] = True
self . results [ ' changed ' ] = True
def __get_node_info ( self ) :
try :
node_info = self . client . inspect_node ( node_id = self . parameters . node_id )
except APIError as exc :
raise exc
json_str = json . dumps ( node_info , ensure_ascii = False )
node_info = json . loads ( json_str )
return node_info
def __check_node_is_down ( self ) :
for _x in range ( 0 , 5 ) :
node_info = self . __get_node_info ( )
if node_info [ ' Status ' ] [ ' State ' ] == ' down ' :
return True
sleep ( 5 )
return False
def remove ( self ) :
def remove ( self ) :
if not ( self . __isSwarmM anager( ) ) :
if not ( self . client . check_if_swarm_manager ( ) ) :
self . client . fail ( " This node is not a manager. " )
self . client . fail ( " This node is not a manager. " )
try :
try :
status_down = self . __check_node_is_down( )
status_down = self . client . check_if_swarm_node_is_down ( repeat_check = 5 )
except APIError :
except APIError :
return
return
if not ( status_down ) :
if not status_down :
self . client . fail ( " Can not remove the node. The status node is ready and not down. " )
self . client . fail ( " Can not remove the node. The status node is ready and not down. " )
if not self . check_mode :
if not self . check_mode :
@ -577,7 +552,7 @@ def main():
ca_force_rotate = dict ( docker_api_version = ' 1.30 ' ) ,
ca_force_rotate = dict ( docker_api_version = ' 1.30 ' ) ,
)
)
client = AnsibleDocker Client(
client = AnsibleDocker Swarm Client(
argument_spec = argument_spec ,
argument_spec = argument_spec ,
supports_check_mode = True ,
supports_check_mode = True ,
required_if = required_if ,
required_if = required_if ,