Repository: ansible/ansible

Issue:
Fixes #29818: chkconfig localizes messages, ansible fails to recognise
##### SUMMARY
In the particular case of executin "chkconfig --list NAME", ansible
checks the stderr looking for a particular english message.
This message is different in other languages, Spanish for example
(although it have been corrected in the latests versions).

This change forces the locale so messages are consistent.

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
- Bugfix Pull Request

##### COMPONENT NAME
lib/ansible/modules/system/service.py

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
master branch
```
"service: enable=yes/no" with spanish CentOS6 servers is broken
From @vicendominguez on 2016-04-14T16:46:43Z
##### ISSUE TYPE
- Documentation Report
##### COMPONENT NAME

service core module
##### ANSIBLE VERSION

```
ansible 2.0.1.0 
(but I wouldn't discard other versions)

```
##### OS / ENVIRONMENT

CentOS 6.7 
(at least)
##### SUMMARY

On CentOS6 at least, a spanish translator did a bad translating in some  error messages returned to the command line by the chkconfig tool which break the "service enable=yes / no" option in Ansible.
It 's not a direct Ansible issue but Ansible is affected.
##### STEPS TO REPRODUCE

```
- name: Start myservice and enable it
  service: name=myservice state=started enabled=yes
```
##### EXPECTED RESULTS

```
TASK [role : Start myservice and enable it ] ***********************************************
ok: [myservice]
```
##### ACTUAL RESULTS

<!--- What actually happened? If possible run with high verbosity (-vvvv) -->

```
TASK [role : Start myservice and enable it ] ***********************************************
fatal: [myservice]: FAILED! => {"changed": false, "failed": true, "msg": "service myservice does not support chkconfig"}
```
##### WHY

Line https://github.com/ansible/ansible-modules-core/blob/devel/system/service.py#L702 waits a concrete answer to do the regexp but, in Spanish, that line is bad translated changing the "--add" statement to "--añada" (doesn't make sense), so you see something like:

"El servicio myservice soporta chkconfig, pero no está registrado (ejecute \n'chkconfig -- añada myservice')\n"

the sentence "chkconfig --add" doesn't appear there and that breaks the "if". So the "enable" feature is broken.
##### WORKAROUND

`export LANG="en"` 

in the terminal, before starting to work with any sofware which uses Ansible inside.

Copied from original issue: ansible/ansible-modules-core#3438



Discussion:

From @ansibot on 2016-04-14T16:46:43Z

<!-- @vicendominguez: Thanks for filing this documentation report! -->

@grossws ping, this issue is waiting for your response.
[click here for bot help](https://github.com/jctanner/ansibullbot/blob/ISSUE_TRIAGE_CLASS/ISSUE_HELP.md)

<!--- boilerplate: issue_bug_new --->

[service.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/system/service.py#L734) should force the locale before checking the stdout/err of the chkconfig command as suggested by the deprecation message in DEFAULT_MODULE_LANG: ``Modules are coded to set their own locale if needed for screenscraping``

Resolving patch:
diff --git a/lib/ansible/modules/system/service.py b/lib/ansible/modules/system/service.py
--- a/lib/ansible/modules/system/service.py
+++ b/lib/ansible/modules/system/service.py
@@ -206,7 +206,10 @@ def execute_command(self, cmd, daemonize=False):
 
         # Most things don't need to be daemonized
         if not daemonize:
-            return self.module.run_command(cmd)
+            # chkconfig localizes messages and we're screen scraping so make
+            # sure we use the C locale
+            lang_env = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C')
+            return self.module.run_command(cmd, environ_update=lang_env)
 
         # This is complex because daemonization is hard for people.
         # What we do is daemonize a part of this module, the daemon runs the


Test patch: