Skip to content

Instantly share code, notes, and snippets.

@pwalkr
Last active May 31, 2023 21:13
Show Gist options
  • Save pwalkr/311e4ce16d6aedb5be50f159c8b5cc9c to your computer and use it in GitHub Desktop.
Save pwalkr/311e4ce16d6aedb5be50f159c8b5cc9c to your computer and use it in GitHub Desktop.
Ansible not detecting locale after system update
ansible --version
ERROR: Ansible requires the locale encoding to be UTF-8; Detected None.

Hacked with explicit assignment of LC_ALL:

LC_ALL=C.UTF-8 ansible --version
ansible [core 2.14.1]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/strayguin/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.10/site-packages/ansible
  ansible collection location = /home/strayguin/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.10.8 (main, Nov  1 2022, 14:18:21) [GCC 12.2.0] (/usr/bin/python)
  jinja version = 3.1.2
  libyaml = True

Current locale settings in /etc/locale.conf

LANG=en_US.UTF-8
LANGUAGE=en_US
LC_ALL=C

Where's this error come from? https://github.com/ansible/ansible/blob/v2.14.1/lib/ansible/cli/__init__.py#L58

    try:
        locale.setlocale(locale.LC_ALL, '')
        dummy, encoding = locale.getlocale()
    except (locale.Error, ValueError) as e:
        raise SystemExit(
            'ERROR: Ansible could not initialize the preferred locale: %s' % e
        )

    if not encoding or encoding.lower() not in ('utf-8', 'utf8'):
        raise SystemExit('ERROR: Ansible requires the locale encoding to be UTF-8; Detected %s.' % encoding)

This change was added recently: ansible/ansible#78175, ansible/ansible#78175 (comment)

With the latest change, C is not allowed.

What should locales be for my system? https://wiki.archlinux.org/title/locale#LC_ALL:_troubleshooting

The locale set for this variable will always override LANG and all the other LC_* variables, whether they are set or not. If LC_ALL is set to C, it will also override LANGUAGE.

LC_ALL is the only LC_* variable which cannot be set in locale.conf files: it is meant to be used only for testing or troubleshooting purposes, for example in /etc/profile.

~ https://wiki.archlinux.org/title/locale#LC_ALL:_troubleshooting

Fixed by unsetting LC_ALL

LC_ALL= ansible --version
ansible [core 2.14.1]
...

Applied to locale.conf

LANG=en_US.UTF-8
LANGUAGE=en_US
@berendt
Copy link

berendt commented Mar 12, 2023

Thanks. Saved my Sunday :)

@notjustanyjoe
Copy link

Thanks for this. My kickstart builds all started failing with the rhel8.8 update since they utilize ansible-pull in the post install section. Lifesaver.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment