Setting locale in one play and have the change take effect in subsequent plays

In my playbooks, I need to set the locale, and have the changes go into effect so that in a subsequent play, when postgresql is installed it will use the appropriate locale settings.

However, if I set the locale in one play using ansible, the locale change does not take effect in subsequent plays. I know that when you’re interacting with a traditional ssh session, you need to log out and back in for a locale change to take effect. What can I do to cause the locale setting change to take effect in subsequent plays? Note that I’m using ssh for transport, with default settings.

See https://gist.github.com/4262334 for an example playbook, output, and my ansible.cfg file. Here you can see the issue being reproduced using the “ansible”

$ ansible vagrant -s -m command -a “/usr/sbin/update-locale LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8”
vagrant.nimbis.net | success | rc=0 >>

$ ansible vagrant -m command -a locale
vagrant.nimbis.net | success | rc=0 >>
LANG=C
LANGUAGE=
LC_CTYPE=“en_US”
LC_NUMERIC=“en_US”
LC_TIME=“en_US”
LC_COLLATE=“en_US”
LC_MONETARY=“en_US”
LC_MESSAGES=“en_US”
LC_PAPER=“en_US”
LC_NAME=“en_US”
LC_ADDRESS=“en_US”
LC_TELEPHONE=“en_US”
LC_MEASUREMENT=“en_US”
LC_IDENTIFICATION=“en_US”
LC_ALL=en_US

If I connect via ssh, it shows the correct thing

$ ssh vagrant locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE=“en_US.UTF-8”
LC_NUMERIC=“en_US.UTF-8”
LC_TIME=“en_US.UTF-8”
LC_COLLATE=“en_US.UTF-8”
LC_MONETARY=“en_US.UTF-8”
LC_MESSAGES=“en_US.UTF-8”
LC_PAPER=“en_US.UTF-8”
LC_NAME=“en_US.UTF-8”
LC_ADDRESS=“en_US.UTF-8”
LC_TELEPHONE=“en_US.UTF-8”
LC_MEASUREMENT=“en_US.UTF-8”
LC_IDENTIFICATION=“en_US.UTF-8”
LC_ALL=en_US.UTF-8

$ ansible --version
ansible 1.0 (devel 637883164e) last updated 2012/12/08 14:06:41 (GMT -400)

Take care,

Lorin

Try removing the controlmaster option, it will slow things down but should correctly present you with updated locale on the remote side.

I should also add, in case someone wants to fiddle with it, the
modules themselves set try to set the locale to C while they execute,
partly to avoid encoding issues.

Various underlying code here
(https://github.com/ansible/ansible/blob/devel/lib/ansible/module_common.py)
and in runner/__init__.py

Not sure if that is playing a role in things or not.

--Michael

Do you think we can pull that into a config (default C) that we can override on command line and/or playbook?

Brian Coca

You should be able to set module_lang in your config or ANSIBLE_MODULE_LANG
environment variable to change that (see
https://github.com/ansible/ansible/blob/devel/lib/ansible/constants.py).
Let us know if that doesn't work.

sf

Thanks, Brian.

In order to keep the speed boost with control master, I’m doing this as a workaround.

vars:
locale: en_US.UTF-8

tasks:

  • name: workaround to install postgresql with locale set
    shell: LANG=$locale LC_COLLATE=$locale LC_CTYPE=$locale LC_MESSAGES=$locale LC_MONETARY=$locale LC_NUMERIC=$locale LC_TIME=$locale LC_ALL=$locale apt-get install -y postgresql

But it’s good to know that I can disable controlmaster if needed to avoid doing something ugly like this.

Take care,

Lorin