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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Slight modification to @jonathanmorgan's approach that worked for me: