使用ansib从源代码安装python包

2024-06-02 04:51:54 发布

您现在位置: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/。在

目录布局如下:

^{pr2}$

但是,我使用vagrant运行ansible脚本,在执行install_mc.sh时出现以下错误:

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

我应该如何更正这个问题?在


Tags: installnamepyconfigjsontrueconnectormongo
2条回答

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

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

当执行安装时_mc.sh公司安装_mc.sh公司找不到设置.py ,因为您不使用绝对路径。在

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

The script module takes the script name followed by a list of space-delimited arguments. The local script at path will be transferred to the remote node and then executed. The given script will be processed through the shell environment on the remote node. This module does not require python on the remote system, much like the raw module.

正如DeHaan后来所说,你可能会有更好的运气只运行你的两个设置.py带有^{}模块的命令。它允许您指定一个目录(使用chdir选项)。在

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

相关问题 更多 >