how to implement '-m pip' in ansible? - pip

I am trying to upgrade pip using ansible on RockyLinux 8.5, you can refer below code snippet which I used in my yml file.
- name: Upgrade pip
pip:
caution: could change to /bin/pip3 without "-"
executable: "/bin/pip-3"
umask: "0022"
name: "pip"
state: "forcereinstall"
environment:
http_proxy: "{{ proxy }}"
https_proxy: "{{ proxy }}"
become: true
while running it's showing an error and suggests to implement Python with '-m pip' instead of running pip
What exact changes require in my code snippet ?

Related

Unable to remove docker-py using ansible pip module

Ansible version: 2.9
Python version being used: 3.6+
OS: ubuntu 18
Trying to remove docker-py using pip.
Code snippets I have tried...
post_tasks:
- pip:
name: docker-py
state: absent
- name: remove docker-py sdk
pip:
name: docker-py
state: absent
Document referred: https://docs.ansible.com/ansible/2.9/modules/docker_container_module.html
package never gets uninstalled.
Direct pip command from shell works
pip uninstall docker-py
- name: remove docker-py sdk
become: no
pip:
name: docker-py
state: absent
I had become: yes for the whole ansible-playbook, i did a override for this task, so may be we should avoid become: yes for pip packages.

Ansible ansible.builtin.expect | requirement: pexpect

Currently, I am trying to use the ansible.builtin.expect.
Here is my usecase:
- name: Set password for built-in user
expect: command: '/usr/share/elasticsearch/bin/elasticsearch-keystore add "bootstrap.password" -f'
responses: Enter value for bootstrap.password: 'test'
To use ansible.builtin.expect I have to install:
python >= 2.6
pexpect >= 3.3
I have Python 2.7.5 installed, but if i want to install pexpect, it only installs version 2.3.
To install pexpect i use:
- name: Install pexpect module
yum:
name: pexpect
state: latest
Does anyone know, how I am able to install pexpect version 3.3?
Why not installing directly the pip version matching your requirement:
- name: Install pexpect throuhg pip
become: true
pip:
name: "pexpect>=3.3"
state: present
something like this maybe:
- name: 'install pexpect'
become: true
become_user: 'root'
yum:
name: '{{ item }}'
state: present
enablerepo: 'standard'
with_items:
- 'pexpect'

installing package using loop in ansible

I need to use with_items loop to install apache2, sqlite3, and git in Ansible. I'm trying to use the below code but it seems like nothing is happening.
---
- hosts: all
sudo: yes
name: install apache2, sqlite3, git on remote server
tasks:
- name: Install list of packages
action: apt pkg={{item}} state=installed
with_items:
- apache2
- sqlite3
- git
you have to place the variable item inside the double quotes...
Try this code it'll work:
---
- name: install apache2, sqlite3, git on remote servers
hosts: all
become: true
tasks:
- name: Install packages
package:
name: "{{item}}"
state: present
loop:
- apache2
- sqlite3
- git
Try
---
- name: install apache2, sqlite3, git on remote servers
hosts: all
sudo: true
tasks:
- name: Install packages
package:
name: {{ item }}
state: present
loop:
- apache2
- sqlite3
- git
See package – Generic OS package manager
"This module actually calls the pertinent package modules for each system (apt, yum, etc)."
See apt – Manages apt-packages if you need apt specific attributes.

Installing locustio 0.14 using ansible playbook

I am new to ansible and writing a playbook to install locustio with python3 on a Ubuntu 18.04. I don't know how to install locustio with pip3 in playbook. If use pip package, then it gives error to use older version of locust. ( use a pinned old locust version (pip/pip3 install locustio==0.13.5) . I would like to know how to install locust with pip3 or locust 0.13.5 version with pip?
- hosts: all
tasks:
- name: Create folder to keep the files
file:
path: /opt/locust
state: directory
mode: '0755'
become: yes
- name: Python installation
apt:
name: ['python3']
state: present
become: yes
- name: Pip installation
apt:
name: ['python3-pip']
state: present
become: yes
- name: pip install
apt:
name: ['python-pip']
state: present
become: yes
- name: Locust Installation
pip:
name: ['locustio']
state: present
become: yes
I haven't used Ansible, but looking at the docs there seems to be a executable parameter that you could set to pip3.

How ansible find python modules on target and is it possible to use virtualenv to install modules dependencies?

I need to create some snake-oil certificates on some playbook and I'm using openssl_privatekey, openssl_csr and openssl_certificate modules for this. The problem is that this module depens on PyOpenSSL and that CentOS versions of PyOpenSSL are outdated.
To workaround this I install PyOpenSSL from pip, which works, but may overwrite an already existing PyOpenSSL module if that was installed with yum.
On my playbook I'm doing this:
- name: 'Install PyOpenSSL'
pip:
name: PyOpenSSL
state: present
version: '>= 0.15'
Now, is there a way to install this in a virtual environment, so that ansible is aware of it? If so I can simply remove the virtual environment in the play's end.
You can create a virtualenv with the pip module. You can probably make Ansible aware of that virtualenv by setting the ansible_python_interpreter fact to point at the python binary in the virtualenv, so something like:
---
- hosts: localhost
gather_facts: false
tasks:
- name: install modules
pip:
state: present
name: "{{ modules }}"
virtualenv: /tmp/venv
vars:
modules:
- pyopenssl
- set_fact:
ansible_python_interpreter: /tmp/venv/bin/python
Caveat: I haven't tested this thoroughly. It didn't break anything.

Resources