如何从ansible脚本调用capistrano

时间:2016-07-06 16:08:11

标签: ruby capistrano ansible

我的问题有点棘手。我有一个任务是为Ruby中的系统构建制作部署脚本(不是由我开发的)。该项目已经有一个capistrano部署,只要机器具有先前的设置,该工作就可以正常工作。我的任务是在不触及capistrano部分的情况下自动执行此设置。由于我对capistrano一无所知并且非常有时间去做,所以我决定使用ansible。

这个想法是运行ansible脚本,这会设置机器并调用capistrano来部署项目。我尝试了以下任务,但都产生了一些错误:

- name: Run cap
  shell: "cap generic_production deploy"
  environment:
    MACHINE: localhost
  args:
    chdir: /home/{{ansible_user_id}}/project
fatal: [test]: FAILED! => {"changed": true, "cmd": "cap generic_production deploy", "delta": "0:00:00.106225", "end": "2016-07-06 15:54:42.482794", "failed": true, "rc": 1, "start": "2016-07-06 15:54:42.376569", "stderr": "/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- capistrano/setup (LoadError)
  from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
  from /usr/lib/ruby/vendor_ruby/capistrano/configuration/loading.rb:152:in `require'
  from Capfile:2:in `load'\
  from /usr/lib/ruby/vendor_ruby/capistrano/configuration/loading.rb:93:in `instance_eval'
  from /usr/lib/ruby/vendor_ruby/capistrano/configuration/loading.rb:93:in `load'
  from /usr/lib/ruby/vendor_ruby/capistrano/configuration/loading.rb:172:in `load_from_file'
  from /usr/lib/ruby/vendor_ruby/capistrano/configuration/loading.rb:89:in `load'
  from /usr/lib/ruby/vendor_ruby/capistrano/configuration/loading.rb:86:in `block in load'
  from /usr/lib/ruby/vendor_ruby/capistrano/configuration/loading.rb:86:in `each'
  from /usr/lib/ruby/vendor_ruby/capistrano/configuration/loading.rb:86:in `load'
  from /usr/lib/ruby/vendor_ruby/capistrano/cli/execute.rb:65:in `block in load_recipes'
  from /usr/lib/ruby/vendor_ruby/capistrano/cli/execute.rb:65:in `each'
  from /usr/lib/ruby/vendor_ruby/capistrano/cli/execute.rb:65:in `load_recipes'
  from /usr/lib/ruby/vendor_ruby/capistrano/cli/execute.rb:31:in `execute!'
  from /usr/lib/ruby/vendor_ruby/capistrano/cli/execute.rb:14:in `execute'
  from /usr/bin/cap:4:in `<main>'", "stdout": "", "stdout_lines": [], "warnings": []}
- name: Run cap
  shell: "bundle exec cap generic_production deploy"
  environment:
    MACHINE: localhost
  args:
    chdir: /home/{{ansible_user_id}}/project
fatal: [test]: FAILED! => {"changed": true, "cmd": "bundle exec cap generic_production deploy", "delta": "0:00:00.001287", "end": "2016-07-06 15:50:21.472625", "failed": true, "rc": 127, "start": "2016-07-06 15:50:21.471338", "stderr": "/bin/sh: 1: bundle: not found", "stdout": "", "stdout_lines": [], "warnings": []}
- name: Run cap
  shell: "sudo -iu {{ansible_user_id}} bundle exec cap generic_production deploy"
  environment:
    MACHINE: localhost
  args:
    chdir: /home/{{ansible_user_id}}/project
fatal: [test]: FAILED! => {"changed": true, "cmd": "sudo -iu deploy bundle exec cap generic_production deploy", "delta": "0:00:00.230098", "end": "2016-07-06 15:28:42.623268", "failed": true, "rc": 10, "start": "2016-07-06 15:28:42.393170", "stderr": "", "stdout": "Could not locate Gemfile or .bundle/ directory", "stdout_lines": ["Could not locate Gemfile or .bundle/ directory"], "warnings": ["Consider using 'become', 'become_method', and 'become_user' rather than running sudo"]}

有没有办法从ansible调用capistrano或者它无法完成?

编辑: generic_production是一个使用 MACHINE 作为目标的部署脚本。

2 个答案:

答案 0 :(得分:0)

如果ansible playbook是从你正常运行cap的目录中运行的话,这对我有用。

- name: capistrano deploy
  local_action: shell cap generic_production deploy
  become: false

答案 1 :(得分:0)

如果您尝试在远程计算机上运行上限部署,则local_action不正确。这是一个适用于rbenv环境的小任务(可能是rvm,但我没有测试过):

- name: Run Capistrano
  shell: bash -lc "bundle exec cap production deploy"
     chdir=/home/ubuntu/rails/myapp

-lc技巧是关键,因为它将此作为登录shell运行,强制执行rbenv设置。