Deep dive into Pip module in Ansible

Last updated on Nov 11 2021
Nitin Bajabalkar

Table of Contents

Deep dive into Pip module in Ansible

Ansible Pip

Ansible pip module is used when you need to manage python libraries on the remote servers.

There are two prerequisites if you need to use all the features in the pip module.

  • The pip package should already be installed on the remote server.
  • Virtualenv package should be installed on the remote server already if you need to manage the packages in the python virtual environment.

NOTE: If you get the error “unable to find any of pip2, pip to use. Pip needs to be installed”. The pip module is not available on the remote server during the execution.

Installing a Pip Module

To install a new python library, you need to set the name of the package against the “name” parameter. By default, the “state” parameter is “present”, the module will try to install the library.

If the library is already installed, then nothing will be done. And if a new version of the library exists, it will not be upgraded.

- hosts: all
  tasks:
- name: Installing NumPy python library using pip module
    pip:
name: NumPy

Installing Using a Requirement File

Another way to install the libraries is via the requirements file. If you have any requirements file with all the libraries in the remote servers, give it as input to the “requirements” parameters.

Also, you can use the copy module beforehand to copy the requirements file to every remote server. In the following code, install the requirements file in the location/tmp/req.txt.

- hosts: all
  tasks:
- name: Installing python libraries using requirements file
    pip:
requirements: req.txt
      chdir: /tmp
req.txt
-------
nltk==3.0.0
numpy<0.0scipy>=1.0.0

Installing Multiple Python Libraries

To install the multiple packages, set all the libraries against the “name” parameter, separated by a comma.

- hosts: all
  tasks:
- name: Installing multiple python packages
    pip:
name: NumPy,SciPy

Installing a Particular Version of Pip Library

There is a “version” parameter, which can be used to install only the mentioned version of a library. In the following code, install the version of nltk library.

- hosts: all
  tasks:
- name: Installing a required version of python library
    pip:
name: nltk
      version: '3.0.0'

Reinstall a Python Library

You can reinstall the python library by using the “forcereinstall” value for the “state” parameter.

This will reinstall the latest version of the library. You can use the “version” parameter along with it. The following code will install the version 3.0.0 of the nltk library.

  1. – hosts: all
  1.   tasks:
  1. – name: Reinstalling a python library
  1.     pip:
  1. name: nltk
  1.       version: 3.0.0
  1. state: forcereinstall

Removing a Python Library

You can delete a python library by changing the state to “absent”. In the following code, we will remove the “NumPy” and “SciPy” python libraries from the remote servers.

- hosts: all
  tasks:
- name: Removing Python libraries
    pip:
name: NumPy,SciPy
      state: absent

So, this brings us to the end of blog. This Tecklearn ‘Deep dive into Pip module in Ansible’ blog helps you with commonly asked questions if you are looking out for a job in DevOps. If you wish to learn Ansible and build a career in DevOps domain, then check out our interactive, Configuration Management using Ansible Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/configuration-management-using-ansible/

Configuration Management using Ansible Training

About the Course

Tecklearn has specially designed this Configuration Management using Ansible Training Course to advance your skills for a successful career in this domain. The course will cover different components of Ansible and how they are used in software development operations.  You will get an in-depth knowledge of these concepts and will be able to work on related demos. Upon completion of this online training, you will hold a solid understanding and hands-on experience with Ansible.

Why Should you take Configuration Management using Ansible Training?

  • The average Ansible salary in USA is $136,500 per year or $70 per hour. Entry level positions start at $100,000 per year while most experienced workers make up to $187,500 per year.
  • Wells Fargo, Capital One, Apple, CISCO & many other MNC’s worldwide use Ansible across industries.
  • According to Grand View Research, the DevOps market size is estimated to be worth $12.85 billion by 2025. DevOps professionals are highly paid and in-demand throughout industries including retail, eCommerce, finance, and technology.

What you will Learn in this Course?

Introduction to DevOps

  • What is Software Development
  • Software Development Life Cycle
  • Why DevOps?
  • What is DevOps?
  • DevOps Lifecycle
  • DevOps Tools
  • Benefits of DevOps
  • How DevOps is related to Agile Delivery
  • DevOps Implementation

Configuration Management using Ansible

  • What is Ansible?
  • Ansible vs Puppet
  • Ansible Architecture
  • Ansible Installation
  • Setting up Master Slave using Ansible
  • Configuring Ansible Roles
  • Write Playbooks
  • Applying configuration using Ansible
  • Hands on

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "Deep dive into Pip module in Ansible"

Leave a Message

Your email address will not be published. Required fields are marked *