Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How: django_manage in ansible with Python3

I'm following this for django manage.py module http://docs.ansible.com/django_manage_module.html

for e.g. one of my tasks looks like -

- name: Django migrate
  django_manage: command=migrate
                 app_path={{app_path}}
                 settings={{django_settings}}
  tags:
    - django

this works perfectly fine with python2(default in ubuntu) but when I try with python3-django project it throws error

failed: [123.456.200.000] => (item=school) => {"cmd": "python manage.py makemigrations --noinput school --settings=myproj.settings.production", "failed": true, "item": "school", "path": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games", "state": "absent", "syspath": ["/home/ubuntu/.ansible/tmp/ansible-tmp-1432039779.41-30449122707918", "/usr/lib/python2.7", "/usr/lib/python2.7/plat-x86_64-linux-gnu", "/usr/lib/python2.7/lib-tk", "/usr/lib/python2.7/lib-old", "/usr/lib/python2.7/lib-dynload", "/usr/local/lib/python2.7/dist-packages", "/usr/lib/python2.7/dist-packages"]}
msg: 
:stderr: Traceback (most recent call last):
  File "manage.py", line 8, in <module>
    from django.core.management import execute_from_command_line
ImportError: No module named django.core.management

from this error it seems Ansible bydefault uses Python2. can we change this to python3 or anyother workaround?

PS: pip freeze ensure that django 1.8 has installed (for python3 using pip3)

Suggestions: when I run ubuntu@ubuntu:/srv/myproj$ python3 manage.py migrate it works fine. so I'm thinking of passing command directly something like

 - name: Django migrate
   command: python3 manage.py migrate
   tags:
     - django

but how do I pass the project path or manage.py file's path, there is only an option to pass settings, something like --settings=myproject.settings.main.

can we do by passing direct command?

like image 578
micheal Avatar asked May 19 '15 13:05

micheal


3 Answers

From Ansible website http://docs.ansible.com/intro_installation.html

Python 3 is a slightly different language than Python 2 and most Python programs (including Ansible) are not switching over yet. However, some Linux distributions (Gentoo, Arch) may not have a Python 2.X interpreter installed by default. On those systems, you should install one, and set the ‘ansible_python_interpreter’ variable in inventory (see Inventory) to point at your 2.X Python. Distributions like Red Hat Enterprise Linux, CentOS, Fedora, and Ubuntu all have a 2.X interpreter installed by default and this does not apply to those distributions. This is also true of nearly all Unix systems. If you need to bootstrap these remote systems by installing Python 2.X, using the ‘raw’ module will be able to do it remotely.

like image 147
jacekbj Avatar answered Nov 20 '22 20:11

jacekbj


If you edit the shebang in the Django manage.py file to be #!/usr/bin/env python3 then you can ensure that python 3 will always be used to run your Django app.

Tried successfully with Ansible 2.3.0 and Django 1.10.5. YMMV

like image 3
PMack Avatar answered Nov 20 '22 19:11

PMack


Ansible is using python to run the django command: https://github.com/ansible/ansible-modules-core/blob/devel/web_infrastructure/django_manage.py#L237

Your only solution is thus to override the executable that will be run, for instance by changing your PATH:

- file: src=/usr/bin/python3 dest=/home/user/.local/bin/python state=link
- name: Django migrate
  django_manage: command=migrate
                 app_path={{app_path}}
                 settings={{django_settings}}
  environment:
    - PATH: "/home/user/.local/bin/:/bin:/usr/bin:/usr/local/bin"
like image 1
Régis B. Avatar answered Nov 20 '22 18:11

Régis B.