2016-03-04 54 views
4

我有以下ansible劇本:使用從源代碼安裝Python包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/

以下是目錄佈局:

[email protected]:/srv/checkout/mongo-connector$ pwd 
/srv/checkout/mongo-connector 

[email protected]:/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 

但後來我的install_mc.sh執行過程中運行使用無業遊民,我得到的跟隨着錯誤ansible腳本:

==> 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

我認爲問題在於,您認爲install_mc腳本正在從您複製到的目錄執行,但script模塊實際上是從本地計算機讀取的,並在主目錄中執行腳本遠程節點。

腳本模塊採用腳本名稱,後跟空格分隔參數列表。路徑上的本地腳本將被傳輸到遠程節點,然後執行。給定的腳本將通過遠程節點上的shell環境進行處理。該模塊不需要遠程系統上的python,就像原始模塊一樣。

正如DeHaan後來的建議,只需運行兩個setup.py命令,使用command模塊就可以獲得更好的運行效果。那個允許你指定一個目錄(使用chdir選項)。

chmod可能是不必要的,因爲1)您使用python二進制文件調用腳本,以及2)mongo維護者可能在其git存儲庫中建立適當的權限。

1

這是因爲你的腳本的結果返回錯誤

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

當你執行install_mc.shinstall_mc.sh找不到setup.py ,因爲你不使用絕對路徑。