Skip to content

Instantly share code, notes, and snippets.

@gwillem
Created June 16, 2016 21:59
Show Gist options
  • Save gwillem/4ba393dceb55e5ae276a87300f6b8e6f to your computer and use it in GitHub Desktop.
Save gwillem/4ba393dceb55e5ae276a87300f6b8e6f to your computer and use it in GitHub Desktop.
Get Ansible to work on bare Ubuntu 16.04 without python 2.7
# Add this snippet to the top of your playbook.
# It will install python2 if missing (but checks first so no expensive repeated apt updates)
# gwillem@gmail.com
- hosts: all
gather_facts: False
tasks:
- name: install python 2
raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)
@eengstrom
Copy link

FWIW, I've got an ansible role based upon this thread that works for EL/RedHat/Debian/Ubuntu/FreeBSD. I just published it to ansible- galaxy. Would love for some feedback.

@rmbleeker
Copy link

rmbleeker commented Nov 13, 2019

@staekblw

does anyone know how to install python if the target machine has no internet access? Let's say I have python package located on my control machine, is there any way to copy the python package to target machine?

I hope you've found a solution by now, but if not my suggestion would be to use 'local_action' and perform an scp or sftp command.

@mahlingam
Copy link

mahlingam commented Jan 7, 2020

Lil' update, on ubuntu 18.04 installing python-minimal will prompt interactively for some libssl installation, which then sits there forever. Using test -e /usr/bin/python || (apt -y update && UCF_FORCE_CONFOLD=1 DEBIAN_FRONTEND=noninteractive apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -qq -y install python-minimal) fixes this behavior.
Inspired by https://bugs.launchpad.net/ubuntu/+source/ansible/+bug/1833013/comments/6

@maherio
Copy link

maherio commented Feb 8, 2020

Why is the raw command necessary for this? I've been having trouble running this lately (likely due to @mahlingam's comment above), and I found that this seems to work for me:

- hosts: all
  become: true
  become_user: root
  become_method: sudo
  gather_facts: no
  pre_tasks:
    - name: Install python for Ansible
      apt:
        update_cache: yes
        name:
          - python-minimal
    - setup: # aka gather_facts
  tasks:
    ...

Just wondering if there's a reason not to do this

@ahmgithubahm
Copy link

Just wondering if there's a reason not to do this

Yep, I think it's simply that a target server without python installed, is unable to run normal ansible task steps. Using 'raw' side-steps the need for python.

@ahmgithubahm
Copy link

PS I think bootstrapping a Python 2 installation is now likely to be unnecessary, since Ansible 2.8+ will auto-detect a suitable Python interpreter to use anyway.

https://docs.ansible.com/ansible/latest/reference_appendices/interpreter_discovery.html

@ProExpertProg
Copy link

If anyone is looking for a solution for Ubuntu 20.04 - change python-minimal to python2-minimal.

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