DEBOPS.PYTHON(5)

NAME
debops.python - Manage Python environment using Ansible

SYNOPSIS

debops service/python [--limit group,host,...] [--diff] [--check] [--tags tag1,tag2,...] [--skip-tags tag1,tag2,...] [<ansible-playbook options>] ...

debops service/python_raw [--limit group,host,...] [--diff] [--check] [<ansible-playbook options>] ...

DESCRIPTION

Python <https://www.python.org/> is a popular, dynamic programming language available on GNU/Linux platforms. Ansible is written in Python and requires Python on remote host for normal operations.

The debops.python Ansible role can be used to manage the Python environment on a Debian/Ubuntu host. Role supports multiple Python versions installed on a host at the same time, which is the default practice in Debian.

A special "raw" mode of operation with a separate Ansible playbook can be used to bootstrap Python environment on a host, so that Ansible can then install packages and operate normally.

GETTING STARTED

  Support for multiple Python versions

The role allows management of multiple Python versions at the same time. The available Python version is dependent on the OS release used on a given host.

Separate default variables can be used to specify APT packages to install for Python 2.x and Python 3.x series. The different Python versions can then be enabled or disabled independently - role automatically disables Python 3 support on older OS releases without adequate versions available, and disables Python 2 support if Ansible is configured to use Python 3 interpreter on a given host, or Python 3.x is autodetected as the Python interpreter on Ansible Controller.

  Python environment bootstrapping

The special "raw" mode of operation with a custom Ansible playbook that doesn't gather Ansible facts automatically can be used to "bootstrap" Python support on a host. Role will automatically purge an existing Python 2.x packages if Python 2.x operation is disabled; this can be leveraged to maintain Python 3.x-only setup, depending on the OS release used on the host.

  Example inventory

The debops.python role is included by default in the common.yml DebOps playbook; you don't need to add hosts to any Ansible groups to enable it.

  Example playbook

If you are using this role without DebOps, here's an example Ansible playbook that uses the debops.python role:

---
- name: Manage Python environment
  collections: [ 'debops.debops', 'debops.roles01',
                 'debops.roles02', 'debops.roles03' ]
  hosts: [ 'debops_all_hosts', 'debops_service_python' ]
  become: True
  environment: '{{ inventory__environment | d({})
                   | combine(inventory__group_environment | d({}))
                   | combine(inventory__host_environment  | d({})) }}'
  roles:
    - role: python
      tags: [ 'role::python', 'skip::python' ]

There's a separate Ansible playbook that allows usage of the debops.python role in a "raw" mode, without fact gathering. This can be used to bootstrap Python support on a host, so that normal Ansible modules can be used afterwards:

---
- name: Bootstrap Python environment
  collections: [ 'debops.debops', 'debops.roles01',
                 'debops.roles02', 'debops.roles03' ]
  hosts: [ 'debops_all_hosts', 'debops_service_python' ]
  strategy: linear
  become: True
  gather_facts: False
  tasks:
    - name: Initialize Ansible support via raw tasks
      import_role:
        name: 'python'
        tasks_from: 'main_raw'
      tags: [ 'role::python_raw', 'skip::python_raw' ]

  Ansible tags

You can use Ansible --tags or --skip-tags parameters to limit what tasks are performed during Ansible run. This can be used after a host was first configured to speed up playbook execution, when you are sure that most of the configuration is already in the desired state.

Available role tags:

role::python
    Main role tag, should be used in the playbook to execute all of the role tasks as well as role dependencies.

  Other resources

List of other useful resources related to the debops.python Ansible role:

•   Manual pages: python(1) <https://manpages.debian.org/python(1)>, pip(1) <https://manpages.debian.org/pip(1)>, virtualenv(1) <https://manpages.debian.org/virtualenv(1)>

•   Debian Python Policy <https://www.debian.org/doc/packaging-manuals/python-policy/>

•   Python page <https://wiki.debian.org/Python> in Debian Wiki

•   Status of Python 3 packages in Debian <https://wiki.debian.org/Python/Python3Packages>

•   Debian plans for Python 3 <https://lwn.net/Articles/642334/> article in Linux Weekly News

•   Python page <https://wiki.ubuntu.com/Python> in Ubuntu Wiki

•   Python (programming language) <https://en.wikipedia.org/wiki/Python_(programming_language)> in Wikipedia

•   import antigravity <https://xkcd.com/353/>

AUTHOR
Maciej Delmanowski

COPYRIGHT
2014-2022, Maciej Delmanowski, Nick Janetakis, Robin Schneider and others