Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

django_manage createsuperuser does not seem idempotent #29786

Closed
ansibot opened this issue Sep 12, 2017 · 4 comments
Closed

django_manage createsuperuser does not seem idempotent #29786

ansibot opened this issue Sep 12, 2017 · 4 comments
Labels
affects_2.0 This issue/PR affects Ansible v2.0 bot_closed bug This issue/PR relates to a bug. collection:community.general collection Related to Ansible Collections work has_pr This issue has an associated PR. module This issue/PR relates to a module. needs_collection_redirect https://github.com/ansible/ansibullbot/blob/master/docs/collection_migration.md support:community This issue/PR relates to code supported by the Ansible community. traceback This issue/PR includes a traceback. web_infrastructure Web-infrastructure category

Comments

@ansibot
Copy link
Contributor

ansibot commented Sep 12, 2017

From @westfood on 2016-06-05T18:53:40Z

ISSUE TYPE
  • Bug Report
COMPONENT NAME

django_manage

ANSIBLE VERSION
ansible 2.0.0.2
OS / ENVIRONMENT

OSX El Captain / 10.11.5, managing CentOS 7.1

SUMMARY

Issuing command createsuperuser ends up in error after second invocation.

STEPS TO REPRODUCE

Using this task, hard coding username/mail does not yeald in better effect

- name: Django add superusers
  django_manage:
    app_path: "{{ seeder_home }}/Seeder"
    virtualenv: "{{ seeder_virtualenv }}"
    command: "createsuperuser --noinput --username={{ item.name }} --email={{ item.mail }}" # Not idempotent, probably bug in django_manage module
  with_items: "{{ seeder_admins }}"
  tags: django
  ignore_errors: yes
EXPECTED RESULTS

It should not try to create user again

ACTUAL RESULTS
stderr: Traceback (most recent call last):\n  File \"/opt/virtualenv/seeder/lib/python3.4/site-packages/django/db/backends/utils.py\", line 64, in execute\n    return self.cursor.execute(sql, params)\npsycopg2.IntegrityError: duplicate key value violates unique constraint \"auth_user_username_key\"\nDETAIL:       
Key (username)=(rudolf) already exists.

Copied from original issue: ansible/ansible-modules-core#3871

@ansibot ansibot added the affects_2.0 This issue/PR affects Ansible v2.0 label Sep 12, 2017
@ansibot
Copy link
Contributor Author

ansibot commented Sep 12, 2017

From @ansibot on 2016-06-05T18:53:40Z

@tastychutney, @scottanderson42, ping. This issue is waiting on your response.
click here for bot help

@ansibot
Copy link
Contributor Author

ansibot commented Sep 12, 2017

From @ambivalentno on 2016-06-05T18:53:40Z

as I've found this ticket while googling (and I'm working on this), here's a sample on how to create superuser in sort-of idempotent way:

- name: check if django superuser exists
  shell: 'echo "from django.contrib.auth.models import User; User.objects.filter(is_superuser=True).exists()" | {{project_dir}}/venv/bin/python manage.py shell'
  args:
    chdir: "{{ project_dir }}"
  register: superuser_exists


- name: create superuser if it doesnt exist
  when: superuser_exists.stdout.find("True") == -1
  django_manage:
    app_path: '{{django_app_path}}'
    settings: '{{django_settings|default(omit,true)}}'
    virtualenv: '{{django_virtualenv|default(omit,true)}}'
    command: "createsuperuser --noinput --username={{ bookiebet_admin_django_superuser_login }} --email={{ bookiebet_admin_django_superuser_email }}"

I have to note not all people will use default django's user model (django.contrib.auth.models.User). Therefore, sothing like settings.AUTH_USER_MODEL should be used.
Also, it's still not idempotent enough, as update of username, email and password don't process here.

UPDATE:
found a gist which does everything I've noted:
https://gist.github.com/elleryq/9c70e08b1b2cecc636d6

@ansibot ansibot added bug_report module This issue/PR relates to a module. python3 support:community This issue/PR relates to code supported by the Ansible community. labels Sep 12, 2017
@abadger abadger removed the python3 label Sep 21, 2017
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 1, 2018
dalepotter added a commit to IATI/deployment-archive that referenced this issue Mar 10, 2018
django_manage does not check if a superuser has already been created, and attempts to create a superuser on each run: ansible/ansible#29786

This results in an IntegrityError database exception. The workaround is to manually create a superuser after Ansible has run.
@ansibot ansibot added the traceback This issue/PR includes a traceback. label May 28, 2018
@ansibot ansibot added the web_infrastructure Web-infrastructure category label Feb 17, 2019
@ansibot ansibot added the has_pr This issue has an associated PR. label Jul 28, 2019
@next-jesusmanuelnavarro
Copy link

It is to be noted that as per ansible version 2.9.4, documentation still shows the failing createuser example.

It probably makes sense to update the doc entry.

@ansibot ansibot added collection Related to Ansible Collections work collection:community.general needs_collection_redirect https://github.com/ansible/ansibullbot/blob/master/docs/collection_migration.md labels Apr 29, 2020
@ansibot ansibot added the needs_triage Needs a first human triage before being processed. label May 16, 2020
@ansibot
Copy link
Contributor Author

ansibot commented Aug 16, 2020

Thank you very much for your interest in Ansible. Ansible has migrated much of the content into separate repositories to allow for more rapid, independent development. We are closing this issue/PR because this content has been moved to one or more collection repositories.

For further information, please see:
https://github.com/ansible/ansibullbot/blob/master/docs/collection_migration.md

@ansibot ansibot closed this as completed Aug 16, 2020
@sivel sivel removed the needs_triage Needs a first human triage before being processed. label Aug 17, 2020
@ansible ansible locked and limited conversation to collaborators Sep 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.0 This issue/PR affects Ansible v2.0 bot_closed bug This issue/PR relates to a bug. collection:community.general collection Related to Ansible Collections work has_pr This issue has an associated PR. module This issue/PR relates to a module. needs_collection_redirect https://github.com/ansible/ansibullbot/blob/master/docs/collection_migration.md support:community This issue/PR relates to code supported by the Ansible community. traceback This issue/PR includes a traceback. web_infrastructure Web-infrastructure category
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants