Repository: ansible-collections/community.general

Issue:
Puppet fails with `invalid byte sequence in US-ASCII`
### Summary

When executing the puppet agent via ansible on a remote host, the command fails with `invalid byte sequence in US-ASCII`.
Executing the puppet agent on the remote host via a shell the command successfully ends with return code 0.

It looks due to shell environment used by ansible to execute the command the shell is not utf-8 compatible

### Issue Type

Bug Report

### Component Name

puppet

### Ansible Version

```console (paste below)
$ ansible --version
ansible [core 2.14.6]
```


### Community.general Version

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


### Configuration

```console (paste below)
$ ansible-config dump --only-changed
CALLBACKS_ENABLED = ['ansible.posix.timer', 'ansible.posix.profile_tasks', 'ansible.posix.profile_roles']
COLLECTIONS_PATHS = ['/home/niklas/Documents/inovex/cloud/inovex-cloud-ansible/external/collections']
DEFAULT_FORKS = 30
DEFAULT_STDOUT_CALLBACK = community.general.diy
ENABLE_TASK_DEBUGGER = True
HOST_KEY_CHECKING = False
RETRY_FILES_ENABLED = False
```


### OS / Environment

Local: Ubuntu 23.10
Remote: Ubuntu 22.04

### Steps to Reproduce

Because of the dependency to puppet and the puppet code I cannot give an example to reproduce the bug. The latest puppet 7 version is used as a server and agent

This is the ansible code used to invoke the module
```yaml
- name: puppet run
  community.general.puppet:
    puppetmaster: "{{ ansible_fqdn }}"
    timeout: 60m
    verbose: true
    debug: true
    logdest: all
  register: puppet_agent_result
  until: puppet_agent_result.rc == 0
```


### Expected Results

I expect that the puppet run finishes with return code 0 or 2 (puppet specifies this as success but changes were executed).

### Actual Results

From the puppet logs

```console (paste below)
Error: Could not prefetch <module>: invalid byte sequence in US-ASCII
```


### Code of Conduct

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


Discussion:
Files identified in the description:
* [`plugins/modules/puppet.py`](https://github.com/ansible-collections/community.general/blob/main/plugins/modules/puppet.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 @emonty
[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/plugins/module_utils/puppet.py b/plugins/module_utils/puppet.py
--- a/plugins/module_utils/puppet.py
+++ b/plugins/module_utils/puppet.py
@@ -107,5 +107,6 @@ def execute_func(execute, manifest):
             verbose=cmd_runner_fmt.as_bool("--verbose"),
         ),
         check_rc=False,
+        force_lang=module.params["environment_lang"],
     )
     return runner
diff --git a/plugins/modules/puppet.py b/plugins/modules/puppet.py
--- a/plugins/modules/puppet.py
+++ b/plugins/modules/puppet.py
@@ -116,6 +116,15 @@
       - Whether to print file changes details
     type: bool
     default: false
+  environment_lang:
+    description:
+      - The lang environment to use when running the puppet agent.
+      - The default value, V(C), is supported on every system, but can lead to encoding errors if UTF-8 is used in the output
+      - Use V(C.UTF-8) or V(en_US.UTF-8) or similar UTF-8 supporting locales in case of problems. You need to make sure
+        the selected locale is supported on the system the puppet agent runs on.
+    type: str
+    default: C
+    version_added: 8.6.0
 requirements:
 - puppet
 author:
@@ -208,6 +217,7 @@ def main():
             debug=dict(type='bool', default=False),
             verbose=dict(type='bool', default=False),
             use_srv_records=dict(type='bool'),
+            environment_lang=dict(type='str', default='C'),
         ),
         supports_check_mode=True,
         mutually_exclusive=[


Test patch: