Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update all examples to assume Python 3 by default #93

ss10sb opened this issue May 16, 2018 · 5 comments

Update all examples to assume Python 3 by default #93

ss10sb opened this issue May 16, 2018 · 5 comments


Copy link

ss10sb commented May 16, 2018

I think it might be worth adding a blurb early on (maybe in the 'creating a basic inventory file' part) about setting ansible to use python 3 by default (for distros that no longer come with 2.7).

For [example], it could be as simple as mentioning you can add a couple of lines to the inventory file like:

Or even directly to the host line. Just to get things started and not fail immediately on the ping example :)

Copy link

geerlingguy commented Aug 9, 2018

@ss10sb - Good idea! Right now the only OSes where this is a concern are Ubuntu 16 and 18... but it will become more and more important.

I think I'll add a note/warning in there about it. Hopefully Ansible itself will eventually get more intelligent about detecting Python 3 installed on a system :/

See related issue upstream: ansible/ansible#26163

Copy link

geerlingguy commented Aug 20, 2019

It's now an issue on Debian 10, which includes Python 3 as the default (though I think it aliases python to python3...? Not sure about pip).

So yes, I think at some point I'm going to try to figure out how to introduce this a little more gently. Otherwise, I wonder if support is now better, as according to the docs, Ansible will automatically use python3 on certain managed OSes...

I'll have to do some testing to see if I still need to do my hacky hack:

    - name: Install Python if not already present.
      raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)
      changed_when: false

    - name: Gather facts after Python is definitely present.

Copy link

goagex commented Dec 31, 2019


I have found an issue installing pip via apt on Debian 10 (apt-get install python3-pip)
The issue is that you get a old version of pip3 (like all debian packages)
No wrong in that, but if you want to upgrade pip-installed packages later on this breaks, as pip3 is also upgraded.

A solution for this is to install your ansible node with only pip(3)-packages.
You install as few as possible via apt.

apt-get update
apt-get install python3-distutils curl sshpass

# Download and install pip the official pip way
curl -o

# Install needed pip3 packages
pip3 install pynacl asn1crypto bcrypt httplib2 paramiko
pip3 install ansible

Upgrade of all pip-installed packages is now easy:

pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 pip install -U

Command taken from Stack Overflow: How to upgrade all Python packages with pip?

This way your ansible-server is installed with only python3, no python2 needed at all.

ansible 2.8.7
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.7/dist-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.7.3 (default, Apr 3 2019, 05:39:12) [GCC 8.3.0]

When you install ansible via pip, you will not get the default ansible.cfg in /etc/ansible.
Just create the folders yourself, and add the default ansible.cfg

@geerlingguy geerlingguy changed the title python 3 by default (ubuntu 16.04+) Update all examples to assume Python 3 by default Jan 16, 2020
Copy link

geerlingguy commented Jan 16, 2020

Updating issue title, and also noting changes from closed PR #148.

Copy link

geerlingguy commented Jul 24, 2020

Through various other issues, I've actually updated almost everything to default to Python 3 now, especially after updating examples to use Debian 10 and Ubuntu 20.04 (both of which have Python 3 by default and make it slightly harder to use Python 2).

So I think I can confidently close out this issue after removing one more change that is not necessary anymore (see commit coming soon). There are still a few places in the book where Python 2 is used for CentOS 7 examples, but a couple of those have issues open for them or I'll work on updating them (e.g. switching to CentOS 8) at a later date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

No branches or pull requests

3 participants