# Python interpreter problem on Ubuntu 16.04

**URL:** <https://forum.ansible.com/t/python-interpreter-problem-on-ubuntu-16-04/4472>\
**Category:** Get Help\
**Tags:** ubuntu\
**Created:** [March 20, 2024, 10:24am UTC](https://forum.ansible.com/t/python-interpreter-problem-on-ubuntu-16-04/4472 "2024-03-20T10:24:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dominicpratt](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dominicpratt/32/2634_2.png) [@dominicpratt](https://forum.ansible.com/u/dominicpratt)\
**Post date:** [March 20, 2024, 10:24am UTC](https://forum.ansible.com/t/python-interpreter-problem-on-ubuntu-16-04/4472/1 "2024-03-20T10:24:39Z")

</div>

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:

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

```

My playbook:

```auto
    - 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:

```auto
    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:

```auto
    ❯ 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.

```auto
root@www3 ~ # python --version
Python 3.9.13

```

Can someone help me and give me a hint?

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![Denney-tech](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/denney-tech/32/1556_2.png) [@Denney-tech](https://forum.ansible.com/u/Denney-tech)\
**Post date:** [March 21, 2024, 5:04pm UTC](https://forum.ansible.com/t/python-interpreter-problem-on-ubuntu-16-04/4472/2 "2024-03-21T17:04:35Z")

</div>

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?

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

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

```

---

<div class="post-metadata">

**Author:** ![dominicpratt](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dominicpratt/32/2634_2.png) [@dominicpratt](https://forum.ansible.com/u/dominicpratt)\
**Post date:** [March 21, 2024, 5:27pm UTC](https://forum.ansible.com/t/python-interpreter-problem-on-ubuntu-16-04/4472/3 "2024-03-21T17:27:02Z")

</div>

Hey, thanks for your answer @Denney-tech!

Here’s the output:

```auto
❯ ~ 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

```auto
❯ 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! 🙂

---

<div class="post-metadata">

**Author:** ![Denney-tech](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/denney-tech/32/1556_2.png) [@Denney-tech](https://forum.ansible.com/u/Denney-tech)\
**Post date:** [March 21, 2024, 7:40pm UTC](https://forum.ansible.com/t/python-interpreter-problem-on-ubuntu-16-04/4472/4 "2024-03-21T19:40:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Denney-tech](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/denney-tech/32/1556_2.png) [@Denney-tech](https://forum.ansible.com/u/Denney-tech)\
**Post date:** [March 21, 2024, 7:57pm UTC](https://forum.ansible.com/t/python-interpreter-problem-on-ubuntu-16-04/4472/5 "2024-03-21T19:57:15Z")

</div>

Based on a [recent issue](https://github.com/ansible/ansible/issues/82524) 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.

```yaml
- 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

```

---

<div class="post-metadata">

**Author:** ![dominicpratt](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dominicpratt/32/2634_2.png) [@dominicpratt](https://forum.ansible.com/u/dominicpratt)\
**Post date:** [March 22, 2024, 7:33am UTC](https://forum.ansible.com/t/python-interpreter-problem-on-ubuntu-16-04/4472/6 "2024-03-22T07:33:57Z")

</div>

Thanks for your answer!

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![Denney-tech](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/denney-tech/32/1556_2.png) [@Denney-tech](https://forum.ansible.com/u/Denney-tech)\
**Post date:** [March 22, 2024, 1:52pm UTC](https://forum.ansible.com/t/python-interpreter-problem-on-ubuntu-16-04/4472/7 "2024-03-22T13:52:47Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![dominicpratt](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dominicpratt/32/2634_2.png) [@dominicpratt](https://forum.ansible.com/u/dominicpratt)\
**Post date:** [March 27, 2024, 8:19am UTC](https://forum.ansible.com/t/python-interpreter-problem-on-ubuntu-16-04/4472/8 "2024-03-27T08:19:54Z")

</div>

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 😉

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 26, 2024, 8:20am UTC](https://forum.ansible.com/t/python-interpreter-problem-on-ubuntu-16-04/4472/9 "2024-04-26T08:20:29Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
