0

Hello In my playbook I try to determine the locale of the target host using the following command

        - name: get locale info
          command: printenv LANG
          register: my_loc

The strange thing is, the result changes depending on which control host I execute the playbook. If I run it from my CentOS 8 box i will get as result the value en_US.UTF-8, If I run it a CentOS7 machine I will get en_US.utf8. These values are the same as i would get in a shell of the console host. But I would expect to that the vales are computed on the target machine and thus should be the same independently from which control host I execute the playbook.

On the CentOS7 machine

[me]$ ansible --version
ansible 2.9.25
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/me/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Nov 16 2020, 22:23:17) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
[me]$ printenv LANG
en_US.utf8

On the CentOS8 machine

[me]$ ansible --version
ansible 2.9.25
  config file = /home/me/ansible-toolbox/ansible.cfg
  configured module search path = ['/home/me/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Sep 21 2021, 20:17:36) [GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]
[me]$ printenv LANG
en_US.UTF-8

The playbook looks like this

# playbook for experiments
---

 - name: setup  servers 
   hosts: all
   tasks:
   
        # make sure the system local is set to american English
    - name:  setting up  as centos server
      command: printenv LANG
      register: my_loc

    - name: show locale
      debug:
        var: my_loc.stdout

And I run it with the command

ansible-playbook  -i 172.19.1.5  area51.yml 

The output is in one case


PLAY [setup  servers] *****************************************************************************

TASK [Gathering Facts] ****************************************************************************
ok: [172.19.1.5]

TASK [setting up  as centos server] ***************************************************************
changed: [172.19.1.5]

TASK [show locale] ********************************************************************************
ok: [172.19.1.5] => {
    "my_loc.stdout": "en_US.UTF-8"
}

PLAY RECAP ****************************************************************************************
172.19.1.5                 : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


And in the other case


PLAY [setup  servers] ***************************************************************************************

TASK [Gathering Facts] **************************************************************************************
ok: [172.19.1.5]

TASK [setting up  as centos server] *************************************************************************
changed: [172.19.1.5]

TASK [show locale] ******************************************************************************************
ok: [172.19.1.5] => {
    "my_loc.stdout": "en_US.utf8"
}

PLAY RECAP **************************************************************************************************
172.19.1.5                 : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

I have tried the same thing with running the command ssh user@172.19.1.9 printenv LANG and got the same result thus I think it is not a problem with ansible.

grafra
  • 411
  • 6
  • 16
  • 1
    Please make this [MCVE](/help/mcve) with a complete playbook, the inventory your are using, the variables set for your group/host, and a complete output in debug mode. As is, my first guess is that your example task is actually running on localhost (i.e. the controller) and not on the target host. The most obvious reason to get this when you think you are running on a remote host is when you set `connection: local` on your play or somewhere in your inventory/command line. – Zeitounator Oct 01 '21 at 16:24
  • 1
    Also mind that the local configuration of the two machines acting as controller might forward those environment settings, ending up with this behaviour. Related link https://stackoverflow.com/a/29609673/2123530 – β.εηοιτ.βε Oct 01 '21 at 19:53
  • added the playbook – grafra Oct 06 '21 at 09:02

1 Answers1

0

The problem her seems to be that ssh transfers the locale settings from the control host to the remote hosts and this will in CentOS have the effect that the remote host will mirror the locale of the control host instead of using the default locale.

See

https://github.com/ansible/ansible/issues/10698

grafra
  • 411
  • 6
  • 16