Search code examples
pythonansiblevirtualenvpython-venv

Ansible error python not executable if virtualenv/venv is inside home directory?


in some Ubuntu 22.04 machines I see the following Ansible error:

TASK [pbalucode.postgresql : Create database] ************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "/bin/sh: 1: /home/user/installer/ansible/bin/python3.10: Permission denied\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 126}

/home/user/installer is a virtualenv/venv created in which Ansible 2.17.3 is installed. The strange thing is, that this error occurs only if the virtualenv/venv is inside the home directory. If I install the virtualenv/venv in /tmp or /opt this error is not occurring.

Even more strange is, that the playbook did already plenty of other things, but it is always failing in the very same task (if failing). It is not failing in a Ubuntu 22.04 docker image, but in all real VMs.

The Ansible code is rather unsuspicious:

- name: Create database
  ansible.builtin.command: "{{ postgresql_bin_dir }}/initdb \
   -D {{ postgresql_pgdata }}"
  become: true
  become_user: "{{ postgresql_user_name }}"
  when: not dbinit.stat.exists

The symbolic links are ok and executable, I can run them manually fine and a lot of other Ansible tasks worked fine, too.

$ ll /home/user/installer/ansible/bin/python3.10
lrwxrwxrwx 1 user user 6 Sep  6 10:06 /home/user/installer/ansible/bin/python3.10 -> python*
$ ll /home/user/installer/ansible/bin/python
lrwxrwxrwx 1 user user 16 Sep  6 10:06 /home/user/installer/ansible/bin/python -> /usr/bin/python3*
$ ll /usr/bin/python3
lrwxrwxrwx 1 root root 10 Aug  8 12:28 /usr/bin/python3 -> python3.10*
$ ll /usr/bin/python3.10
-rwxr-xr-x 1 root root 5904936 Jul 29 16:56 /usr/bin/python3.10*
$ /home/user/installer/ansible/bin/python3.10 --version
Python 3.10.12

Any idea what is wrong here and how to fix this? Feels like a rather harsh restriction to avoid creating a virtualenv/venv inside a home directory.

There was a comment shortly asking for the mount options, they are straight forward, no special things here: /dev/mapper/ubuntu--vg-ubuntu--lv on / type ext4 (rw,relatime)


Solution

  • The issue is coming from using ansible_connection=local which changes to use the same python interpreter from the playbook also for the execution, which fails if using a virtualenv inside a home directory and using become_user without access.

    This was already filed as a bug: https://github.com/ansible/ansible/issues/76371

    But closed as expected and documented.