温馨提示:本文翻译自stackoverflow.com,查看原文请点击:django - Ansible causes error 500 on save to database
ansible django python-3.x

django - Ansible在保存到数据库时导致错误500

发布于 2020-03-27 10:36:45

当使用Ansible Playbook设置服务器时,然后将帖子发送到数据库会导致错误500。如果我自己进行设置(从开始,在终端命令上进行),则可以正常工作。

这是我使用的代码,不是完整的,只是我在终端上所做的部分。系统是Ubuntu 18.04

- hosts: all

  vars:
    host: "{{ ansible_host }}"
    site_folder: "/home/{{ ansible_user }}/sites/{{ ansible_host }}"
    repo_url: "https://github.com/ForisTale/Django_TDD.git"

  tasks:
    - name: Deadsnakes PPA to get Python 3.7
      apt_repository:
        repo: ppa:deadsnakes/ppa

    - name: make sure required packages are installed
      apt:
        name: ["nginx", "git", "python3.7", "python3.7-venv"]
        state: present
        update_cache: yes

    - name: Create site folders.
      file:
        path: "{{ site_folder }}"
        state: directory

    - name: Check if .git exists.
      stat:
        path: "{{ site_folder }}/.git"
      register: git_exists

    - name: If .git exists fetch repository.
      command: git fetch
      args:
         chdir: "{{ site_folder }}"
      when: git_exists.stat.exists == True

    - name: If .git don't exists clone repository
      command: git clone {{ repo_url }} .
      args:
        chdir: "{{ site_folder }}"
      when: git_exists.stat.exists == False

    - name: Get local hash commit.
      local_action: command git log -n 1 --format=%H
      register: commmit_hash

    - name: Reset git to actualy used commit.
      command: git reset --hard {{ commmit_hash.stdout }}
      args:
        chdir: "{{ site_folder }}"

    - name: Check if virtualenv exists.
      stat:
        path: "{{ site_folder }}/virtualenv/bin/pip"
      register: venv_exists

    - name: If virtualenv don't exists set it up.
      command: python3.7 -m venv virtualenv
      args:
        chdir: "{{ site_folder }}"
      when: venv_exists.stat.exists == False

    - name: Update requirements.
      command: ./virtualenv/bin/pip install -r requirements.txt
      args:
        chdir: "{{ site_folder }}"

    - name: Check if .env exists
      stat:
        path: "{{ site_folder }}/.env"
      register: env_exists

    - name: Generate .env
      script: generate_env_file.py
      args:
        chdir: "{{ site_folder }}/deploy_tools/"
        executable: python3
      when: env_exists.stat.exists == False

    - name: Update static files.
      command: ./virtualenv/bin/python manage.py collectstatic --noinput
      args:
        chdir: "{{ site_folder }}"

    - name: Update database.
      command: ./virtualenv/bin/python manage.py migrate --noinput
      args:
        chdir: "{{ site_folder }}"

通过ansible-playbook安装后,一切看起来正常,文件位于应有的位置,仅在发送POST时出现此错误500。

查看更多

查看更多

提问者
Foris
被浏览
190
Foris 2019-07-03 21:38

我只是发现出了什么问题。将文件和文件夹的所有权设置为root,并防止对数据库进行任何更改(因为所有内容都设置为使用用户帐户)。最后添加

- name: Change ownership of all files and folders.
  command: "chown -R {{ ansible_user}} {{ host }}"
  args:
    chdir: "{{ site_folder }}/.."

解决问题。