Thursday, May 3, 2018

Setting up Python Environments for Ansible

There is always a need for a different version of Ansible that what is installed on ur running OS. Initially to avoid this mind loops, i have run Ansible from docker containers. I basically just build a docker image with my preferred version of Ansible and other packages i might need and run my Ansible playbooks from within the docker container. 

While Docker is great tool, there are some times that managing the various docker images and container can become a pain. One other method that can be used is creating virtual environments for specific versions of Ansible.

The steps would typically involve


  • installing pip
  • install python virtualenv module
  • install you preferred sensible version 
  • create Python virtual environment.
All of these steps have been combined in the script below


#!/bin/bash

ANSIBLE_VERSIONS=( "2.2.0.0" "2.2.1.0" "2.2.2.0" "2.2.3.0" "2.3.0.0" "2.3.1.0" "2.4.0.0")

VIRTUALENV_PATH="/home/$USER"

if [[ $(uname) == "Linux" ]]; then
  # Ubuntu
  if [ -f /etc/debian_version ]; then
    codename="$(lsb_release -c | awk '{print $2}')"
    sudo apt-get update
    sudo apt-get -y install build-essential libffi-dev libssl-dev python-dev \
    python-minimal python-pip python-setuptools python-virtualenv
  fi

  # AMAZON LINUX
  if [ -f /etc/system-release ]; then
    codename="$(cat /etc/system-release | awk '{print $1}')"
    sudo yum -y install gmp-devel libffi-devel openssl-devel python-crypto \
    python-devel python-pip python-setuptools python-virtualenv \
    sudo yum -y group install "Development Tools"
  fi
fi

  # RHEL
  if [ -f /etc/redhat-release ]; then
    codename="$(cat /etc/redhat-release | awk '{print $1}')"
    if [[ $codename == "Fedora" ]]; then
      sudo dnf -y install gmp-devel libffi-devel openssl-devel python-crypto \
      python-devel python-dnf python-pip python-setuptools python-virtualenv \
      redhat-rpm-config && \
      sudo dnf -y group install "C Development Tools and Libraries"
      elif [[ $codename == "CentOS" ]]; then
      sudo yum -y install gmp-devel libffi-devel openssl-devel python-crypto \
      python-devel python-pip python-setuptools python-virtualenv \
      redhat-rpm-config && \
      sudo yum -y group install "Development Tools"
    fi
  fi


if  ! python -c "import virtualenv" &> /dev/null;
then
    sudo pip install virtualenv;
    # Allow users to use virtualenv in their homedirs
    sudo chmod -R o+rX /usr/local/lib/python2.7/dist-packages/virtualenv*
fi


# Setup Ansible Virtual Environments
for ANSVER in "${ANSIBLE_VERSIONS[@]}"
do
  if [ ! -d "$VIRTUALENV_PATH/ansible-$ANSVER" ]; then
    virtualenv $VIRTUALENV_PATH/ansible-$ANSVER
    source $VIRTUALENV_PATH/ansible-$ANSVER/bin/activate
    pip install ansible==$ANSVER ansible-lint
    deactivate
  fi
done

printf "\nRun this to activate virtualenv:\n\n\tsource ~/ansible-(version)/bin/activate\n\n"

I hope this helps

No comments:

Post a Comment