Skip to content

How to get newer versions of Ansible and Python on EC2s

0

I'm trying to create a Lambda function that calls an Ansible playbook. This post relates to my attempting to create a layer via .zip file.

The error I get when testing reveals an incompatibility between versions of Ansible and Python.

On our EC2s, currently installed versions of Ansible (2.9x) and Python (3.7x) are way behind what's available. I'd like 2.10x of Ansible and, at least, 3.12 of Python. Unfortunately, 'yum' ('dnf' not installed) does not show anything beyond 2.9x for Ansible. 'pip3', nothing newer than 3.7x for Python.

I need the newer versions for Lambda compatibility which, for our environment, seems to require Python3.9x and above though 3.12x is preferred.

It may be that AWS runbooks will also do what I'm looking for but, please, let's leave that as a discussion for another day.

asked a year ago601 views
2 Answers
2

For most part, answer given above stands valid. I am adding few things here for more clarity.

For Ansible, consider utilizing Python Package Manager (pip):

  1. Use pip

    pip install --upgrade ansible
    
  2. Use official repositories

For Ubuntu/Debian

sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible

For RHEL/CentOS

sudo yum install ansible

For Python, you can either compile from source or use Python Environment Manager (pyenv)

  1. Use pyenv

    curl https://pyenv.run | bash
    pyenv install 3.x.x
    pyenv global 3.x.x
    
  2. Compile from source:

    wget https://www.python.org/ftp/python/3.x.x/Python-3.x.x.tgz
    tar xzf Python-3.x.x.tgz
    cd Python-3.x.x
    ./configure --enable-optimizations
     make altinstall
    
  3. For Amazon Linux 2

     sudo amazon-linux-extras install python3.8
    
AWS
EXPERT
answered a year ago
EXPERT
reviewed a year ago
  • Did it help in unblocking the issue, comment here if you have additional questions, happy to help.

0

To get newer versions of Ansible and Python on your EC2 instances for Lambda compatibility, you'll need to take a different approach than using the default package managers. Here's how you can achieve this:

  1. For Python: Instead of relying on yum or pip3, you should consider using a tool like pyenv to manage multiple Python versions on your EC2 instances. This allows you to install and use newer Python versions without affecting the system-wide installation.

    To install pyenv:

    • Install the necessary dependencies
    • Download and install pyenv
    • Add pyenv to your shell configuration
    • Install the desired Python version (e.g., 3.12) using pyenv
  2. For Ansible: Since the repositories don't have the version you need, you can install Ansible using pip after setting up the newer Python version. This ensures Ansible is compatible with the Python version you're using.

    • Activate the Python environment you set up with pyenv
    • Use pip to install the desired Ansible version: pip install ansible==2.10.0 (or whichever specific version you need)

For your Lambda function, you should create a layer with the appropriate Python runtime and Ansible version. Here's how:

  1. Create a directory for your layer (e.g., my-ansible-layer)
  2. Inside this directory, create a python folder
  3. Install Ansible and its dependencies into this folder: pip install ansible -t ./python
  4. Zip the contents of the python folder
  5. Upload this zip file as a new layer in the AWS Lambda console
  6. Attach the layer to your Lambda function

When creating your Lambda function, make sure to select the appropriate Python runtime (e.g., Python 3.9 or 3.12) that matches the version you used to create the layer.

By following these steps, you can ensure that your Lambda function has access to the correct versions of Python and Ansible, independent of what's installed on your EC2 instances. This approach allows you to use the newer versions required for Lambda compatibility while maintaining the existing setup on your EC2 instances.
Sources
Community | Step-by-Step Guide to Creating an AWS Lambda Function Layer
Working with layers for Python Lambda functions - AWS Lambda

answered a year ago
AWS
EXPERT
reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.