Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sp3c73r2038/7462696 to your computer and use it in GitHub Desktop.
Save sp3c73r2038/7462696 to your computer and use it in GitHub Desktop.
ansible's installation just sucks. I want to use it while I'm hacking it, but not messing with PATH and PYTHONPATH !

Using Ansible with virtualenv (or pyenv if you prefer) without Install

assuming we are using $HOME/src as source code directory and $HOME/bin is in PATH so we can add script more easily.

get

get ansible source code first.

$ cd $HOME/src

$ git clone https://github.com/ansible/ansible

write script

we need to do some hacking to hacking/env-setup script coming with ansible source code.

$ cd $HOME/bin
$ cp $HOME/src/ansible/hacking/env-setup _asb # say we use this to dispatch command

open _asb with your favorite editor, rewrite some code

  • HACKING_DIR
HACKING_DIR="$HOME/src/ansible/hacking"
  • append these lines (say we've created a virtualenv there)
bin=$1
shift
$HOME/src/ansible/.virtualenv/bin/python $HOME/src/ansible/bin/$bin $@

# you could change above command to this, if you prefer pyenv
$HOME/.pyenv/versions/2.7.5/bin/python $HOME/src/ansible/bin/$bin $@
  • write the command we will use, $HOME/bin/ansible
#!/bin/bash

bin="${0##/*/}"
/bin/bash $HOME/bin/_asb $bin $@

so as the $HOME/ansible-playbook, but this time, we just need symlinking :D

$ ln -s $HOME/bin/ansible $HOME/bin/ansible-playbook

install the dependencies

ansible's using distribute by default

# in setup.py
# ...
from distribute.core import setup
# ...

This just sucks! python setup.py develop reports no such command! python setup.py install reports unknown option install_requires (How do they even install this python package!?!?).

change setup line in setup.py

# comment out this line
from distribute.core import setup

# use this, and off course you should have setuptools installed =D
from setuptools import setup

then run

.virtualenv/bin/python setup.py develop

You are now good to go

try

$ ansible
@erning
Copy link

erning commented Nov 14, 2013

嗯,这个不错。可以不需要copy env-setup,新建一个_asb文件只要这几行就好了

#!/bin/bash
source $HOME/src/ansible/hacking/env-setup -q
bin="${0##/*/}"
PYENV_VERSION=2.7.5 exec $ANSIBLE_HOME/bin/$bin $@

@erning
Copy link

erning commented Nov 14, 2013

还可以用alias来做

alias ansible="source $HOME/src/ansible/hacking/env-setup -q && PYENV_VERSION=2.7.5 $ANSIBLE_HOME/bin/ansible"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment