Skip to content

Instantly share code, notes, and snippets.

@bryan-kinney
Last active December 20, 2018 05:19
Show Gist options
  • Save bryan-kinney/7f6ab8efc95eaffd26a4e3de12f57abf to your computer and use it in GitHub Desktop.
Save bryan-kinney/7f6ab8efc95eaffd26a4e3de12f57abf to your computer and use it in GitHub Desktop.
Ansible 2.7 on rhel 7.5 in python 3.6 venv

Ansible 2.7 and Python 3.6 on a Red Hat linux 7.5

How to install ansible inside a python virtual environment on Red Hat Linux 7.5.
This allows you to control the python ansible versions. All while protecting the kernal's python version.
The python-ovirt-engine-sdk4 is an optional library for oVirt API connections (Example: Red Hat Virtualization)
Environment:
  • Red Hat Linux 7.5 (Virtual machine)
  • Python 3.6.3 used
  • Anisble 2.7
  • oVirt API libraries
Install
  • Prepare the VM with RHEL 7.5
  • Change hostname and subscribe the VM.
hostnamectl set-hostname ansible.dev
subscription-manager register --autosubscribe
<access.redhat.com credentials>
  • Enable dev and software repos
subscription-manager repos --enable rhel-7-server-optional-rpms  --enable rhel-server-rhscl-7-rpms --enable rhel-7-server-rh-common-rpms
yum -y install @development
  • Install development versions of curl and openssl
yum -y install vim-enhanced libcurl-devel openssl-devel
  • Install new version of Python
yum -y install rh-python36
  • Install common python libraries for that version
yum -y install rh-python36-numpy rh-python36-scipy rh-python36-python-tools rh-python36-python-six
export PYCURL_SSL_LIBRARY=openssl
export LDFLAGS=-L/usr/local/opt/openssl/lib
export CPPFLAGS=-I/usr/local/opt/openssl/include
scl enable rh-python36   (Only once persession)
python3 -m pip install pycurl --compile --no-cache-dir
yum -y install python-ovirt-engine-sdk4
  • Add a normal user for daily use
useradd awx
usermod -aG wheel awx
passwd awx
  • You should add those environment variables above inside this user's ~/.bashrc file.
  • Installing pipenv for current user.
scl enable rh-python36 bash
python3.6 -m pip install --user pipenv
mkdir myproject; cd myproject
pipenv --python 3.6
pipenv shell
  • Install ansible in a venv
  • Pick up the version of python to use before entering the shell.
  • If you haven't done this yet. (Only once persession)
scl enable rh-python36 bash
  • Now get the path and save for later
which python
/opt/rh/rh-python36/root/usr/bin/python
  • Move to the project directory (Exa: myproject) and install ansible
cd ~/myproject
pipenv install ansible
  • Check the ansible version to confirm installation in the shell.
pipenv shell
(myproject) [myproject]$ ansible --version
  • Create the ansible.cfg configuration file.
(myproject) [myproject]$ vim ansible.cfg
  • Create the inventory yaml and add this variable to each group:
ansible_python_interpreter: /opt/rh/rh-python36/root/usr/bin/python
  • Later Update ansible using:
pipenv update ansible
  • To start a new project create a new folder and set the python version.
scl enable rh-python36 bash (Only once per session)
mkdir ovirtproject; cd ovirtproject
pipenv --python 3.6
pipenv install ansible
pipenv shell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment