Since a system where pip
hasn't been installed reports with
pip --version; echo $?
-bash: pip: command not found
127
you could implement installation and version tests before running the download and installation task.
---
- hosts: test
become: false
gather_facts: false
tasks:
- name: Gather installed pip version, if there is any
shell:
cmd: pip --version | cut -d ' ' -f 2
register: result
failed_when: result.rc != 0 and result.rc != 127
# Since this is a reporting task, it should never change
# as well run and register a result in any case
changed_when: false
check_mode: false
- name: Set default version, if there is no
set_fact:
result:
stdout_lines: "00.0.0"
when: "'command not found' in result.stdout"
- name: Report result
debug:
msg: "{{ result.stdout }}"
check_mode: false
when: result.stdout != "20.3.4"
Resulting into an output of
TASK [Gather installed pip version, if there is any] ***
ok: [test.example.com]
TASK [Report result] ***********************************
ok: [test.example.com] =>
msg: 20.3.4
Further Q&A
Documentation
... just an example add-on with shell operators
- name: Install 'pip' only if it not exist
shell:
cmd: pip --version || /usr/bin/python /tmp/get-pip.py