You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

So we're using Ansible for a lot of our deployments. This agent-less tool requires almost no overhead, and this low barrier of entrance enables people from our community to contribute deployment code.

The Ansible code has seen a few major changes over the last years, which unfortunately causes some features to change, disappear, or even break.

It is impractical to refactor large projects, so there is a need to able to use different Ansible versions.

On my MacOS computer I usually use the HomeBrew package manager, which works OK for Ansible. Switching between different Ansible versions is possible, but in practise this is only possible if you have cached the old version, i.e. you have been installing and updating versions continuously. Picking a random older version is very tedious at best.

Enter Python's virtualenv.

This allows you to isolate a complete python environment, and install a specific version of Ansible. With help of some bash aliases this can be conveniently automated.

Assuming you have the required tools available, install this in your home directory:

mkdir ~/.virtualenvs
virtualenv ~/.virtualenvs/ansible-2.3.3
source ~/.virtualenvs/ansible-2.3.3/bin/activate
pip install ansible==2.3.3
deactivate

Repeat this for every version you want to be able to use.

Then add a line like this to your .bash_profile for each version:

alias ansible-activate-2.3.3="source ~/.virtualenv/ansible-2.3.3/bin/activate"


You you can just type ansible-activate and use tab completion to pick the specific version.


Obviously you can use ansible to install ansible - for instance onto a deployment VM or bastion host:

--- 
- name: Different Ansible versions
  become: true
  hosts: bastions
    
  vars:
    ansible_versions:
      - "2.3.3"
      - "2.4.4"
      - "2.5.2"
    virtualenv_basepath: /opt/virtualenvs

  tasks:
    - name: Ensure basic packages are installed
      package:
        name: "{{ item }}"
        state: present
      with_items:
        - python-pip
        - python-virtualenv
        - git
    
    - name: Ensure ansible versions are available in virtualenv
      pip:
        name: ansible
        virtualenv: "{{ virtualenv_basepath }}/ansible-{{ item }}"
        version: "{{ item }}"
      with_items: "{{ ansible_versions }}"
    
    - name: Ensure system wide activation aliases are available
      copy:
        dest: /etc/profile.d/ansible_virtualenvs.sh
        content: "{% for i in ansible_versions %}alias ansible-activate-{{ i }}='source {{ virtualenv_basepath }}/ansible-{{ i }}/bin/activate'\n{% endfor %}"        



  • No labels