@ -39,7 +39,7 @@ options:
description :
description :
- Desired state of a plugin .
- Desired state of a plugin .
required : False
required : False
choices : [ present , absent ]
choices : [ " present " , " absent " ]
default : present
default : present
url :
url :
description :
description :
@ -92,6 +92,10 @@ EXAMPLES = '''
- elasticsearch_plugin : state = absent name = " mobz/elasticsearch-head "
- elasticsearch_plugin : state = absent name = " mobz/elasticsearch-head "
'''
'''
PACKAGE_STATE_MAP = dict (
present = " install " ,
absent = " remove "
)
def parse_plugin_repo ( string ) :
def parse_plugin_repo ( string ) :
elements = string . split ( " / " )
elements = string . split ( " / " )
@ -111,11 +115,9 @@ def parse_plugin_repo(string):
return repo
return repo
def is_plugin_present ( plugin_dir , working_dir ) :
def is_plugin_present ( plugin_dir , working_dir ) :
return os . path . isdir ( os . path . join ( working_dir , plugin_dir ) )
return os . path . isdir ( os . path . join ( working_dir , plugin_dir ) )
def parse_error ( string ) :
def parse_error ( string ) :
reason = " reason: "
reason = " reason: "
try :
try :
@ -123,18 +125,49 @@ def parse_error(string):
except ValueError :
except ValueError :
return string
return string
def install_plugin ( module , plugin_bin , plugin_name , version , url , proxy_host , proxy_port , timeout ) :
cmd_args = [ plugin_bin , PACKAGE_STATE_MAP [ " present " ] , plugin_name ]
def main ( ) :
if version :
name = name + ' / ' + version
package_state_map = dict (
if proxy_host and proxy_port :
present = " install " ,
cmd_args . append ( " -DproxyHost= %s -DproxyPort= %s " % ( proxy_host , proxy_port ) )
absent = " remove "
)
if url :
cmd_args . append ( " --url %s " % url )
if timeout :
cmd_args . append ( " --timeout %s " % timeout )
cmd = " " . join ( cmd_args )
rc , out , err = module . run_command ( cmd )
if rc != 0 :
reason = parse_error ( out )
module . fail_json ( msg = reason )
return True , cmd , out , err
def remove_plugin ( module , plugin_bin , plugin_name ) :
cmd_args = [ plugin_bin , PACKAGE_STATE_MAP [ " absent " ] , parse_plugin_repo ( plugin_name ) ]
cmd = " " . join ( cmd_args )
rc , out , err = module . run_command ( cmd )
if rc != 0 :
reason = parse_error ( out )
module . fail_json ( msg = reason )
return True , cmd , out , err
def main ( ) :
module = AnsibleModule (
module = AnsibleModule (
argument_spec = dict (
argument_spec = dict (
name = dict ( required = True ) ,
name = dict ( required = True ) ,
state = dict ( default = " present " , choices = package_state_map . keys ( ) ) ,
state = dict ( default = " present " , choices = PACKAGE_STATE_MAP . keys ( ) ) ,
url = dict ( default = None ) ,
url = dict ( default = None ) ,
timeout = dict ( default = " 1m " ) ,
timeout = dict ( default = " 1m " ) ,
plugin_bin = dict ( default = " /usr/share/elasticsearch/bin/plugin " , type = " path " ) ,
plugin_bin = dict ( default = " /usr/share/elasticsearch/bin/plugin " , type = " path " ) ,
@ -145,46 +178,31 @@ def main():
)
)
)
)
name = module . params [ " name " ]
name = module . params [ " name " ]
state = module . params [ " state " ]
state = module . params [ " state " ]
url = module . params [ " url " ]
url = module . params [ " url " ]
timeout = module . params [ " timeout " ]
timeout = module . params [ " timeout " ]
plugin_bin = module . params [ " plugin_bin " ]
plugin_bin = module . params [ " plugin_bin " ]
plugin_dir = module . params [ " plugin_dir " ]
plugin_dir = module . params [ " plugin_dir " ]
proxy_host = module . params [ " proxy_host " ]
proxy_host = module . params [ " proxy_host " ]
proxy_port = module . params [ " proxy_port " ]
proxy_port = module . params [ " proxy_port " ]
version = module . params [ " version " ]
version = module . params [ " version " ]
present = is_plugin_present ( parse_plugin_repo ( name ) , plugin_dir )
present = is_plugin_present ( parse_plugin_repo ( name ) , plugin_dir )
# skip if the state is correct
# skip if the state is correct
if ( present and state == " present " ) or ( state == " absent " and not present ) :
if ( present and state == " present " ) or ( state == " absent " and not present ) :
module . exit_json ( changed = False , name = name )
module . exit_json ( changed = False , name = name , state = state )
if ( version ) :
name = name + ' / ' + version
cmd_args = [ plugin_bin , package_state_map [ state ] , name ]
if proxy_host and proxy_port :
cmd_args . append ( " -DproxyHost= %s -DproxyPort= %s " % ( proxy_host , proxy_port ) )
if url :
if state == " present " :
cmd_args . append ( " --url %s " % url )
changed , cmd , out , err = install_plugin ( module , plugin_bin , name , version , url , proxy_host , proxy_port , timeout )
if timeout :
cmd_args . append ( " --timeout %s " % timeout )
cmd = " " . join ( cmd_args )
rc , out , err = module . run_command ( cmd )
if rc != 0 :
elif state == " absent " :
reason = parse_error ( out )
changed , cmd , out , err = remove_plugin ( module , plugin_bin , name )
module . fail_json ( msg = reason )
module . exit_json ( changed = True , cmd = cmd , name = name , state = state , url = url , timeout = timeout , stdout = out , stderr = err )
module . exit_json ( changed = changed , cmd = cmd , name = name , state = state , url = url , timeout = timeout , stdout = out , stderr = err )
from ansible . module_utils . basic import *
from ansible . module_utils . basic import *
main ( )
if __name__ == ' __main__ ' :
main ( )