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

Compare with Current View Page History

« Previous Version 7 Next »

We're using Ansible for a lot of our deployments. This agent-less configuration management tool requires almost no overhead, and this low barrier to entry enables people from our community to contribute deployment code. So far so good.

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

It is impractical to refactor large projects to work with the latest Ansible version, so there is a need to use to specific Ansible versions for specific deployments.

On MacOS I use the HomeBrew package manager to provide me with Ansible, and this OK. Switching between different Ansible versions is possible, but in practise this is only works 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 the help of some bash aliases you can conveniently switch between all versions.

Assuming you have the required basic 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 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