Repository: ansible/ansible

Issue:
[module] packaging/os/pacman.py - empty package wants to install all groups
##### ISSUE TYPE

 - Bug Report

##### COMPONENT NAME

pacman

##### ANSIBLE VERSION

```
ansible 2.3.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.13 (default, Feb 11 2017, 12:22:40) [GCC 6.3.1 20170109]
```

##### OS / ENVIRONMENT

ArchLinux, CentOS 7

##### SUMMARY

If the `name` parameter is given an empty name, the command `pacman -Sgq` is ran, returning every available groups. Then, each group is expanded to the real list of packages, returning every single package available for install.

Line: https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/packaging/os/pacman.py#L381

##### STEPS TO REPRODUCE


```yaml
- name: install packages
  pacman:
    name: ''
    state: present
```

##### EXPECTED RESULTS

Install nothing.

##### ACTUAL RESULTS

Wants to install everything.

Note: currently gives an error since some packages are in conflict.


Discussion:
As a workaround, add to your task:

```
when: package_list | length > 0
```
cc @indrajitr
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
name is a list type option, an empty string will still appear as an 'element', use  `[]` to signify 'no packages'

Resolving patch:
diff --git a/lib/ansible/modules/packaging/os/pacman.py b/lib/ansible/modules/packaging/os/pacman.py
--- a/lib/ansible/modules/packaging/os/pacman.py
+++ b/lib/ansible/modules/packaging/os/pacman.py
@@ -143,10 +143,7 @@
     force: yes
 '''
 
-import shlex
-import os
 import re
-import sys
 
 def get_version(pacman_output):
     """Take pacman -Qi or pacman -Si output and get the Version"""
@@ -212,8 +209,6 @@ def upgrade(module, pacman_path):
 
     if rc == 0:
         regex = re.compile('([\w-]+) ((?:\S+)-(?:\S+)) -> ((?:\S+)-(?:\S+))')
-        b = []
-        a = []
         for p in data:
             m = regex.search(p)
             packages.append(m.group(1))
@@ -378,17 +373,18 @@ def expand_package_groups(module, pacman_path, pkgs):
     expanded = []
 
     for pkg in pkgs:
-        cmd = "%s -Sgq %s" % (pacman_path, pkg)
-        rc, stdout, stderr = module.run_command(cmd, check_rc=False)
-
-        if rc == 0:
-            # A group was found matching the name, so expand it
-            for name in stdout.split('\n'):
-                name = name.strip()
-                if name:
-                    expanded.append(name)
-        else:
-            expanded.append(pkg)
+        if pkg: # avoid empty strings
+            cmd = "%s -Sgq %s" % (pacman_path, pkg)
+            rc, stdout, stderr = module.run_command(cmd, check_rc=False)
+
+            if rc == 0:
+                # A group was found matching the name, so expand it
+                for name in stdout.split('\n'):
+                    name = name.strip()
+                    if name:
+                        expanded.append(name)
+            else:
+                expanded.append(pkg)
 
     return expanded
 
@@ -432,7 +428,9 @@ def main():
 
         pkg_files = []
         for i, pkg in enumerate(pkgs):
-            if re.match(".*\.pkg\.tar(\.(gz|bz2|xz|lrz|lzo|Z))?$", pkg):
+            if not pkg: # avoid empty strings
+                continue
+            elif re.match(".*\.pkg\.tar(\.(gz|bz2|xz|lrz|lzo|Z))?$", pkg):
                 # The package given is a filename, extract the raw pkg name from
                 # it and store the filename
                 pkg_files.append(pkg)


Test patch: