@ -22,43 +22,67 @@ module: macports
author : " Jimmy Tang (@jcftang) "
short_description : Package manager for MacPorts
description :
- Manages MacPorts packages
- Manages MacPorts packages ( ports )
version_added : " 1.1 "
options :
name :
description :
- name of package to install / remove
- A list of port names .
aliases : [ ' port ' ]
required : true
state :
description :
- state of the package
- Indicates the desired state of the port .
choices : [ ' present ' , ' absent ' , ' active ' , ' inactive ' ]
default : present
update_ cache :
update_ ports :
description :
- update the package db first
- Update the ports tree first .
aliases : [ ' update_cache ' ]
default : " no "
type : bool
variant :
description :
- A port variant specification .
- ' C(variant) is only supported with state: I(installed)/I(present). '
aliases : [ ' variants ' ]
version_added : " 2.7 "
'''
EXAMPLES = '''
- macports :
- name : Install the foo port
macports :
name : foo
state : present
- macports :
- name : Install the universal , x11 variant of the foo port
macports :
name : foo
variant : + universal + x11
- name : Install a list of ports
macports :
name : " {{ ports }} "
vars :
ports :
- foo
- foo - tools
- name : Update the ports tree then install the foo port
macports :
name : foo
state : present
update_cache : yes
update_ports : yes
- macports :
- name : Remove the foo port
macports :
name : foo
state : absent
- macports :
- name : Activate the foo port
macports :
name : foo
state : active
- macports :
- name : Deactivate the foo port
macports :
name : foo
state : inactive
'''
@ -67,17 +91,17 @@ from ansible.module_utils.basic import AnsibleModule
from ansible . module_utils . six . moves import shlex_quote
def update_package_db ( module , port_path ) :
""" Updates packages list . """
def sync_ports ( module , port_path ) :
""" Sync ports tree . """
rc , out , err = module . run_command ( " %s sync " % port_path )
if rc != 0 :
module . fail_json ( msg = " could not update package db" )
module . fail_json ( msg = " Could not update ports tree" , stdout = out , stderr = err )
def query_p ackage ( module , port_path , name , state = " present " ) :
""" Returns whether a p ackage is installed or not. """
def query_p ort ( module , port_path , name , state = " present " ) :
""" Returns whether a p ort is installed or not. """
if state == " present " :
@ -97,108 +121,109 @@ def query_package(module, port_path, name, state="present"):
return False
def remove_p ackages( module , port_path , package s) :
""" Uninstalls one or more p ackage s if installed. """
def remove_p orts( module , port_path , port s) :
""" Uninstalls one or more p ort s if installed. """
remove_c = 0
# Using a for loop in case of error, we can report the p ackage that failed
for p ackage in package s:
# Query the p ackage first, to see if we even need to remove
if not query_p ackage( module , port_path , package ) :
# Using a for loop in case of error, we can report the p ort that failed
for p ort in port s:
# Query the p ort first, to see if we even need to remove
if not query_p ort( module , port_path , port ) :
continue
rc , out , err = module . run_command ( " %s uninstall %s " % ( port_path , p ackage ) )
rc , out , err = module . run_command ( " %s uninstall %s " % ( port_path , p ort ) )
if query_p ackage( module , port_path , package ) :
module . fail_json ( msg = " failed to remove %s : %s " % ( package , out ) )
if query_p ort( module , port_path , port ) :
module . fail_json ( msg = " Failed to remove %s : %s " % ( port , err ) )
remove_c + = 1
if remove_c > 0 :
module . exit_json ( changed = True , msg = " removed %s package (s)" % remove_c )
module . exit_json ( changed = True , msg = " Removed %s port (s)" % remove_c )
module . exit_json ( changed = False , msg = " package (s) already absent" )
module . exit_json ( changed = False , msg = " Port (s) already absent" )
def install_p ackages( module , port_path , packages ) :
""" Installs one or more p ackage s if not already installed. """
def install_p orts( module , port_path , ports , variant ) :
""" Installs one or more p ort s if not already installed. """
install_c = 0
for p ackage in package s:
if query_p ackage( module , port_path , package ) :
for p ort in port s:
if query_p ort( module , port_path , port ) :
continue
rc , out , err = module . run_command ( " %s install %s " % ( port_path , p ackage ) )
rc , out , err = module . run_command ( " %s install %s %s " % ( port_path , p ort, variant ) )
if not query_p ackage( module , port_path , package ) :
module . fail_json ( msg = " failed to install %s : %s " % ( package , out ) )
if not query_p ort( module , port_path , port ) :
module . fail_json ( msg = " Failed to install %s : %s " % ( port , err ) )
install_c + = 1
if install_c > 0 :
module . exit_json ( changed = True , msg = " installed %s package (s)" % ( install_c ) )
module . exit_json ( changed = True , msg = " Installed %s port (s)" % ( install_c ) )
module . exit_json ( changed = False , msg = " package (s) already present" )
module . exit_json ( changed = False , msg = " Port (s) already present" )
def activate_p ackages( module , port_path , package s) :
""" Activate a p ackage if it' s inactive. """
def activate_p orts( module , port_path , port s) :
""" Activate a p ort if it' s inactive. """
activate_c = 0
for p ackage in package s:
if not query_p ackage( module , port_path , package ) :
module . fail_json ( msg = " failed to activate %s , package(s) not present " % ( package ) )
for p ort in port s:
if not query_p ort( module , port_path , port ) :
module . fail_json ( msg = " Failed to activate %s , port(s) not present " % ( port ) )
if query_p ackage( module , port_path , package , state = " active " ) :
if query_p ort( module , port_path , port , state = " active " ) :
continue
rc , out , err = module . run_command ( " %s activate %s " % ( port_path , p ackage ) )
rc , out , err = module . run_command ( " %s activate %s " % ( port_path , p ort ) )
if not query_p ackage( module , port_path , package , state = " active " ) :
module . fail_json ( msg = " failed to activate %s : %s " % ( package , out ) )
if not query_p ort( module , port_path , port , state = " active " ) :
module . fail_json ( msg = " Failed to activate %s : %s " % ( port , err ) )
activate_c + = 1
if activate_c > 0 :
module . exit_json ( changed = True , msg = " activated %s package (s)" % ( activate_c ) )
module . exit_json ( changed = True , msg = " Activated %s port (s)" % ( activate_c ) )
module . exit_json ( changed = False , msg = " package (s) already active" )
module . exit_json ( changed = False , msg = " Port (s) already active" )
def deactivate_p ackages( module , port_path , package s) :
""" Deactivate a p ackage if it' s active. """
def deactivate_p orts( module , port_path , port s) :
""" Deactivate a p ort if it' s active. """
deactivated_c = 0
for p ackage in package s:
if not query_p ackage( module , port_path , package ) :
module . fail_json ( msg = " failed to activate %s , package(s) not present " % ( package ) )
for p ort in port s:
if not query_p ort( module , port_path , port ) :
module . fail_json ( msg = " Failed to deactivate %s , port(s) not present " % ( port ) )
if not query_p ackage( module , port_path , package , state = " active " ) :
if not query_p ort( module , port_path , port , state = " active " ) :
continue
rc , out , err = module . run_command ( " %s deactivate %s " % ( port_path , p ackage ) )
rc , out , err = module . run_command ( " %s deactivate %s " % ( port_path , p ort ) )
if query_p ackage( module , port_path , package , state = " active " ) :
module . fail_json ( msg = " failed to deactivated %s : %s " % ( package , out ) )
if query_p ort( module , port_path , port , state = " active " ) :
module . fail_json ( msg = " Failed to deactivate %s : %s " % ( port , err ) )
deactivated_c + = 1
if deactivated_c > 0 :
module . exit_json ( changed = True , msg = " deactivated %s package (s)" % ( deactivated_c ) )
module . exit_json ( changed = True , msg = " Deactivated %s port (s)" % ( deactivated_c ) )
module . exit_json ( changed = False , msg = " package (s) already inactive" )
module . exit_json ( changed = False , msg = " Port (s) already inactive" )
def main ( ) :
module = AnsibleModule (
argument_spec = dict (
name = dict ( aliases = [ " p kg " ] , required = True ) ,
name = dict ( aliases = [ " p ort " ] , required = True , type = ' list ' ) ,
state = dict ( default = " present " , choices = [ " present " , " installed " , " absent " , " removed " , " active " , " inactive " ] ) ,
update_cache = dict ( default = " no " , aliases = [ " update-cache " ] , type = ' bool ' )
update_ports = dict ( aliases = [ " update_cache " ] , default = " no " , type = ' bool ' ) ,
variant = dict ( aliases = [ " variants " ] , default = None , type = ' str ' )
)
)
@ -206,22 +231,24 @@ def main():
p = module . params
if p [ " update_cache " ] :
update_package_db ( module , port_path )
if p [ " update_ports " ] :
sync_ports ( module , port_path )
pkgs = p [ " name " ]
pkgs = p [ " name " ] . split ( " , " )
variant = p [ " variant " ]
if p [ " state " ] in [ " present " , " installed " ] :
install_p ackage s( module , port_path , pkgs )
install_p ort s( module , port_path , pkgs , variant )
elif p [ " state " ] in [ " absent " , " removed " ] :
remove_p ackage s( module , port_path , pkgs )
remove_p ort s( module , port_path , pkgs )
elif p [ " state " ] == " active " :
activate_p ackage s( module , port_path , pkgs )
activate_p ort s( module , port_path , pkgs )
elif p [ " state " ] == " inactive " :
deactivate_p ackage s( module , port_path , pkgs )
deactivate_p ort s( module , port_path , pkgs )
if __name__ == ' __main__ ' :