How: django_manage in ansible with Python3 How: django_manage in ansible with Python3 django django

How: django_manage in ansible with Python3


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.


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


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"