Title: How could i switch between Ansible versions?
Tags: ansible;versions
Question: 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?

Comment: I highly suggest running with the latest unless there are bugs that are blocking you.


Here is another answer: 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
```


Here is another answer: 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 &gt; /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 v51.243.225.55-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&gt;/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.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:04/06/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<ansible><versions>', 'Title': 'How could i switch between Ansible versions?', 'ViewCount': 5569, 'id': 36453992, 'user_AboutMe': None, 'user_AccountId': 428306.0, 'user_DisplayName': 'WebComer', 'user_Reputation': 1073.0, 'user_Views': 673.0, 'user_id': 810546.0}}