mig-foxbat mig-foxbat - 21 days ago 5
Python Question

Missing SimpleJson Module

I have Ansible playbook run against a machine with CentOS release 5.6 (Final).
I have simplejson installed in the target machine and the module is importable from the python interpreter.
But still my playbooks fails with the below error.

Error: ansible requires a json module, none found!


I am confirming the presence of the simple json module at runtime by a raw module as shown below.

---
-
gather_facts: false
hosts: "{{ host_group }}"
name: deploy
vars_files:
- "{{env}}.yml"
tasks:
- name: check python version
raw: python -c "import simplejson"

- name: "git checkout"
git: "repo={{repository}} dest={{base_dir}} version={{branch}}"


The first step succeeds without any issue as shown below

TASK: [check python version] **************************************************
ok: [my-target-machine] => {"rc": 0, "stderr": "", "stdout": ""}


but the second fails with the above said error of missing json module.

Answer

This could be happening because you have two versions of python: the system python at /usr/bin/python and another python at perhaps /usr/local/bin/python. If the first-on-path python is >=2.5 or otherwise has simplejson in its site-packages, the first task would execute fine. However, if you've not installed simplejson for the system python at /usr/bin/python (easiest to just sudo yum -y install python-simplejson), then the git task could fail.

Standard ansible modules always use the #!/usr/bin/python shebang, and the git module is no exception.

Also, from the ansible documentation:

By default Ansible assumes it can find a /usr/bin/python on your remote system that is a 2.X version of Python, specifically 2.4 or higher.

Setting of an inventory variable ‘ansible_python_interpreter’ on any host will allow Ansible to auto-replace the interpreter used when executing python modules. Thus, you can point to any python you want on the system if /usr/bin/python on your system does not point to a Python 2.X interpreter.