Last active
June 13, 2023 13:24
-
-
Save elleryq/9c70e08b1b2cecc636d6 to your computer and use it in GitHub Desktop.
Create Django super user in ansible
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
django_dir: /webapp/djangoapp | |
settings: djangoapp.settings | |
admin_user: admin | |
admin_email: [email protected] | |
admin_pass: PLEASE_MODIFY_ME |
I ended up with this:
# check if superuser user exists, if not, create user.
- name: Check if superuser user exists, if not, create user.
django_manage:
command: shell -c "from django.contrib.auth import get_user_model; MyUser = get_user_model(); MyUser.objects.filter( username__exact = '{{ django_superuser_username }}' ).count() == 0 or exit(); new_super_user = MyUser( username = '{{ django_superuser_username }}', password='{{ django_superuser_password }}', email='{{ django_superuser_email }}', is_superuser = True, is_staff = True ); new_super_user.save();"
app_path: "{{ django_project_folder_path }}"
virtualenv: "{{ django_virtualenv_path }}"
And it worked, where the following did not (it never returned False that the user did not exist, so never created the user).
# check if superuser user exists.
- name: super user {{ django_superuser_username }} exists?
command: echo "from django.contrib.auth import get_user_model; User = get_user_model(); print(User.objects.filter(username='{{ django_superuser_username }}').count()>0)" | {{ django_virtualenv_bin_path }}/python ./manage.py shell -i python
args:
chdir: "{{ django_project_folder_path }}"
environment:
DJANGO_SETTINGS_MODULE: "{{ django_settings_import }}"
register: superuser_existed
# if no super user, create.
- name: If no super user {{ django_superuser_username }}, create super user
django_manage:
command: "createsuperuser --noinput --username={{ django_superuser_username }} --email={{ admin_email }}"
app_path: "{{ django_project_folder_path }}"
virtualenv: "{{ django_virtualenv_path }}"
settings: "{{ django_settings_import }}"
when: not superuser_existed
I left in the change of password, so it would update even if user already existed.
I probably did something wrong, but I prefer the shell command approach, anyway, since it keeps all the logic together, doesn't depend on the output of "manage.py shell", which I think could change depending on ipython, bpython, or python.
Slight modification to @jonathanmorgan's approach that worked for me:
django_manage:
command: shell -c "from django.contrib.auth.hashers import make_password; from django.contrib.auth import get_user_model; MyUser = get_user_model(); MyUser.objects.filter( username__exact = '{{ admin_user }}' ).count() == 0 or exit(); new_super_user = MyUser( username = '{{ admin_user }}', email='{{ admin_email }}', is_superuser = True, is_staff = True ); new_super_user.password = make_password('{{ admin_pass }}'); new_super_user.save();"
app_path: "{{ project_path }}"
virtualenv: "{{ virtualenv_path }}"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I solved this issue by creating the user from django shell like this:
Is there any disadvantage to this method vs using
createsuperuser
command ?