0

I'm creating a custom AMI using packer and ansible. The base AMI is an Ubuntu 20.04 provided by the Canonical and it's customized using an ansible playbook. I'm installing python3.9 and setting it as the default python:

        - name: Setup Python3.9 As Default Python Interpreter
      shell: |
          sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 1
          sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2

Each EC2 instance based on this AMI is unreachable via ssh (I have installed openssh-server and placed the keys). Once the instance is rebooted I can ssh to the server. The issue seams to be with the cloud-init package that is not working with python3.9. What did I try?

  1. Disable the cloud-init package - no ssh connection even after the reboot
  2. Manually install the latest version of cloud-init - no ssh on the initial boot, ssh after the reboot worked. Got this error - ubuntu@ip-XXX-XX-XX-XXX:~$ docker Traceback (most recent call last): File "/usr/lib/command-not-found", line 28, in <module> from CommandNotFound import CommandNotFound File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 19, in <module> from CommandNotFound.db.db import SqliteDatabase File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in <module> import apt_pkg ModuleNotFoundError: No module named 'apt_pkg'
  • Don't forget if you change the default python3 version; all Ubuntu tools that rely on that may misbehave which leads to two results (a) they won't work (best outcome) or (b) in rare cases corruption. If you change default python3, you can't use user tools like apt etc being limited to tools that don't use python3! Unless you're very aware of your Ubuntu system & thus what you can/cannot use, do not change the default python3 where you value your data. – guiverc Jul 26 '23 at 22:48
  • 1
  • The answer was to set the specific order of things. The final goal was achieved via an external script run from Ansible playbook.
    set -x
    export DEBIAN_FRONTEND=noninteractive
    sudo apt update
    sudo apt purge -y python3-apt
    sudo apt install -y python3.9
    sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 2
    sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1
    
    – droritzz Nov 06 '23 at 13:42

0 Answers0