Repository: ansible-collections/community.general

Issue:
community.general.snap fails on 7.3.0 when list of packages is empty
### Summary

When attempting to install a list of snap packages, if the list of snap packages is empty, an error is thrown and the playbook execution fails.

### Issue Type

Bug Report

### Component Name

snap

### Ansible Version

```console (paste below)
$ ansible --version
ansible [core 2.14.5]
  config file = /home/<snip>/playbooks/ansible.cfg
  configured module search path = ['/usr/share/ansible/plugins/modules', '/home/<snip>/playbooks/library']
  ansible python module location = /home/<snip>/.local/lib/python3.10/site-packages/ansible
  ansible collection location = /home/<snip>/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/<snip>/.local/bin/ansible
  python version = 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True
```


### Community.general Version

```console (paste below)
$ ansible-galaxy collection list community.general
# /home/<snip>/.ansible/collections/ansible_collections
Collection        Version
----------------- -------
community.general 7.3.0

# /home/<snip>/.local/lib/python3.10/site-packages/ansible_collections
Collection        Version
----------------- -------
community.general 6.5.0
```


### Configuration

```
 $ ansible-config dump --only-changed
CONFIG_FILE() = /home/<snip>/playbooks/ansible.cfg
DEFAULT_FORCE_HANDLERS(/home/<snip>/playbooks/ansible.cfg) = True
DEFAULT_MODULE_PATH(/home/<snip>/playbooks/ansible.cfg) = ['/usr/share/ansible/plugins/modules', '/home/<snip>/playbooks/library']
DEFAULT_STDOUT_CALLBACK(/home/<snip>/playbooks/ansible.cfg) = yaml
DISPLAY_SKIPPED_HOSTS(/home/<snip>/playbooks/ansible.cfg) = False
INJECT_FACTS_AS_VARS(/home/<snip>/playbooks/ansible.cfg) = True
RETRY_FILES_ENABLED(/home/<snip>/playbooks/ansible.cfg) = False
TRANSFORM_INVALID_GROUP_CHARS(/home/<snip>/playbooks/ansible.cfg) = always
```

### OS / Environment

Ubuntu 22.04

### Steps to Reproduce

defaults/main.yml:
```yaml
snap_packages: []
```

tasks/main.yml:
```yaml
- name: install snap packages
  community.general.snap:
    name: "{{ snap_packages }}"
 ```

### Expected Results

I expected the playbook to complete successfully (even though no packages will be installed since the list is empty).

### Actual Results

The playbook fails as follows:
```console (paste below)
 Module failed with exception: list index out of range
```

A workaround is to add the following:
```yaml
- name: install snap packages
  community.general.snap:
    name: "{{ snap_packages }}"
  when: snap_packages.0 is defined
 ```

However this isn't necessary for other similar commands like `ansible.builtin.apt` and worked on older versions of `community.general`, so this seems like a bug.

### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct


Discussion:
Files identified in the description:
* [`plugins/modules/snap.py`](https://github.com/ansible-collections/community.general/blob/main/plugins/modules/snap.py)

If these files are incorrect, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @angristan @vcarceler
[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: notify --->
CC @russoz

Resolving patch:
diff --git a/plugins/modules/snap.py b/plugins/modules/snap.py
--- a/plugins/modules/snap.py
+++ b/plugins/modules/snap.py
@@ -326,11 +326,13 @@ def process(rc, out, err):
                                                               if x.startswith("warning: no snap found")]))
             return process_(rc, out, err)
 
-        with self.runner("info name", output_process=process) as ctx:
-            try:
-                names = ctx.run(name=snaps)
-            finally:
-                self.vars.snapinfo_run_info.append(ctx.run_info)
+        names = []
+        if snaps:
+            with self.runner("info name", output_process=process) as ctx:
+                try:
+                    names = ctx.run(name=snaps)
+                finally:
+                    self.vars.snapinfo_run_info.append(ctx.run_info)
         return names
 
     def snap_status(self, snap_name, channel):


Test patch:
diff --git a/tests/integration/targets/snap/tasks/main.yml b/tests/integration/targets/snap/tasks/main.yml
--- a/tests/integration/targets/snap/tasks/main.yml
+++ b/tests/integration/targets/snap/tasks/main.yml
@@ -19,3 +19,5 @@
       ansible.builtin.include_tasks: test_dangerous.yml
     - name: Include test_3dash
       ansible.builtin.include_tasks: test_3dash.yml
+    - name: Include test_empty_list
+      ansible.builtin.include_tasks: test_empty_list.yml
diff --git a/tests/integration/targets/snap/tasks/test_empty_list.yml b/tests/integration/targets/snap/tasks/test_empty_list.yml
new file mode 100644
--- /dev/null
+++ b/tests/integration/targets/snap/tasks/test_empty_list.yml
@@ -0,0 +1,14 @@
+---
+# Copyright (c) Ansible Project
+# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+- name: Empty list present
+  community.general.snap:
+    name: []
+    state: present
+
+- name: Empty list absent
+  community.general.snap:
+    name: []
+    state: absent