Animesh Pandey
Animesh Pandey

Reputation: 6018

Installing a python package from source using ansible

I have the following ansible playbook:

- 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

Here I pull a repository from github, then I copy a config.json to the folder I cloned the git repository. After that I need to run python setup.py install to install the package and then python setup.py install_service in the same directory.

I put both the installation commands in a shell file install_mc.sh and the copied the file to the same directory where I cloned the repository.

The git repository is cloned in /srv/checkout/mongo-connector/.

Following is the directory layout:

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

But then I run the ansible script using vagrant I get the followin error during the execution of 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 

Content of install_mc.sh is:

#!/usr/bin/env bash

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

How should I correct this issue?

Upvotes: 4

Views: 3244

Answers (2)

Lincoln
Lincoln

Reputation: 181

It happens because your script result return error

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

when your execute install_mc.sh, install_mc.sh cannot find setup.py , because you don't use absolute path.

Upvotes: 1

billkw
billkw

Reputation: 3699

I think the problem is that you're assuming that the install_mc script is being executed from the directory that you copied it to, but the script module actually reads from your local machine, and executes the script in the home directory of the remote node.

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.

As DeHaan later suggests, you'll probably have better luck with just running your two setup.py commands with the command module. That one allows you to specify a directory (with the chdir option).

The chmod is probably unnecessary because 1) you're calling the script with the python binary, and 2) the mongo maintainers likely established the appropriate permissions in their git repository.

Upvotes: 2

Related Questions