Repository: ansible-collections/community.general

Issue:
Gem: fails to recognize "default" packages as installed
#### SUMMARY

The `gem` module fails to  recognize "default" packages as installed, due to a mismatch between the `gem` command output and the parsing code in this module.

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME

gem

##### ANSIBLE VERSION

```
ansible 2.9.12
  config file = /Users/vlad/reannz/code/ansible-playbooks/ansible.cfg
  configured module search path = ['/Users/vlad/reannz/code/ansible-playbooks/library']
  ansible python module location = /usr/local/miniconda3/envs/ansible-test-py38/lib/python3.8/site-packages/ansible
  executable location = /usr/local/miniconda3/envs/ansible-test-py38/bin/ansible
  python version = 3.8.3 (default, May 19 2020, 13:54:14) [Clang 10.0.0 ]
```

##### CONFIGURATION

Empty (no overrides needed to reproduce)
```paste below
```

##### OS / ENVIRONMENT

Linux Ubuntu 18.04

Ruby 2.7.1 (installed via ruby-build)

##### STEPS TO REPRODUCE

Ubuntu 18.04 with Ruby 2.7.1 installed into /usr/local/rbenv/versions/2.7.1 - comes with bundler 2.1.4 installed into `/usr/local/rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/` as the default package.

Invoke the `gem` module asking it to install bundler 2.1.4 
```yaml
- name: Running the playbook
  hosts: all
  become: yes
  gather_facts: no

  tasks:
    - name: Install bundler
      gem:
          name: bundler
          version: "2.1.4"
          executable: "/usr/local/rbenv/versions/2.7.1/bin/gem"
          user_install: False
          install_dir: "/tmp"
```

##### EXPECTED RESULTS

Expected task to do nothing - `changed: no`

##### ACTUAL RESULTS

Task installs the gem (bundler) into target location, despite the gem already being present.

```paste below
TASK [Install bundler] *****************************************************************************************************************************************************************************************
task path: /private/tmp/ansible/playbook.yml:8
changed: [testhost] => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"}, "changed": true, "name": "bundler", "state": "present", "version": "2.1.4"}
META: ran handlers
META: ran handlers

PLAY RECAP *****************************************************************************************************************************************************************************************************
testhost : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
```

There was already an existing PR for this issue against ansible main repo: https://github.com/ansible/ansible/pull/57331
I'll file a new PR against this repo shortly.




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

If these files are inaccurate, 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/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @johanwiren
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/plugins/modules/packaging/language/gem.py b/plugins/modules/packaging/language/gem.py
--- a/plugins/modules/packaging/language/gem.py
+++ b/plugins/modules/packaging/language/gem.py
@@ -157,7 +157,7 @@ def get_installed_versions(module, remote=False):
     (rc, out, err) = module.run_command(cmd, environ_update=environ, check_rc=True)
     installed_versions = []
     for line in out.splitlines():
-        match = re.match(r"\S+\s+\((.+)\)", line)
+        match = re.match(r"\S+\s+\((?:default: )?(.+)\)", line)
         if match:
             versions = match.group(1)
             for version in versions.split(', '):


Test patch: