@ -67,6 +67,9 @@ EXAMPLES = '''
# Install package foo
# Install package foo
- pacman : name = foo state = present
- pacman : name = foo state = present
# Upgrade package foo
- pacman : name = foo state = present update_cache = yes
# Remove packages foo and bar
# Remove packages foo and bar
- pacman : name = foo , bar state = absent
- pacman : name = foo , bar state = absent
@ -85,17 +88,37 @@ import sys
PACMAN_PATH = " /usr/bin/pacman "
PACMAN_PATH = " /usr/bin/pacman "
def get_version ( pacman_output ) :
""" Take pacman -Qi or pacman -Si output and get the Version """
lines = pacman_output . split ( ' \n ' )
for line in lines :
if ' Version ' in line :
return line . split ( ' : ' ) [ 1 ] . strip ( )
return None
def query_package ( module , name , state = " present " ) :
def query_package ( module , name , state = " present " ) :
# pacman -Q returns 0 if the package is installed,
""" Query the package status in both the local system and the repository. Returns a boolean to indicate if the package is installed, and a second boolean to indicate if the package is up-to-date. """
# 1 if it is not installed
if state == " present " :
if state == " present " :
cmd = " pacman -Q %s " % ( name )
lcmd = " pacman -Qi %s " % ( name )
rc , stdout , stderr = module . run_command ( cmd , check_rc = False )
lrc , lstdout , lstderr = module . run_command ( lcmd , check_rc = False )
if lrc != 0 :
if rc == 0 :
# package is not installed locally
return True
return False , False
return False
# get the version installed locally (if any)
lversion = get_version ( lstdout )
rcmd = " pacman -Si %s " % ( name )
rrc , rstdout , rstderr = module . run_command ( rcmd , check_rc = False )
# get the version in the repository
rversion = get_version ( rstdout )
if rrc == 0 :
# Return True to indicate that the package is installed locally, and the result of the version number comparison
# to determine if the package is up-to-date.
return True , ( lversion == rversion )
return False , False
def update_package_db ( module ) :
def update_package_db ( module ) :
@ -118,7 +141,8 @@ def remove_packages(module, packages):
# Using a for loop incase of error, we can report the package that failed
# Using a for loop incase of error, we can report the package that failed
for package in packages :
for package in packages :
# Query the package first, to see if we even need to remove
# Query the package first, to see if we even need to remove
if not query_package ( module , package ) :
installed , updated = query_package ( module , package )
if not installed :
continue
continue
cmd = " pacman - %s %s --noconfirm " % ( args , package )
cmd = " pacman - %s %s --noconfirm " % ( args , package )
@ -140,7 +164,9 @@ def install_packages(module, packages, package_files):
install_c = 0
install_c = 0
for i , package in enumerate ( packages ) :
for i , package in enumerate ( packages ) :
if query_package ( module , package ) :
# if the package is installed and up-to-date then skip
installed , updated = query_package ( module , package )
if installed and updated :
continue
continue
if package_files [ i ] :
if package_files [ i ] :