How to install python in target host using Ansible

Knoldus Blog Audio
Reading Time: 3 minutes

Ansible is an open-source automation engine that automates software provisioning, configuration management, and application deployment. It is quickly becoming the popular configuration management tool today. It lets you to control and config the target nodes from single host machine.

As we know the only requirement in target machine is the modern version of python installed. But what if the python version is not installed on the target machines. You can do it manually on some machine but what if you have tons of target server which does not have python installed by default.

The raw module

The ansible_base run on python because we want ansible to manage a wide variety of machines. Any module that Ansible uses against target host uses python internal to make required changes. But what you do if target does not have python installed? So for this ansible has raw module. This module is very useful and is used in various cases, one of is installing python on target host. Let’s begin that how can we install python on target host using ansible.

Setting up Ansible to connect with EC2-instance

So before we moving forward, we need to configure ansible to connect to ec2-instance. Firstly we need to make an inventory file in the current directory. An inventory file that consists of our remote machine IP address. I have created a python group that consists of an ec2 instance IP address. Now, we add variables for the python group to use the private key of the server. For this, we give the path of the private key for variable ansible_ssh_private_key_file.

Inventory

[python]
34.232.76.203

[python:vars]
ansible_user=ubuntu
ansible_ssh_private_key_file=./ansibletest.pem

Creating Ansible playbook to install python

Next, we need to create a playbook that contains a raw module to install python on the target machine. I have created a install_python.yml file as follows:

---
- name: Install python in target node with ansible
  hosts: python
  become: true
  gather_facts: no
  pre_tasks:
  - name: install python
    raw: 'cat < /home/ubuntu/script.sh'
    args:
      executable: /bin/bash
script.sh
#!/bin/bash
declare -A osInfo;
osInfo[/etc/debian_version]="apt"
osInfo[/etc/alpine-release]="apk"
osInfo[/etc/centos-release]="yum"
osInfo[/etc/fedora-release]="dnf"

for f in ${!osInfo[@]}
do
    if [[ -f $f ]];then
        package_manager=${osInfo[$f]}
    fi
done

sudo $package_manager install python3

In the above playbook, the most important part is should be no if python is not installed. As we know gather_facts uses python to collect the target machine information. become is uses to get the privileges of the root user. When we need to install python, this must be specified in pre-tasks section of the playbook. By specifying any pre-work in the pre_task, it should first execute pre_tasks after connecting to the host server.

After that raw module contains straight Linux command or script, it consists of a script that first gathers the package manager and then uses that package manager to install python in the target machine. Also, we need to specify the executable argument through which the script runs like bash, sh, etc.

Running Ansible playbook

Running a playbook is as easy as issuing the following command:

$ ansible-playbook -i inventory install_python.yml 

In the above ansible-playbook command we give our own inventory using “-i” flag.

After running this command you will see the below output :

PLAY [Install python in target node with ansible] ********************************************************************************************

TASK [install python] ************************************************************************************************************************
changed: [34.232.76.203]

PLAY RECAP ***********************************************************************************************************************************
34.232.76.203              : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

You can double check that python installed in target host by simple SSHing into the server and type python –version.

Conclusion

In this blog, we learn that how can we install python in target host with ansible using raw module.

There are some workaround with this approach that you should not include this pre-tasks to install python in your main playbook as it is going to install python again and again which is not a good practice.

So it should be run only once. Other thing is that gather_facts is no so you should not use this in main playbook as there as some tasks that use gather_facts to gather some important information of host to use in the playbook like package information.

You can create a separate playbook that will just make sure that all target machines have Python installed. Subsequent Ansible tasks can be added to another playbook where gather_facts can be turned on.

References

https://docs.ansible.com/ansible/2.3/raw_module.html