How do I add PATH to the ansible_galaxy_install module?

1k Views Asked by At

When attempting to install an ansible collection using ansible_galaxy_install, I receive the following error:

"Failed to find required executable \"ansible-galaxy\" in paths: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"

It is my understanding that ansible_galaxy_install is looking for ansible-galaxy on the the above PATH on the managed host and that I need to add the PATH where ansible-galaxy is installed.

which ansible-galaxy # managed host
/home/<user>/.local/bin/ansible-galaxy

- name: install collections | sbaerlocher.virtualization | virtualbox
  environment:
    PATH: "{{ lookup('env', 'HOME') }}/.local/bin:{{ ansible_env.PATH }}"
  community.general.ansible_galaxy_install:
    type: collection
    name: sbaerlocher.virtualization

This overcomes the first error, but then I receive a second error that the "ansible" module cannot be found (which is also located at /home/<user>/.local/bin/). It appears that the subsequent call is not respecting the PATH I provided.

fatal: [lenovo_laptop]: FAILED! => {"changed": false, "cmd": "/home/<user>/.local/bin/ansible-galaxy --version", "msg": "Traceback (most recent call last):\n  File \"/home/<user>/.local/bin/ansible-galaxy\", line 44, in <module>\n    from ansible import context\nModuleNotFoundError: No module named 'ansible'", "rc": 1, "stderr": "Traceback (most recent call last):\n  File \"/home/<user>/.local/bin/ansible-galaxy\", line 44, in <module>\n    from ansible import context\nModuleNotFoundError: No module named 'ansible'\n", "stderr_lines": ["Traceback (most recent call last):", "  File \"/home/<user>/.local/bin/ansible-galaxy\", line 44, in <module>", "    from ansible import context", "ModuleNotFoundError: No module named 'ansible'"], "stdout": "", "stdout_lines": []}

I also tried creating a links the ansible-galaxy and ansible, but it did not overcome the second errro.

- name: install collections | workaround | failed to find required executable | create symbolic links to executables required to install collections
  become: yes
  file:
    src: "{{ lookup('env', 'HOME') }}/.local/bin/{{ item }}"
    path: /usr/bin/{{ item }}
    state: link
    mode: u=rwx,g=rwx,o=rx # '0775'
  loop:
    - ansible-galaxy
    - ansible

How can I get the ansible_galaxy_install module to use my PATH to find ansible-galaxy and ansible?

1

There are 1 best solutions below

0
On

The issue was a result of mixing pip and apt installations of ansible, which use different file locations. Ran pip uninstall ansible and sudo apt install ansible to make my environment consistent. The search path for modules are displayed by ansible --version.

Credit to Vladimir Botka [https://www.mail-archive.com/[email protected]/msg59830.html][1]