Open In App

How To Install Python Using Ansible Playbook ?

Last Updated : 02 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Automation is now absolutely necessary for the effective management of software deployment and infrastructure in IT landscape. Ansible emerges as a main automation tool, eminent for its effortlessness, simplicity, and flexibility. Software installation, configuration management, and application deployment are all simple to automate and streamline with Ansible. Python, a broadly utilized programming language known for its readability and flexibility, fills in as an essential structure block in numerous automation work processes.

This guide focuses on harnessing the power of Ansible to automate the installation of Python, a basic part in numerous applications and system-level cycles. Users can define the desired state of their infrastructure and execute out consistent and repeatable tasks across multiple servers by utilizing Ansible playbooks. This saves time and exertion as well as ensure consistency and unwavering quality in programming provisioning.

Understanding how to install Python utilizing Ansible playbooks is beneficial for system administrators, DevOps engineers, and anybody associated with managing IT infrastructure. Using Ansible to automate the installation of Python for web development, data analysis, or automation tasks expedites software deployment cycles, reduces human error, and streamlines operations.

What is Python?

Python is a well-known high-level, interpreted programming language that is known for being to simplicity, readability, and versatility. Its clean syntax and easy-to-understand structure make it an ideal decision for beginners and experienced developers. Because variables do not need to be explicitly declared, Python’s dynamic typing system makes it possible to develop and prototype quickly. It supports different programming paradigms, including procedural, object-oriented, and utilitarian programming, giving adaptability in coding styles.

By providing a wide range of modules and functions for data manipulation, networking, and file handling, Python’s extensive standard library reduces the need for external dependencies. Its cross-platform similarity empowers code to run seamlessly on different operating systems, improving portability.

  • High-level and Interpreted: Python is a high-level language, and that implies it abstracts away many low-level subtleties like memory management and hardware interactions. It’s additionally an interpreted language, implying that code is executed line by line by an interpreter, as opposed to compiled into machine code in advance. This makes Python code simple to write, read, and debug.
  • Dynamic Typing: Python utilizes dynamic typing, so you don’t have to expressly pronounce the kind of a variable. All things being equal, the interpreter construes the information type in view of the worth assigned to it. This makes Python flexible and considers quick prototyping and development.
  • Readability: Python makes it simple for developers to express concepts in fewer lines of code thanks to its emphasis on readability and clear syntax. Its syntax structure looks like plain English, which decreases the cognitive load while reading and composing code, particularly for beginners.
  • Object-Oriented: Python supports object-oriented programming (OOP), allowing developers to make reusable and measured code using classes and objects. OOP concepts like encapsulation, inheritance, and polymorphism are major in Python programming.
  • Extensive Standard Library: From working with files and networks to handling data structures and module operations, Python comes with a large standard library of modules and functions. This dispenses with the need to compose code without any preparation for common tasks and promotes code reuse.
  • Cross-platform: Python is extremely portable because it runs on a variety of platforms, including Linux, Windows, and macOS. This allows developers to compose code once and run it on various operating systems without modification.

Step-by-step process to install Python using ansible-playbook

Step 1: Launch an instance

  • Go to Amazon console management and log into that with your credentials or create a New account.
  • Now launch two Instances because Ansible is a master-slave configuration management tool. So We are working from the master node and installing Python in the slave node. In the master node, we are defining the host details of the slave node.

Launch instance

  • Now connect with terminal like git bash,powershell, putty, command prompt and so on. By using SSH Command

SSH

Step 2: Install Ansible

Now Install Ansible on your control node (local machine). By using following command

 sudo amazon-linux-extras install ansible2

Install ansible

Step 3: Create a playbook

Now create a playbook with .yml extension

sudo vi <filename.yml>

Here is the playbook script to install PYTHON in slave node

- name: Install Python
hosts: slave # here give your instance name
become: yes
tasks:
- name: Install Python
package:
name: python
state: present

python yaml

Step 4: Setting up Host Permission for slave node

Now move to ansible directory path. In that ansible directory there is a file for HOST. In that host file we can provide host details. Following command navigate to ansible path

cd /etc/ansible/

ansible

  • Now open that Host file by using following command. Without sudo we cannot open that file
sudo vi host 

host

  • Inside this host file we providing host details that means our slave node details like Private IP Address, user name and keypairs details because to install our python.
  • Ensure that our instance keypair’s was downloaded in local machine that is our master node. We can download by using following command
 scp -i keyapir.pem keypair.pem ec2-user@public-IP-address:/home/ec2-user(path to download)
  • After that change permissions to that keypair file because security purpose only giving read permissions. By using following command
sudo chmod 400 <keypair.pem>

chmod

  • Now execute ansible ping command to verify that our slave node connected or not
ansible ping -m all

ansible all

Step 5: Running the Playbook

Now run ansible-playbook command to run your playbook, specifying the playbook file and target hosts. By using following command

ansible-playbook <filename.yml>

ansible playbook

To verify whether python installed or not by using following command. Check in slave node because we installed in slave node.

python --version

python --version

Conclusion

Python stands out as a powerful and adaptable programming language that is widely used in a wide range of domains and industries. Its simplicity, readability, and extensive ecosystem of libraries and structures go with it a excellent choice for a great many applications, from web improvement and data analysis to scientific computing and artificial intelligence.

All through this guide, we’ve featured Python’s vital features and strengths, including its easy to learn syntax, platform independence, and dynamic local area support. We’ve also looked at how Python’s automatic memory management and dynamic typing make it flexible and easy to use.

Python powers everything from small scripts and automation tasks to large-scale software systems and machine learning models, putting it at the forefront of technological model. Its prominence keeps on developing, with a thriving community of developers adding to its turn of events, sharing information, and offering help to newcomers.

Install Python Using Ansible Playbook – FAQ’s

Can I use Ansible to install a specific version of Python?

Yes, you can specify the version in the playbook by adjusting the package name. Use “python3.9” instead of “python” to install Python 3.9.

How do I deal with errors made during playbook execution?

Ansible gives errors dealing with components like ‘failed_when’ and ‘ignore_errors’ to manage errors during playbook execution. These directives can be used to gracefully handle errors.

Is it possible to uninstall Python utilizing Ansible?

Yes, you can write a playbook with tasks to uninstall Python packages utilizing the ‘package’ module with ‘state: absent’.

Could I at any point utilize Ansible to manage Python packages/modules?

Yes, Ansible gives modules like ‘pip’ to install Python packages and ‘python_requirements’ to manage requirements files.

How can I parallel run Ansible playbooks?

Ansible allows you to run tasks in parallel involving the ‘serial’ keyword in your playbook. Set the value to the ideal number of hosts to simultaneously run tasks.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads