使用ansible从源代码安装python包

时间:2016-03-04 04:33:59

标签: python shell ansible ansible-playbook

我有以下ansible剧本:

- hosts: all
  gather_facts: false
  sudo: true
  tasks:
  - name: Pull sources from the repository.
    git: repo=https://github.com/mongodb-labs/mongo-connector.git dest=/srv/checkout/mongo-connector

- hosts: all
  sudo: true
  tasks:
  - name: copy local config.json to remote if exists
    local_action: stat path="./config.json"
    register: file
    ignore_errors: True
  - name: copy file if it exists
    copy: src=./config.json dest=/srv/checkout/mongo-connector/config.json force=yes
    when: file.stat.exists

- hosts: all
  sudo: true
  tasks:
  - name: copy local install_mc.sh to remote if exists
    local_action: stat path="./install_mc.sh"
    register: file
    ignore_errors: True
  - name: copy installation scripts
    copy: src=./install_mc.sh dest=/srv/checkout/mongo-connector/install_mc.sh mode=755
    when: file.stat.exists
  - name: Execute script
    script: /srv/checkout/mongo-connector/install_mc.sh

这里我从github中提取存储库,然后将config.json复制到我克隆git存储库的文件夹中。之后,我需要运行python setup.py install来安装软件包,然后运行python setup.py install_service在同一目录中。

我将两个安装命令都放在一个shell文件install_mc.sh中,并将该文件复制到我克隆存储库的同一目录中。

git存储库克隆在/srv/checkout/mongo-connector/中。

以下是目录布局:

vagrant@vagrant-ubuntu-trusty-64:/srv/checkout/mongo-connector$ pwd
/srv/checkout/mongo-connector

vagrant@vagrant-ubuntu-trusty-64:/srv/checkout/mongo-connector$ ls
CHANGELOG.rst  config.json  ez_setup.py  install_mc.sh  LICENSE  mongo_connector  README.rst  scripts  setup.cfg  setup.py  tests

然后我使用vagrant运行ansible脚本我在执行install_mc.sh期间遇到了followin错误:

==> connector: TASK [Execute script] **********************************************************
==> connector: task path: /vagrant/provisioning/mc_playbook.yml:36
==> connector: fatal: [127.0.0.1]: FAILED! => {"changed": true, "failed": true, "rc": 2, "stderr": "chmod: cannot access ‘./setup.py’: No such file or directory\npython: can't open file './setup.py': [Errno 2] No such file or directory\npython: can't open file './setup.py': [Errno 2] No such file or directory\n", "stdout": "", "stdout_lines": []}
==> connector: 
==> connector: NO MORE HOSTS LEFT *************************************************************
==> connector:  to retry, use: --limit @mc_playbook.retry
==> connector: 
==> connector: PLAY RECAP *********************************************************************
==> connector: 127.0.0.1                  : ok=10   changed=4    unreachable=0    failed=1 

install_mc.sh的内容是:

#!/usr/bin/env bash

chmod +x ./setup.py
python ./setup.py install
python ./setup.py install_service

我该如何纠正这个问题?

2 个答案:

答案 0 :(得分:2)

我认为问题在于您假设install_mc脚本是从您复制到的目录执行的,但script模块实际上是从本地计算机读取的,并执行远程节点主目录中的脚本。

  

脚本模块获取脚本名称,后跟空格分隔的参数列表。路径中的本地脚本将传输到远程节点,然后执行。将通过远程节点上的shell环境处理给定脚本。这个模块在远程系统上不需要python,就像原始模块一样。

正如DeHaan后来建议的那样,使用command模块运行两个setup.py命令可能会更好。那个允许你指定一个目录(chdir选项)。

chmod可能是不必要的,因为1)你使用python二进制文件调用脚本,2)mongo维护者可能在他们的git存储库中建立了适当的权限。

答案 1 :(得分:1)

这是因为您的脚本结果返回错误

script: /srv/checkout/mongo-connector/install_mc.sh

执行 install_mc.sh 时, install_mc.sh 找不到setup.py ,因为你没有使用绝对路径。