elleryq
1/8/2016 - 4:03 AM

Create Django super user in ansible

Create Django super user in ansible

#!/usr/bin/expect

set timeout -1;
spawn {{django_dir}}/venv/bin/python manage.py changepassword {{admin_user}};
expect {
    "Password:" { exp_send "{{admin_pass}}\r" ; exp_continue }
    "Password (again):" { exp_send "{{admin_pass}}\r" ; exp_continue }
    eof
}
---
# other tasks ...
- name: super user existed?
  command: echo "from django.contrib.auth import get_user_model; User = get_user_model(); print(User.objects.filter(username='{{fota_admin_user}}').count()>0)" | {{ django_dir }}/venv/bin/python ./manage.py shell
  args:
    chdir: "{{django_dir}}"
  environment:
    DJANGO_SETTINGS_MODULE: "{{settings}}"
  register: superuser_existed
- name: Create super user
  django_manage: command="createsuperuser --noinput --username={{admin_user}} --email={{admin_email}}"
                 app_path={{django_dir}}
                 virtualenv={{django_dir}}/venv
                 settings={{settings}}
  when: not superuser_existed
- name: Change password tricks
  template: src=changepassword.sh.j2 dest={{django_dir}}/changepassword.sh mode=0755
- name: Change password
  command: "{{django_dir}}/changepassword.sh"
  args:
    chdir: "{{django_dir}}"
  environment:
    DJANGO_SETTINGS_MODULE: "{{settings}}"
- name: remove changepassword.sh
  file: path="{{django_dir}}/changepassword.sh" state=absent
django_dir: /webapp/djangoapp
settings: djangoapp.settings
admin_user: admin
admin_email: admin@example.com
admin_pass: PLEASE_MODIFY_ME