Repository: ansible-collections/community.general

Issue:
openbsd_pkg: No progress meter: failed termcap lookup on xterm-kitty
### Summary

I run my playbooks under `kitty` terminal which exports the varaible `TERM=xterm-kitty`. Since `pkg_add` wants to look up the terminal on its own system to learn how to show the progress meter, it warns via stderr "No progress meter: failed termcap lookup on xterm-kitty". The command still works and the packages are installed, though the task detects the warning in text as a failure.

Overriding the env var `TERM: dumb` to the task fixes the issue, eg.:

```yaml
- name: Install rsync
  hosts: all
  become: yes
  tasks:
    - name: Install rsync
      community.general.openbsd_pkg:
        name: rsync--
        state: present
      environment:
        TERM: dumb
```

To free the task from clutter and an environment-based footgun, is it possible to include this in the task itself?

### Issue Type

Bug Report

### Component Name

openbsd_pkg

### Ansible Version

```console (paste below)
$ ansible --version
ansible [core 2.14.1]
  config file = None
  configured module search path = ['/home/russky/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/russky/.local/lib/python3.10/site-packages/ansible
  ansible collection location = /home/russky/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/russky/.local/bin/ansible
  python version = 3.10.9 (main, Dec  7 2022, 13:47:07) [GCC 12.2.0] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True
```


### Community.general Version

```console (paste below)
$ ansible-galaxy collection list community.general
community.general 6.1.0
```


### Configuration

```console (paste below)
$ ansible-config dump --only-changed
CONFIG_FILE() = None
```


### OS / Environment

host: Debian bookworm/sid Linux 6.0.0-4
remote: OpenBSD 7.2

### Steps to Reproduce

Any play that prompts the remote to install a new package

```yaml (paste below)
---
- name: Install some package
  hosts: all
  become: yes
  tasks:
    - name: Install rsync
      community.general.openbsd_pkg:
        name: rsync--
        state: present
```


### Expected Results

The task reports itself as OK (changed)
```
changed: [example.org]
```

### Actual Results

The task falsely reports itself as fatal
```console (paste below)
fatal: [example.org]: FAILED! => {"build": false, "changed": false, "msg": "No progress meter: failed termcap lookup on xterm-kitty\n", "name": ["rsync--"], "state": "present"}
```


### Code of Conduct

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


Discussion:
Files identified in the description:
* [`plugins/modules/openbsd_pkg.py`](https://github.com/ansible-collections/community.general/blob/main/plugins/modules/openbsd_pkg.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 @JoergFiedler @MacLemon @bcoca @dch @eest @jasperla @mekanix @opoplawski @overhacked @tuxillo
[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Hello,

Thank you for the report, as well as finding a workaround to the issue!

I think I agree that making openbsd_pkg set something "good enough" is the preferred solution, but I am not sure what TERM setting that would be. I have posted a question to the tech@openbsd.org mailing list to see if someone involved with the pkg tools have an opinion: https://marc.info/?l=openbsd-tech&m=167290482630534&w=2
Looking at pkg_add(1), You should be able to disable the progress meter with the flag `-x`:

```sh
$ TERM=xterm-kitty doas pkg_add rsync
No progress meter: failed termcap lookup on xterm-kitty
quirks-6.42 signed on 2023-01-03T17:17:11Z
Ambiguous: choose package for rsync
a	0: <None>
	1: rsync-3.2.5pl0
	2: rsync-3.2.5pl0-iconv
Your choice: ^Cpkg_add: Caught SIGINT

$ TERM=xterm-kitty doas pkg_add -x rsync
quirks-6.42 signed on 2023-01-03T17:17:11Z
Ambiguous: choose package for rsync
a	0: <None>
	1: rsync-3.2.5pl0
	2: rsync-3.2.5pl0-iconv
Your choice: 1
The following new rcscripts were installed: /etc/rc.d/rsyncd
See rcctl(8) for details.
```
Ahh scratch what I said earlier, I misread the mail (I thought you were asking to disable the progress meter entirely instead of finding an appropriate TERM) -- looking through the source code the progress meter is required to detect the package was installed.

Resolving patch:
diff --git a/plugins/modules/openbsd_pkg.py b/plugins/modules/openbsd_pkg.py
--- a/plugins/modules/openbsd_pkg.py
+++ b/plugins/modules/openbsd_pkg.py
@@ -152,7 +152,11 @@ def execute_command(cmd, module):
     # This makes run_command() use shell=False which we need to not cause shell
     # expansion of special characters like '*'.
     cmd_args = shlex.split(cmd)
-    return module.run_command(cmd_args)
+
+    # We set TERM to 'dumb' to keep pkg_add happy if the machine running
+    # ansible is using a TERM that the managed machine does not know about,
+    # e.g.: "No progress meter: failed termcap lookup on xterm-kitty".
+    return module.run_command(cmd_args, environ_update={'TERM': 'dumb'})
 
 
 # Function used to find out if a package is currently installed.


Test patch: