I started an Ubuntu EC2 the other day hoping to run Ansible on it and was surprised that Python was not installed. A requirement for Ansible to run on target machines is to have a modern version of Python.

You can just run sudo apt install python but this is not ideal if you need to install python on let’s say 200 Ubuntu Servers.

The raw module to the rescue

Before writing the playbook, you will need to make minor changes to how Ansible connects to remote hosts.

Create a new file in your current directory and call it ansible.cfg. This file will override settings in the default /etc/ansible/ansible.cfg. Example:

[defaults]
inventory = hosts
remote_user = ubuntu
# Use the below if you’re connecting for the first time
host_key_checking = False
# Location of your .pem you got from AWS
private_key_file = ~/.ssh/yourkey.pem

Now create a hosts file:

[ubuntu_server]
34.210.125.120 # This is a sample IP address. Use your IP.

The playbook using the raw module

This playbook will install python on the target host Ubuntu EC2 server:

- hosts: ubuntu_server
become: yes
gather_facts: no
pre_tasks:
- name: 'install python'
raw: 'sudo apt -y install python'

Leave a comment

Your email address will not be published.