Convert Ansible command to pip task - pip

I'd like to convert the following to a Ansible pip task:
- name: install packages and setup profile 3
shell: pip install --user vex
I do not see anything in the Ansible documentation for installing for user.

You can use the pip module with extra_args parameter:
- name: install packages and setup profile 3
pip:
name: vex
state: present
extra_args: --user
Ansible passes the value given in the parameter directly to the pip command.

Related

How to use ansible pip executable `/usr/local/bin/python3 -m pip`?

My playbook
- name: "Install python package 'requests'"
pip:
name: requests
executable: /usr/local/bin/python3 -m pip
become: yes
Which errors with:
TASK [Install python package 'requests'] **************************
fatal: [integration]: FAILED! => changed=false
cmd: '''/usr/local/bin/python3 -m pip'' install requests'
msg: '[Errno 2] No such file or directory'
rc: 2
stderr: ''
stderr_lines: <omitted>
stdout: ''
stdout_lines: <omitted>
I didn't install pip or pip3 in the managed node, that is why executable: pip3 would not work.
As per documentation, the recommended way to achieve this is to use the easy_install module in order to make pip available on the managed node, and then, use the builtin module pip, normally.
Please note that the easy_install module can only install Python libraries. Thus this module is not able to remove libraries. It is generally recommended to use the ansible.builtin.pip module which you can first install using community.general.easy_install.
So, in a two tasks:
- name: Install or update pip
community.general.easy_install:
name: pip
state: latest
become: yes
- name: Install python package 'requests'
ansible.builtin.pip:
name: requests
become: yes

ansible throwing error when using pip command

When I am trying to run pip command through ansible I land up in error.
{"changed": false, "msg": "Unable to find any of pip to use. pip needs to be installed."}
When I debugged on my machine I found pip was installed in latest version. I realized my file uses sudo to run the pip.
So if I do which pip
I get the path of pip but if I do sudo which pip I get nothing.
I don't know how to change my file so instead of sudo it take
- name: "Allow newuser for new super user without SUDO password for using rsync:"
lineinfile:
path: /etc/sudoers
state: present
insertafter: '^%sudo'
line: "{{ user }} ALL=(ALL:ALL) NOPASSWD: /usr/bin/rsync"
- pip:
name: opencv-python
state: forcereinstall
executable: pip
I have no idea how to fix this issue
At the remote host find out which pip is used by the remote user. Use the executable and become the remote user. For example,
- name: Install opencv-python
become_user: admin
become: true
pip:
name: opencv-python
state: forcereinstall
executable: /home/admin/.local/bin/pip
See Becoming an Unprivileged User. Pipelining should solve the problems of becoming an unprivileged user.
shell> grep pipe ansible.cfg
pipelining = true
Notes
Whatever you install by pip this way will be available to this particular remote user only.
Preferably distro packages should be used. For example
shell> apt-cache search python-opencv
python-opencv - Python bindings for the computer vision library
python-opencv-apps - opencv_apps Robot OS package - Python 2 bindings
See The pip module isn't always idempotent #28952 and the Conclusions in particular.

Ansible - pip not found

I am getting this error:
TASK [pip] *********************************************************************
failed: [default] (item=urllib3) =>
{"changed": false, "item": "urllib3",
"msg": "Unable to find any of pip2, pip to use. pip needs to be installed."}
Upon a suggestion I run following command:
ansible default -a "which pip"
I get an error:
default | FAILED | rc=1 >>
non-zero return code
So I guess that means no pip installed. I tried installing pip using:
ansible default -a "easy_install pip"
I get the following error:
default | FAILED | rc=2 >>
[Errno 2] No such file or directory
Any ideas?
UPDATE
In play_local.yaml, I have the following task:
- name: Prepare system
hosts: default
become: yes
gather_facts: false
pre_tasks:
- raw: sudo apt-get -y install python python-setuptools python-pip build-essential libssl-dev libffi-dev python-dev easyinstall pip
- file: path=/etc/sudoers.d/ssh-auth-sock state=touch mode=0440
#- lineinfile: line='Defaults env_keep += "SSH_AUTH_SOCK"' path=/etc/sudoers.d/ssh-auth-sock
- replace:
path: /etc/apt/sources.list
regexp: 'br.'
replace: ''
Shouldn't this task install pip?
Seems like pip is not installed, you can use the following task to install it:
- name: Install pip
apt:
name: python-pip
update_cache: yes
state: present
May be pip is hashed. Meaning pip is installed at path x (may be /usr/local/bin/pip), however, cached at path y (may be /usr/bin/pip). You can confirm that from - ansible default -m shell -a ‘type pip’. To resolve this you’ll need to run - ansible default -m shell -a ‘hash -r’.
BTW, you can also use command module instead of shell.
I've just met the same problem on a brand new CentOS 7. Solved through installing setuptools with yum first and then pip with easy_install as below :
ansible default -b -m yum -a "name=python-setuptools state=present"
ansible default -b -m easy_install -a "name=pip state=present"
For Debian Based systems (RUN THIS ON CLIENT SYSTEMS):
first install package python-is-python3 then add alias for pip echo alias pip=pip3 >> ~/.bashrc
I know my solution would be dumb but It works.
# pip-fix.yml
- name: pip fix
hosts: all
become: true
tasks:
- name: install python-is-python3
apt: name=python-is-python3 update_cache=yes state=present
- name: creating alias
shell: echo alias pip=pip3 >> ~/.bashrc
- name: test and upgrade pip
pip: name=pip state=latest
tags:
- packages
run using ansible-playbook pip-fix.yml
Ansible Not finding / Installing Pip
While not using the author's original details, i think this might help them or others.
I was getting the following error for installing pip:
via Ansible:
FAILED! => {"changed": false, "cmd": "/bin/easy_install --upgrade pip", "failed": true, "msg": "Couldn't find index page for 'pip' (maybe misspelled?)
directly on the host:
Searching for pip
Reading https://pypi.python.org/simple/pip/
Couldn't find index page for 'pip' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading https://pypi.python.org/simple/
No local packages or download links found for pip
error: Could not find suitable distribution for
Requirement.parse('pip')
I attempted to use the above hash -r answer:
(NOTE: help hash... hash -r == forget all remembered locations)
- name: forget easy_install path
shell: hash -r
become: true
however this was not a solve for me.
I found via another post: 'pip install' fails for every package ("Could not find a version that satisfies the requirement") that this was the final fix:
curl https://bootstrap.pypa.io/get-pip.py | python
or
- name: manually install pip
shell: curl https://bootstrap.pypa.io/pip/2.7/get-pip.py | python
become: true
NOTE: i changed the pip version. Also, there is a better way to do a curl in ansible, this is simply an example
I then followed with the easy_install pip latest. to ensure it was up to date.
below fix worked for me
#ln -s /usr/local/bin/pip /usr/bin/pip

Ansible: How to pip install with --upgrade

I want to pip install with --upgrade, using Ansible.
What's the syntax?
- name: install the package, force upgrade
pip:
name: <your package name>
state: latest
Or with:
- name: install the package, force reinstall to the latest version
pip:
name: <your package name>
state: forcereinstall
Eventually found the answer here:
https://groups.google.com/forum/#!topic/ansible-project/a19JEpdXzck
this is the syntax:
- name: install the package, force upgrade
pip:
name: <your package name>
extra_args: --upgrade
If you need upgrade pip for other python version, you can use executable parameter:
- name: install the package, force upgrade
pip:
name: pip
executable: pip3
state: latest

How to install ansible-modules-extras?

I've installed ansible via the ubuntu apt package ansible, I am trying to use the npm module which is an extras module, which is provided only in the ansible-modules-extras Github repository.
How do I install ansible-modules-extras?
Looking at where files were installed as part of the ansible apt package, I would guess I have to merge some of the source codes folders to like /usr/share/ansible
or somewhere under /usr/lib/python2.7/dist-packages/ansible.
I ask this as I get this error from the Ansible output:
msg: Failed to find required executable npm
Ansible extras are included in the Ubuntu ansible apt package.
The target machine must have npm installed, apt package npm, can be installed like so via Ansible:
tasks:
- name: install npm
apt: pkg=npm state=present
Try to install with python-pip, for this first time delete the ansible.
sudo apt-get remove ansible
After install python-pip
sudo apt-get install gcc python-pip python-dev
And install ansible
sudo pip install ansible
It is install the newest version. It should contain the npm mondule.

Resources