0

我正在使用 Ansible 和 Docker 部署 ROR 应用程序,创建 Ruby Docker 映像并推送到 Docker HUB,Ansible 将应用程序从私有仓库克隆到目标服务器,成功创建了 docker 容器,但无法运行 Ruby 命令,例如捆绑安装、数据库迁移、Rake 任务。这些命令需要在从 Ansible Code 以交互模式部署时运行。

以及如何在使用 unicorn 运行上述任务后启动应用程序。

这是为了使用 Ansible 自动化部署过程,以前使用 Capistrano,但现在我们正在从 Capistrano 迁移到 Ansible。

---
- hosts: Ec2-Instance
  become: true
  tasks:
   - name: Creating Deploy path
     file:
       state: directory
       path: "/var/www/"
  - name: Copying SSH Key | Temp
    copy:
      src: ~/.ssh/id_rsa
      dest: ~/.ssh/id_rsa
      owner: deployer
      group: deployer
      mode: 400
  - name: Cloning a web application to the application path
    git:
      repo: private repo
      version: master
      dest: /var/www/app-path/ 
      accept_hostkey: yes
    register: git
     - name: Remove SSH Key
       shell: "sudo rm -rf ~/.ssh/id_rsa"

  - name: Run Ruby Docker Container
    docker_container:
      name: 'container_name'
      image: 'docker-hub/ruby-2.4.1:0.1'
      tty: yes
      detach: true
      restart: yes # not required. Use with started state to force a matching container to be stopped and restarted.
      interactive: yes # not required. Keep stdin open after a container is launched, even if not attached.
      state: started
      volumes: /var/www/app-path:/var/www/app-path/
      working_dir: /var/www/app-path/
      env:
        gem_path: /var/www/app-path
        gemfile: /var/www/app-path
      bundler:
        deployment_mode: production

我希望 Ansible Playbook 使用 Docker / Nginx / Unicorn 部署 Ruby on Rails 应用程序

4

1 回答 1

0

您的剧本失败了,因为您指定了以下参数,该参数不是 docker_container 模块(docker_container 模块

...
bundler:
      deployment_mode: production

这里的另一种方法可能是这样的

在 /var/www/app-path/install-ror.sh 中创建一个 shell 脚本

#!/bin/sh

working_dir=/var/www/app-path/
cd $working_dir

export gem_path=/var/www/app-path
export gemfile=/var/www/app-path

#see https://stackoverflow.com/questions/10912614/rails-bundle-install-production-only 
gem install bundle-only

#install only production modules
bundle-only production

如下更新您的剧本

- name: Run Ruby Docker Container
    docker_container:
        name: 'container_name'
        image: 'docker-hub/ruby-2.4.1:0.1'
        tty: yes
        detach: true
        restart: yes # not required. Use with started state to force a matching container to be stopped and restarted.
        interactive: yes # not required. Keep stdin open after a container is launched, even if not attached.
        state: started
        volumes: /var/www/app-path:/var/www/app-path/
        working_dir: /var/www/app-path/
        command: /var/www/app-path/install-ror.sh

您可以按照输出来改进这一点。

于 2019-05-05T05:54:52.110 回答