How To Use Ansible For Centos 5 / RHEL 5

Published on Author gryzli

If you try to run some of your playbooks over old Centos/Rhel 5 host, you might be ending with weird message like this:

{"changed": false, "module_stderr": " File \"<stdin>\", line 39\n with open(module, 'wb') as f:\n ^\nSyntaxError: invalid syntax\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

Without going into too much detail, there is a problem when you try to use newer Ansible versions with older hosts or more precise hosts with older python version.

The main problem of operating Centos 5 is the python version, which by default is python 2.4. The problem is applicable to any other operating system using old python version.

 

Solution – Use older Ansible version, that works with python 2.4

 

1) Install older Ansible version on your manager host

Before you proceed, make sure you have “virtualenv” installed.

In order to not screw up your current Ansible installation, this could be achieved by using virtualenv, which is pretty handy.

virtualenv ansible2.3-env
source ansible2.3-env/bin/activate
pip install 'ansible==2.3'

 

If your Python version is newer than 3.7 , you must use 3.7 or older version for the virtualenv creation. If not, you will most probably end up with the following error: 

Unexpected Exception: invalid syntax (adhoc.py, line 85)

In this case use this (on Fedora for example)

virtualenv -p /usr/bin/python2
ansible2.3-env

 

2) Install python-simplejson on your Centos 5 machines

You will need the package “python-simplejson” installed on your host machines (the ones you are going to orchestrate).

This is as easy as executing:

yum install python-simplejson

 

3) Run playbook by using Ansible 2.3 on manager host

Finally you can try running your playbooks, by first activating the virtualenv and executing ansible-playbook.

# Activate ansible 2.3 environment
source ansible2.3-env/bin/activate

# Make sure you are using the correct version: 
ansible --version 

# Execute your playbook 
ansible-playbook some_playbook.yml

 

Some More Ansible Resources I’ve Written

Optimizing Ansible for Maximum Performance

Print All Variables in Ansible

Dump All Variables In Ansible

Ansible CheatSheet