In this presentation, I’m going to show you how to deploy Ansible for Python 3. I’m going to focus on the Python 3 aspects of it so if you’re not familiar with to Ansible, head over to Ansible Docs first – it’s an excellent resource for everyone who wants to learn more about Ansible.

If you already know Ansible and you’re curious why it’s a good idea to deploy it in Python 3, read on.

I’m going to talk about why Python 3 is the tool you should be using for Ansible deployment, what’s different in its setup and configuration, and how to run your deployment pipeline for optimal results.

Why Python 3?

Python 2 works pretty well with Ansible; there’s no doubt about it. But I think it’s a great idea to switch to Python 3 if you’re planning to include Ansible in your project.

Here are some good reasons why. Python 2.7 will be marked as End-Of-Life (EOL) pretty soon – in 2020, to be exact. That means you actually don’t have that much time to switch.

Moreover, some frameworks have already moved to Python 3 exclusively, so you might encounter problems when keeping your codebase in Python 2. Django is a good example here.

Finally, don’t forget that Python 3 was released in 2008, so it’s been around for a long time. Most of the existing tools, libraries, and frameworks support it, so there’s no reason to hesitate and keep on working with Python 2.

Why is Python 3 better for Ansible deployment?

  • The primary reason is that with Python 3, you no longer need to install the Python 2 on the host machine (that means no more hacking your pre-tasks with special roles, especially since most providers like Digital Ocean offer Python 3 packages out of the box in the Ubuntu installation images, so that’s easy);
  • Python 3 will be supported for a long time (definitely longer than 2 years);
  • Python 3 has a better syntax than Python 2 (the improved syntax is a controversial claim, but I’ve seen many cases where this turns out to be true).

There are also some downsides to using Python 3 for Ansible, and I thought I should mention them here

  • Some roles might not work out of the box, and you might need to work extra to introduce changes and research why they’re not working (for example, PostgreSQL can be problematic here, but I’ll show you how to fix it later on)
  • Sometimes it requires a bit more effort to set up and configure your deployment.

Now that we’ve covered the advantages of deploying Ansible with Python 3, let’s see how that works in practice.


First things first, you need to set up and configure your deployment.

To get started, you need to meet the following requirements:

  • Python 3 installed on your operating system and host system;
  • Latest Ansible version with its dependencies installed in your virtual env (if you pick an older version, you might encounter issues that haven’t been resolved in previous versions – note that Python 3 support to Ansible was added quite recently). Keep in mind that when you install Ansible on your Python 3 virtual env, you no longer need to setup additional Python 2 virtual env for Ansible itself – that’s pretty convenient, right?
  • SSH or PowerShell (MS Windows).

Once you put all these elements together, you can start setting up the deployment. Here’s how to do it.

First, you need to add the deployment directory to the root directory of our repository project.

We have 3 directories here:

  • tasks – tasks are a set of playbook utils and handles;
  • templates – templates files used e.g. for uWSGI config;
  • vars – that directory contains Ansible configuration environment details.

We also have 3 files:

  • environment.ini – this is the inventory file for a given host;
  • roles.yml – this file is about the roles used in this deployment by the client;
  • site.yml – this file is used as a playbook that in turn uses tasks, templates, and vars.

That’s a slightly simplified presentation, but it’s enough to get you going.


What’s the difference between configuring Ansible for Python 2 and 3?


The first key difference lies in the inventory. You need to add a specific Python interpreter to a given inventory file, to a given host. In Python 3, that interpreter will be used on a given host during deployment.


It’s smart to install the latest roles version – an older version release might not contain fixes that are present in the repository.

As I’ve mentioned before, most of the time you’ll need to apply some changes in the existing roles. For example, ANXS.postgresql is missing python3-psycopg2 in dependencies for Debian-based systems.


In the case of playbooks, the only thing you need to keep in mind is to drop the Python 2 installation because it won’t be necessary anymore. Besides that, most playbooks won’t require any changes.


The process of running an Ansible deployment is pretty simple now. Just run it as usual – there are no differences, and you won’t need to add any special flags.

And the job is done!

Have you got any questions about deploying Ansible in Python 3? Leave a comment below or drop us a line at [email protected], we’re happy to share our knowledge with beginner and advanced Pythonistas alike!


The Ultimate Tutorial for Django REST Framework: Selective Fields and Related Objects (Part 7)

This is the last part of my tutorial for the Django REST framework. Be sure to catch up with the work we’ve completed in other parts of the series: [...]

Data science

Data Mining vs. Machine Learning: What do they have in common and how are they different?

Here’s something practically every company out there is focusing on today: data. Organizations are collecting and processing data in unprecedented volume. They’re also using sophisticated data analytics solutions that [...]

Get insights from software experts.

Almost finished…

But we need to confirm your email address first.

To complete the subscription process, please click the link in the email we’ve just sent you.

Sunscrapers Sp. z o.o.

ul. Pokorna 2/947

Warsaw 00-199


Add us to your address book

Thanks for subscribing!

Your email address already exists in our database.

Every month, you’ll get a portion of insights about tech trends, best practices in building software, and managing tech teams. You’ll hear from us soon.

Scroll to bottom

Hi there, we use cookies to provide you with an amazing experience on our site. If you continue without changing the settings, we'll assume that you're happy to receive all cookies on the Sunscrapers website. You can change your cookie settings at any time.

Learn more

Learn how to create a REST API for Django projects !

Build a functional REST API with the Django REST Framework

Download ebook No, thank you
Rest API eBook