ansible-collections / community.general

Ansible Community General Collection
https://galaxy.ansible.com/ui/repo/published/community/general/
GNU General Public License v3.0
836 stars 1.53k forks source link

django_command: cannot find python executable #8884

Open Hraesvelg opened 2 months ago

Hraesvelg commented 2 months ago

Summary

When i try to run django_commande to run migrate i got :

TASK [quasm.django_quasm : Run Django database migrations] ***** fatal: []: FAILED! => {"changed": false, "msg": "Failed to find required executable \"python\" in paths: /:/:/:/:/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"}

Issue Type

Bug Report

Component Name

django_command

Ansible Version

$ ansible --version
ansible [core 2.17.3]

Community.general Version

$ ansible-galaxy collection list community.general
Collection        Version
----------------- -------
community.general 9.4.0

Configuration

$ ansible-config dump --only-changed

OS / Environment

Ubuntu

Steps to Reproduce

- name: Run Django database migrations
  community.general.django_command:
    command: migrate
    settings: "{{ django_settings_module }}"
    pythonpath: "{{ virtualenv_path }}/bin"
    venv: "{{ virtualenv_path }}"

Expected Results

i expect migrate is applied on database

Actual Results

Code of Conduct

ansibullbot commented 2 months ago

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

ansibullbot commented 2 months ago

cc @russoz click here for bot help

russoz commented 2 months ago

Hi @Hraesvelg thanks for reporting this. I believe that issue derives from Ubuntu having only python3 and not python in its default path. Whilst the module doe snot support passing a specific python executable, it does support virtualenvs, so that might be used as a workaround.

The module will need some adjusting to allow passing the python executable, and I am not sure yet when I will be able to spend time on this. Will keep you posted through this issue.

Hraesvelg commented 2 months ago

Hi @russoz ,

i expected that venv argument will be enough to found python executable... i don't understand why command need a python from system...

i found a workaround but what is your suggestion ?

---

- hosts: ...

  environment:
    PATH: "{{ virtualenv_path }}/bin:{{ ansible_env.PATH }}"

or another idea is to create personnal.community.general custom to replace python by python3 ...

russoz commented 2 months ago

Hi @Hraesvelg , I clearly need to improve the quality of the tests on this module - the idea is to expand to a set of django-related modules, so I am glad we caught this early on, so that it will be smooth for the future modules. Thanks again for pointing it out, I should be able to write more extensive testing and have this fixed before the 9.5.0 release.

russoz commented 2 months ago

Hi @Hraesvelg

I reviewed the code and spotted two bugs, for which I submitted the PR above. Could you please test that it actually solves your problem? you can test it by installing the collection from the PR branch with:

$ ansible-galaxy collection install git+https://github.com/russoz-ansible/community.general.git,8884-django-command-fix

That being said, I could not help but notice that in the Steps to Reproduce section of your report you wrote this:

- name: Run Django database migrations
  community.general.django_command:
    command: migrate
    settings: "{{ django_settings_module }}"
    pythonpath: "{{ virtualenv_path }}/bin"
    venv: "{{ virtualenv_path }}"

It must be noted that pythonpath does not represent the PATH for the python binary. It is rather a proxy to PYTHONPATH, or more specifically in this case, it is passed in command line to the django-admin command as the parameter --pythonpath. This parameter is meant to set paths in the filesytem from which Python will search for its packages. In the case of this module, we use it to set the path from which Python will find the settings module. I have added some integration tests to that PR, do take a look for examples on how to use it. Even with the fix, the module is unlikely to work with pythonpath set like that.

russoz commented 2 months ago

See https://docs.djangoproject.com/en/5.0/ref/django-admin/#cmdoption-pythonpath for more details on PYTHONPATH/--pythonpath.

russoz commented 2 months ago

@Hraesvelg please confirm it has solved the problem. If not, please reopen this issue. Thanks

Hraesvelg commented 4 weeks ago

Hi @russoz , sorry for the delay, i tested with community.general 9.5.0 and issue stay alive :

- name: Run Django database migrations
  community.general.django_command:
    command: migrate
    settings: "{{ django_settings_module }}"
    venv: "{{ virtualenv_path }}"

i tested also with pythonpath: "{{ virtualenv_path }}/bin" but without effect :

FAILED! => {"changed": false, "msg": "Failed to find required executable \"python\" in paths: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"}

russoz commented 4 weeks ago

Hi @russoz , sorry for the delay, i tested with community.general 9.5.0 and issue stay alive :

- name: Run Django database migrations
  community.general.django_command:
    command: migrate
    settings: "{{ django_settings_module }}"
    venv: "{{ virtualenv_path }}"

Would you please share what you have used for settings and venv? Feel free to obfuscate any sensitive info but I need to have a general idea of what is being used.

Also, qhile at it, please share the path and/or directory structure where the django project is. Particularly, the settings.py file.

i tested also with pythonpath: "{{ virtualenv_path }}/bin" but without effect :

As mentioned before, pythonpath is not meant to indicate the binaries' path. Please see previous comments.

Hraesvelg commented 3 weeks ago

Hi, you are right pythonpath is useless for me.

settings can be a classical value my_project.settings.dev (you can consider default settings from new django project.) venv is also a simple absolute path like /opt/my_project/pyr-my_project/

russoz commented 2 weeks ago

Maybe not useless, but we could make some adjustment. Let's say your file structure is like:

/home/user/projects/my_project/
    settings/
        dev.py

And your venv is /opt/my_project/pyr-my_project/ like you wrote. For that setup the command should work with:

- name: Run Django database migrations
  community.general.django_command:
    command: migrate
    pythonpath: /home/user/projects
    settings: my_project.settings.dev
    venv: /opt/my_project/pyr-my_project/

Although personally I would prefer it like:

- name: Run Django database migrations
  community.general.django_command:
    command: migrate
    pythonpath: /home/user/projects/my_project
    settings: settings.dev
    venv: /opt/my_project/pyr-my_project/
russoz commented 6 days ago

Hi @Hraesvelg , have you managed to make it work?