Python interpreter problem on Ubuntu 16.04

Good morning,

I have a problem with my Ansible playbook to upgrade all APT packages on two remote machines running Ubuntu 16.04.

My inventory:

    [example]
    www3.example.org ansible_python_interpreter="/root/.pyenv/shims/python"
    www4.example.org ansible_python_interpreter="/root/.pyenv/shims/python"

My playbook:

    - hosts: all
      become: yes
      tasks:
        - name: Update apt repo and cache on all Debian/Ubuntu boxes
          apt: update_cache=yes force_apt_get=yes cache_valid_time=3600
    
        - name: Upgrade all packages on servers
          apt: upgrade=dist force_apt_get=yes
    
        - name: Autoremove unused packages
          apt: autoremove=true

The output:

    TASK [Update apt repo and cache on all Debian/Ubuntu boxes] ********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
    fatal: [www4.example.org]: FAILED! => {"changed": false, "msg": "ansible-core requires a minimum of Python2 version 2.7 or Python3 version 3.6. Current version: 3.5.2 (default, Jan 26 2021, 13:30:48) [GCC 5.4.0 20160609]"}
    fatal: [www3.example.org]: FAILED! => {"changed": false, "msg": "ansible-core requires a minimum of Python2 version 2.7 or Python3 version 3.6. Current version: 3.5.2 (default, Jan 26 2021, 13:30:48) [GCC 5.4.0 20160609]"}

Ansible Ping:

    ❯ ansible (master) ✘ ansible -i hosts -m "ping" example
    www4.example.org | SUCCESS => {
        "changed": false,
        "ping": "pong"
    }
    www3.example.org | SUCCESS => {
        "changed": false,
        "ping": "pong"
    }

I have set ansible_python_interpreter for the two hosts (see inventory), but unfortunately it still seems to want to use a wrong interpreter.

There is a current Python 3 (/root/.pyenv/shims/python) on both systems via pyenv.

root@www3 ~ # python --version
Python 3.9.13

Can someone help me and give me a hint?

Thanks a lot!

We’re going to need some more information.

What does ansible --version return you on your control node/local machine?

What does ansible_python_interpreter actually return you in a playbook?

- hosts: all
  become: yes
  tasks:
    - name: Show interpreter
      debug: var=ansible_python_interpreter

    - name: Get interpreter version
      command: "{{ ansible_python_interpreter }} --version"
1 Like

Hey, thanks for your answer @Denney-tech!

Here’s the output:

❯ ~ ansible --version
ansible [core 2.16.4]
  config file = None
  configured module search path = ['/Users/dominic/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/homebrew/Cellar/ansible/9.3.0/libexec/lib/python3.12/site-packages/ansible
  ansible collection location = /Users/dominic/.ansible/collections:/usr/share/ansible/collections
  executable location = /opt/homebrew/bin/ansible
  python version = 3.12.2 (main, Feb  6 2024, 20:19:44) [Clang 15.0.0 (clang-1500.1.0.2.5)] (/opt/homebrew/Cellar/ansible/9.3.0/libexec/bin/python)
  jinja version = 3.1.3
  libyaml = True

and

❯ ansible (master) ✘ ansible-playbook -i ubuntu1604-hosts playbooks/python-interpreter.yml

PLAY [all] *********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [www4.example.org]
ok: [www3.example.org]

TASK [Show interpreter] ********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [www3.example.org] => {
    "ansible_python_interpreter": "/root/.pyenv/versions/3.9.13/bin/python3"
}
ok: [www4.example.org] => {
    "ansible_python_interpreter": "/root/.pyenv/versions/3.9.13/bin/python3"
}

TASK [Get interpreter version] *************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
changed: [www4.example.org]
changed: [www3.example.org]

PLAY RECAP *********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
www3.example.org              : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
www4.example.org              : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Hope this helps! :slight_smile:

Heh, I forgot to tell you to increase the verbosity so we would see what was returned by the second task. That being said, I think it’s safe to assume it should be 3.9.13 based on the path above.

This leads me to believe that this problem is specific to the Apt module being stuck with the platform version of python which is only python 3.5 in Ubuntu 16.04. Are you able to perform OS upgrades up to 20.04+? Ubuntu 16.04 and 18.04 are already EOL.

If not (or if that’s part of the objective with this playbook), then you may need to use command/shell or even the raw modules to do what you need to with apt.

1 Like

Based on a recent issue that was closed as “wontfix” essentially, you will have to install python2.7-apt on the host (if it doesn’t have it already) and specifically use python2 as the interpreter. I think you can set the interpreter as a var specifically for the apt module steps.

- hosts: all
  become: yes
  tasks:
    - name: Update apt repo and cache on all Debian/Ubuntu boxes
      apt: update_cache=yes force_apt_get=yes cache_valid_time=3600
      vars:
        ansible_python_interpreter: /usr/bin/python2

    - name: Upgrade all packages on servers
      apt: upgrade=dist force_apt_get=yes
      vars:
        ansible_python_interpreter: /usr/bin/python2

    - name: Autoremove unused packages
      apt: autoremove=true
      vars:
        ansible_python_interpreter: /usr/bin/python2
1 Like

Thanks for your answer!

Unfortunately, that doesn’t work either… what a pity.

FAILED! => {"ansible_facts": {}, "changed": false, "failed_modules": {"ansible.legacy.setup": {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"}, "failed": true, "msg": "ansible-core requires a minimum of Python2 version 2.7 or Python3 version 3.6. Current version: 3.5.2 (default, Jan 26 2021, 13:30:48) [GCC 5.4.0 20160609]"}}, "msg": "The following modules failed to execute: ansible.legacy.setup\n"}

I think I’ll give up on this project now and hope that the machines will be retired soon.

Did you unset the ansible_python_interpreter in inventory? You wouldn’t need to; we just want to override apt and which will take precedence over inventory vars. If gather_facts doesn’t work in spite of specifying the python3.9 available your venv, I guess you’ll have to set the interpreter to python2 by default then.

The only alternative I see is to specifically install an older version of ansible on your control node to handle these. Since you seem to be familiar with venv’s already, you could make one specifically for installing an older version of ansible. I think ansible-core~=2.11.0 or ~=2.12.0 might be able to bridge the gap. `pip install “ansible>=4.0.0,<5.0.0” in your venv (or “ansible>=5.0.0,<6.0.0” for core 2.12) and see if it’s okay with the discovered interpreters. (if you’re using AWX/AAP, you can also create a custom EE with an older vs of ansible to do this)

Regardless, they definitely need to be upgraded or retired. I’m in a similar boat with RHEL 7 going EOL in June. Good luck!

1 Like

Hey,

sorry for the late reply.

I have now temporarily solved the problem by downgrading Ansible to core version 2.14.6 (Ansible 7.6.0)

That works now… Time to sort out the two machines :wink:

1 Like