Ansible导致在保存到数据库时出现错误500

时间:2019-07-02 14:19:30

标签: django python-3.x ansible

使用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。

1 个答案:

答案 0 :(得分:-1)

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

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

解决问题。