gpt4 book ai didi

django - 如何在 Ansible 中以幂等方式创建 Django super 用户?

转载 作者:行者123 更新时间:2023-12-02 20:41:51 26 4
gpt4 key购买 nike

我正在使用 Ansible 部署我的 Django 应用程序。

我的 Ansible 剧本中有以下步骤用于创建 super 用户:

  - name: django create superuser
django_manage:
virtualenv: /.../app
app_path: /.../app
command: "createsuperuser --noinput --username=admin --email=admin@{{ inventory_hostname }}"

但是当我第二次运行我的剧本时,它因数据库约束错误而失败,因为具有给定用户名的 super 用户已经存在。我希望 Ansible 仅创建用户一次。

如何使此步骤幂等?

最佳答案

这未经测试,但应该可以工作:

- name: Check if django superuser exists
django_manage:
virtualenv: /.../app
app_path: /.../app
command: shell -c 'import sys; from django.contrib.auth.models import User; sys.exit(0 if User.objects.filter(username="admiin").count() > 0 else 1)'
register: checksuperuser
check_mode: True
ignore_errors: True
changed_when: False

- name: django create superuser
django_manage:
virtualenv: /.../app
app_path: /.../app
command: "createsuperuser --noinput --username=admin --email=admin@{{ inventory_hostname }}"
when: checksuperuser.rc != 0

关于django - 如何在 Ansible 中以幂等方式创建 Django super 用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45915174/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com