How could i switch between Ansible versions?
Asked Answered
W

2

7

Could i install several Ansible versions at a single OS and switch them at will? For now we have several releases, say 1.5.4 for Ubuntu, but the latest is 2.0.1, and 1.9.4 is still around. I would appreciate install all of them and just switch to one that is suitable for me. If yes, how?

Weeds answered 6/4, 2016 at 14:22 Comment(1)
I highly suggest running with the latest unless there are bugs that are blocking you.Armington
K
13

Ansible is just a python package, so, if you have virtualenv installed on your host it is just a matter of creating a new venv for each ansible version you want, and then pip install it.

So if for example you want ansible v1.9.5 you could do:

$ virtualenv ~/venvs/ansible_1_9_5
$ source ~/venvs/ansible_1_9_5/bin/activate
$ pip install "ansible==1.9.5" 
$ ansible --version
      ansible 1.9.5
      configured module search path = None
Klink answered 6/4, 2016 at 14:41 Comment(0)
K
0

A virtualenv per version works nicely if you're okay with only using versioned packages of Ansible. To do Ansible development or you just wanna follow the upstream source code for bug fixes (and new bugs...), you can use the following in your .zshrc (bash will work as well of course):

function ansible-switch {
  if [ "$1" != "off" ]; then
    VIRTUAL_ENV_DISABLE_PROMPT=1 source $ANSIBLE_VIRTUALENV/bin/activate
    git -C "$ANSIBLE_SOURCE_DIR" checkout -q $1
    source $ANSIBLE_SOURCE_DIR/hacking/env-setup -q
    echo "Environment configured to run Ansible from source (branch: $1)"
  else
    if [[ -v ANSIBLE_HOME ]]; then
      export PYTHONPATH=$(echo $PYTHONPATH | sed "s@$ANSIBLE_HOME/lib:@@")
      export PATH=$(echo $PATH | sed "s@$ANSIBLE_HOME/bin:@@")
      export MANPATH=$(echo $MANPATH | sed "s@$ANSIBLE_HOME/docs/man:@@")
      unset ANSIBLE_HOME
      deactivate
    fi
    echo "Environment configured to not run Ansible from source"
  fi
}

if ! [[ -v ANSIBLE_HOME ]]; then
  ansible-switch devel > /dev/null
fi

You will need to define the ANSIBLE_SOURCE_DIR and ANSIBLE_VIRTUALENV variables. ANSIBLE_SOURCE_DIR is the git clone of the Ansible source code and ANSIBLE_VIRTUALENV is the virtualenv you set up with Python2 and any required Ansible dependencies (check http://docs.ansible.com/ansible/intro_installation.html#running-from-source for more info about running from source).

You can then switch to any Ansible git branch like this:

ansible-switch devel

Or a tag:

ansible-switch v2.3.0.0-1

You can turn off running from source like this:

ansible-switch off

As a kicker, I use the following script (called 'ansible-update') to update my own Ansible fork with Ansible upstream commits:

cd "$ANSIBLE_SOURCE_DIR"
current_branch_tag=$(git symbolic-ref --short HEAD 2>/dev/null)
if [ $? -ne 0 ]; then
  current_branch=$(git describe --tags)
fi
git checkout devel -q
git fetch upstream -q
git rebase upstream/devel -q
git checkout $current_branch -q

This last bit assumes you have a fork and have set upstream as the official Ansible remote.

Kierkegaard answered 19/5, 2017 at 17:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.