Ansible: resolving ‘could not initialize the preferred locale: unsupported locale setting’

If you are getting the following error when invoking ‘ansible’, ‘ansible-playbook’, ‘ansible-galaxy’ or any of the Ansible related utilities:

ERROR: Ansible could not initialize the preferred locale: unsupported locale setting

This means Ansible cannot find a locale ending in “.UTF-8”.  Check the currently installed locales:

$ locale -a

Then export the LC_ALL variable to one of the locales ending in UTF-8

export LC_ALL=C.UTF-8

# ansible should work now
ansible --version

As a permanent fix look at: installing locales, regenerating with “dpkg-reconfigure locales“, and setting the value in “/etc/default/locale”

REFERENCES

https://stackoverflow.com/questions/14547631/python-locale-error-unsupported-locale-setting

https://github.com/NixOS/nixpkgs/issues/223151#issuecomment-1521702680

https://www.delftstack.com/howto/python/locale.error-unsupported-locale-setting/

https://linuxpip.org/fix-python-locale-error/