2020-01-02 Upgrade to Python3 for Ansible

Overview

I wanted to update Ansible to use Python3.

References

Procedure

First Attempt to Install Python3 Version of Ansible

The procedure given in “Python 3 Support” is very simple:

pip3 install ansible

The command failed with:

Command 'pip3' not found, but can be installed with:


sudo apt install python3-pip

Install Pip3

As suggested by the error messages above, I ran the following command:

sudo apt install python3-pip

The command succeeded (the log is attached as install_pip3.log)

Second Attempt to Install Python3 Version of Ansible

I repeated the procedure given in “Python 3 Support”:

pip3 install ansible

The command succeeded as shown below:

Collecting ansible

Downloading https://files.pythonhosted.org/packages/f3/0d/ee54843308769f2c78be849d9e9f65dc8d63781941dc880f68507aae33ba/ansible-2.9.2.tar.gz (14.2MB)

100% |████████████████████████████████| 14.2MB 99kB/s

Collecting PyYAML (from ansible)

Downloading https://files.pythonhosted.org/packages/8d/c9/e5be955a117a1ac548cdd31e37e8fd7b02ce987f9655f5c7563c656d5dcb/PyYAML-5.2.tar.gz (265kB)

100% |████████████████████████████████| 266kB 763kB/s

Collecting cryptography (from ansible)

Downloading https://files.pythonhosted.org/packages/45/73/d18a8884de8bffdcda475728008b5b13be7fbef40a2acc81a0d5d524175d/cryptography-2.8-cp34-abi3-manylinux1_x86_64.whl (2.3MB)

100% |████████████████████████████████| 2.3MB 393kB/s

Collecting jinja2 (from ansible)

Downloading https://files.pythonhosted.org/packages/65/e0/eb35e762802015cab1ccee04e8a277b03f1d8e53da3ec3106882ec42558b/Jinja2-2.10.3-py2.py3-none-any.whl (125kB)

100% |████████████████████████████████| 133kB 821kB/s

Collecting six>=1.4.1 (from cryptography->ansible)

Downloading https://files.pythonhosted.org/packages/65/26/32b8464df2a97e6dd1b656ed26b2c194606c16fe163c695a992b36c11cdf/six-1.13.0-py2.py3-none-any.whl

Collecting cffi!=1.11.3,>=1.8 (from cryptography->ansible)

Downloading https://files.pythonhosted.org/packages/49/72/0d42f94fe94afa8030350c26e9d787219f3f008ec9bf6b86c66532b29236/cffi-1.13.2-cp36-cp36m-manylinux1_x86_64.whl (397kB)

100% |████████████████████████████████| 399kB 692kB/s

Collecting MarkupSafe>=0.23 (from jinja2->ansible)

Downloading https://files.pythonhosted.org/packages/b2/5f/23e0023be6bb885d00ffbefad2942bc51a620328ee910f64abe5a8d18dd1/MarkupSafe-1.1.1-cp36-cp36m-manylinux1_x86_64.whl

Collecting pycparser (from cffi!=1.11.3,>=1.8->cryptography->ansible)

Downloading https://files.pythonhosted.org/packages/68/9e/49196946aee219aead1290e00d1e7fdeab8567783e83e1b9ab5585e6206a/pycparser-2.19.tar.gz (158kB)

100% |████████████████████████████████| 163kB 816kB/s

Building wheels for collected packages: ansible, PyYAML, pycparser

Running setup.py bdist_wheel for ansible ... done

Stored in directory: /home/douglas/.cache/pip/wheels/3c/9c/04/feedbe29d7d314714eb5a8c8b5a18fe7fcc9ceca8f3789159c

Running setup.py bdist_wheel for PyYAML ... done

Stored in directory: /home/douglas/.cache/pip/wheels/54/b7/c7/2ada654ee54483c9329871665aaf4a6056c3ce36f29cf66e67

Running setup.py bdist_wheel for pycparser ... done

Stored in directory: /home/douglas/.cache/pip/wheels/f2/9a/90/de94f8556265ddc9d9c8b271b0f63e57b26fb1d67a45564511

Successfully built ansible PyYAML pycparser

Installing collected packages: PyYAML, six, pycparser, cffi, cryptography, MarkupSafe, jinja2, ansible

Successfully installed MarkupSafe-1.1.1 PyYAML-5.2 ansible-2.9.2 cffi-1.13.2 cryptography-2.8 jinja2-2.10.3 pycparser-2.19 six-1.13.0

Verify Installation

I used the following procedure to verify the installation of the Python3 version of Ansible:

ansible --version

The Python2 version was still active as shown below:

ansible 2.6.3

config file = /etc/ansible/ansible-ocm12c/ansible.cfg

configured module search path = [u'/home/douglas/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']

ansible python module location = /usr/lib/python2.7/dist-packages/ansible

executable location = /usr/bin/ansible

python version = 2.7.17 (default, Nov 7 2019, 10:07:09) [GCC 7.4.0]

Uninstall and Re-install Ansible

I used the following commands to uninstall and re-install Ansible. This time, I will use elevated privileges (as root) to install Ansible:

pip3 uninstall ansible

sudo pip3 install ansible

The logs are similar to above.

Find All Versions of Ansible

The verification tests said that I was still finding the Python2 version of Ansible. I used the following command to find all versions of the Ansible executable:

sudo find / -name ansible -executable -type f 2>/dev/null

The following three (3) versions were returned:

/usr/local/lib/python3.6/dist-packages/ansible_test/_data/injector/ansible

/usr/local/bin/ansible

/usr/bin/ansible

I assume I can safely ignore the first result returned.

The second result is the Python2 as shown below:

douglas@auburn:/etc/ansible/ansible-ocm12c$ /usr/bin/ansible --version

ansible 2.6.3

config file = /etc/ansible/ansible-ocm12c/ansible.cfg

configured module search path = [u'/home/douglas/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']

ansible python module location = /usr/lib/python2.7/dist-packages/ansible

executable location = /usr/bin/ansible

python version = 2.7.17 (default, Nov 7 2019, 10:07:09) [GCC 7.4.0]

The third result is the Python3 version as shown below:

douglas@auburn:/etc/ansible/ansible-ocm12c$ /usr/local/bin/ansible --version

ansible 2.9.2

config file = /etc/ansible/ansible-ocm12c/ansible.cfg

configured module search path = ['/home/douglas/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']

ansible python module location = /usr/local/lib/python3.6/dist-packages/ansible

executable location = /usr/local/bin/ansible

python version = 3.6.9 (default, Nov 7 2019, 10:44:02) [GCC 8.3.0]

Logoff and Logon

I got the new version of Ansible after I logged off and back on to Auburn:

douglas@auburn:~$ which ansible

/usr/local/bin/ansible

douglas@auburn:~$ ansible --version

ansible 2.9.2

config file = /etc/ansible/ansible.cfg

configured module search path = ['/home/douglas/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']

ansible python module location = /usr/local/lib/python3.6/dist-packages/ansible

executable location = /usr/local/bin/ansible

python version = 3.6.9 (default, Nov 7 2019, 10:44:02) [GCC 8.3.0]