Ansible Cannot 'Initialize_locale'

Jan 29, 2024 · 414 words · 2 minute read

I'm leaving a note here as I've had this problem occur a couple of times, and each time it confuses me. When I try and launch a Vagrant box with an Ansible provisioning step, or otherwise try to run an Ansible playbook I get the error: ImportError: cannot import name 'initialize_locale'. Even a simple ansible --version will trigger this error.

In full:

  Traceback (most recent call last):
    File "/home/<user>/.local/bin/ansible", line 66, in <module>
      from ansible.utils.display import Display, initialize_locale
  ImportError: cannot import name 'initialize_locale' from
  'ansible.utils.display' (/usr/lib/python3.11/site-packages/ansible/utils/display.py)

I'm currently running Manjaro Linux 23.1.3, which is derived from Arch Linux.

I install Ansible via the OS package manager; pacman, and not via pip or pipx. This may be a mistake, but it's quite convenient not having to worry about which venv I'm currently in just to run some playbooks. My use of Ansible tends not to be so advanced that I need very version dependent features.

> pacman -Q ansible
ansible 9.1.0-2

When this problem occurs it somehow appears that Ansible is installed in the ~/.local directory, but I don't know why this happens. This has happened more than once, but doing nothing more than using pacman to update the system and packages.

You can see the location in the error message above, but also by running which:

> which ansible
/home/<user>/.local/bin/ansible

The solution for me is to remove the relevant content of the ~./local directory. Once that's done (in case of doubt just rename ".local" to something else, you can name it back if something else breaks). The clue is from this Arch Linux bug report, where someone else seems to have the same issue, but like me, isn't sure how an additional 'local' version of Ansible was installed, as pacman should install it system wide.

Once that's done running which should give you the system installation, and not a user specific one:

> which ansible
/usr/bin/ansible

and you can check the version:

> ansible --version
ansible [core 2.16.2]
  config file = None
  configured module search path = ['/home/<user>/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.11/site-packages/ansible
  ansible collection location = /home/<user>/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.11.6 (main, Nov 14 2023, 09:36:21) [GCC 13.2.1 20230801] (/usr/bin/python)
  jinja version = 3.1.2
  libyaml = True

If you're confused about the versioning, why pacman says 9.1 and ansible itself seems to say 2.16, that's because of the difference between 'community package' vs 'core version dependency' versioning conventions, I have no idea why that's done that way.