Skip to content

Commit

Permalink
Add install method logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tersmitten committed Nov 1, 2024
1 parent 5d9f2a3 commit 88f48e8
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 114 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@
[![CI](https://github.com/Oefenweb/ansible-pip/workflows/CI/badge.svg)](https://github.com/Oefenweb/ansible-pip/actions?query=workflow%3ACI)
[![Ansible Galaxy](http://img.shields.io/badge/ansible--galaxy-pip-blue.svg)](https://galaxy.ansible.com/Oefenweb/pip)

Set up (the latest version of) [pip](https://pypi.python.org/pypi/pip), [wheel](https://packaging.python.org/key_projects/#wheel) and [setuptools](https://packaging.python.org/key_projects/#setuptools) in Debian-like systems.
Set up (the latest version of) [pip](https://pypi.python.org/pypi/pip) in Debian-like systems.

#### Requirements

* `python(2|3)` (will be installed)
* `python(2|3)-dev` (will be installed)
* `curl` (will be installed)

when using `pip_install_method: get-pip`

#### Variables

* `pip_python_version_major` [default: `2`]: Python version to install `pip` for.
* `pip_python_version` [default: `pip_python_version`]: Deprecated
* `pip_install_method`: [default: `native`]: The way to install `pip` (e.g. `native` (from Ubuntu/Debian repo) or `get-pip`, `< 24.04` only)

* `pip_python_version_major` [default: `3`]: Python version to install `pip` for

* `pip_install`: [default: `[]`]: Additional packages to install

## Dependencies

Expand Down
8 changes: 7 additions & 1 deletion defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# defaults file
---
pip_python_version_major: 2
pip_install_method: native
# Only available for < 24.04
# pip_install_method: get-pip

pip_python_version_major: 3
pip_python_version: "{{ pip_python_version_major }}"

pip_install: []
113 changes: 113 additions & 0 deletions tasks/install-get-pip.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# tasks file
---
- name: install | get-pip | install dependencies

Check warning on line 3 in tasks/install-get-pip.yml

View workflow job for this annotation

GitHub Actions / Lint

name[casing]

All names should start with an uppercase letter.
ansible.builtin.apt:
name: "{{ pip_install_get_pip_dependencies }}"
state: "{{ apt_install_state | default('latest') }}"
update_cache: true
cache_valid_time: "{{ apt_update_cache_valid_time | default(3600) }}"
tags:
- pip-install-get-pip-dependencies

- name: install | get-pip | create (download) directory

Check warning on line 12 in tasks/install-get-pip.yml

View workflow job for this annotation

GitHub Actions / Lint

name[casing]

All names should start with an uppercase letter.
ansible.builtin.file:
path: "{{ pip_install_get_pip_download_path }}"
state: directory
owner: root
group: root
mode: 0755
tags:
- pip-install-get-pip-directory

- name: install | get-pip | get python version # noqa risky-shell-pipe

Check warning on line 22 in tasks/install-get-pip.yml

View workflow job for this annotation

GitHub Actions / Lint

name[casing]

All names should start with an uppercase letter.
ansible.builtin.shell: >
python{{ pip_python_version_major | string }} --version 2>&1 | awk '{print $2}'
register: _python_version
changed_when: false
tags:
- pip-install-get-pip-get-python-version

- name: install | get-pip | get download url

Check warning on line 30 in tasks/install-get-pip.yml

View workflow job for this annotation

GitHub Actions / Lint

name[casing]

All names should start with an uppercase letter.
tags:
- pip-install-get-pip-get-download-url
block:
- name: install | get-pip | get download url (2.6)

Check warning on line 34 in tasks/install-get-pip.yml

View workflow job for this annotation

GitHub Actions / Lint

name[casing]

All names should start with an uppercase letter.
ansible.builtin.set_fact:
pip_download_url: 'https://bootstrap.pypa.io/pip/2.6/get-pip.py'
when:
- _python_version.stdout is version('2.6', '>=')
- _python_version.stdout is version('2.7', '<')

- name: install | get-pip | get download url (2.7)

Check warning on line 41 in tasks/install-get-pip.yml

View workflow job for this annotation

GitHub Actions / Lint

name[casing]

All names should start with an uppercase letter.
ansible.builtin.set_fact:
pip_download_url: 'https://bootstrap.pypa.io/pip/2.7/get-pip.py'
when:
- _python_version.stdout is version('2.7', '>=')
- _python_version.stdout is version('2.8', '<')

- name: install | get-pip | get download url (3.2)

Check warning on line 48 in tasks/install-get-pip.yml

View workflow job for this annotation

GitHub Actions / Lint

name[casing]

All names should start with an uppercase letter.
ansible.builtin.set_fact:
pip_download_url: 'https://bootstrap.pypa.io/pip/3.2/get-pip.py'
when:
- _python_version.stdout is version('3.2', '>=')
- _python_version.stdout is version('3.3', '<')

- name: install | get-pip | get download url (3.3)

Check warning on line 55 in tasks/install-get-pip.yml

View workflow job for this annotation

GitHub Actions / Lint

name[casing]

All names should start with an uppercase letter.
ansible.builtin.set_fact:
pip_download_url: 'https://bootstrap.pypa.io/pip/3.3/get-pip.py'
when:
- _python_version.stdout is version('3.3', '>=')
- _python_version.stdout is version('3.4', '<')

- name: install | get-pip | get download url (3.4)
ansible.builtin.set_fact:
pip_download_url: 'https://bootstrap.pypa.io/pip/3.4/get-pip.py'
when:
- _python_version.stdout is version('3.4', '>=')
- _python_version.stdout is version('3.5', '<')

- name: install | get-pip | get download url (3.5)
ansible.builtin.set_fact:
pip_download_url: 'https://bootstrap.pypa.io/pip/3.5/get-pip.py'
when:
- _python_version.stdout is version('3.5', '>=')
- _python_version.stdout is version('3.6', '<')

- name: install | get-pip | get download url (3.6)
ansible.builtin.set_fact:
pip_download_url: 'https://bootstrap.pypa.io/pip/3.6/get-pip.py'
when:
- _python_version.stdout is version('3.6', '>=')
- _python_version.stdout is version('3.7', '<')

- name: install | get-pip | get download url (3.7)
ansible.builtin.set_fact:
pip_download_url: 'https://bootstrap.pypa.io/pip/3.7/get-pip.py'
when:
- _python_version.stdout is version('3.7', '>=')
- _python_version.stdout is version('3.8', '<')

- name: install | get-pip | get download url (latest)
ansible.builtin.set_fact:
pip_download_url: 'https://bootstrap.pypa.io/pip/get-pip.py'
when: _python_version.stdout is version('3.8', '>=')

- name: install | get-pip | download (latest)
ansible.builtin.get_url:
url: "{{ pip_download_url }}"
dest: "{{ pip_install_get_pip_download_path }}/{{ pip_download_url | basename }}-{{ pip_python_version_major | string }}"
owner: root
group: root
mode: 0644
force: true
register: _download_latest
tags:
- pip-install-get-pip-download

- name: install | get-pip | install (latest)
ansible.builtin.command: >
python{{ pip_python_version_major | string }} {{ _download_latest.dest }}
when: _download_latest is changed
changed_when: true
tags:
- pip-install-get-pip-install
10 changes: 10 additions & 0 deletions tasks/install-native.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# tasks file
---
- name: install | native | dependencies
ansible.builtin.apt:
name: "{{ pip_install_native_dependencies }}"
state: "{{ apt_install_state | default('latest') }}"
update_cache: true
cache_valid_time: "{{ apt_update_cache_valid_time | default(3600) }}"
tags:
- pip-install-native-dependencies
126 changes: 19 additions & 107 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,125 +1,37 @@
# tasks file
---
- name: install dependencies
ansible.builtin.apt:
name: "{{ pip_dependencies }}"
state: "{{ apt_install_state | default('latest') }}"
update_cache: true
cache_valid_time: "{{ apt_update_cache_valid_time | default(3600) }}"
tags:
- configuration
- pip
- pip-dependencies

- name: create (download) directory
ansible.builtin.file:
path: "{{ pip_download_path }}"
state: directory
owner: root
group: root
mode: 0755
tags:
- configuration
- pip
- pip-directory

- name: get python version # noqa risky-shell-pipe
ansible.builtin.shell: >
python{{ pip_python_version_major | string }} --version 2>&1 | awk '{print $2}'
register: _python_version
changed_when: false
- name: install native
ansible.builtin.import_tasks: install-native.yml
when: pip_install_method == 'native'
tags:
- configuration
- pip
- pip-get-python-version
- pip-install-native

- name: get download url
- name: install pip
ansible.builtin.import_tasks: install-get-pip.yml
when: pip_install_method == 'get-pip'
tags:
- configuration
- pip
- pip-get-download-url
block:
- name: get download url (2.6)
ansible.builtin.set_fact:
pip_download_url: 'https://bootstrap.pypa.io/pip/2.6/get-pip.py'
when:
- _python_version.stdout is version('2.6', '>=')
- _python_version.stdout is version('2.7', '<')
- pip-install-get-pip

- name: get download url (2.7)
ansible.builtin.set_fact:
pip_download_url: 'https://bootstrap.pypa.io/pip/2.7/get-pip.py'
when:
- _python_version.stdout is version('2.7', '>=')
- _python_version.stdout is version('2.8', '<')

- name: get download url (3.2)
ansible.builtin.set_fact:
pip_download_url: 'https://bootstrap.pypa.io/pip/3.2/get-pip.py'
when:
- _python_version.stdout is version('3.2', '>=')
- _python_version.stdout is version('3.3', '<')

- name: get download url (3.3)
ansible.builtin.set_fact:
pip_download_url: 'https://bootstrap.pypa.io/pip/3.3/get-pip.py'
when:
- _python_version.stdout is version('3.3', '>=')
- _python_version.stdout is version('3.4', '<')

- name: get download url (3.4)
ansible.builtin.set_fact:
pip_download_url: 'https://bootstrap.pypa.io/pip/3.4/get-pip.py'
when:
- _python_version.stdout is version('3.4', '>=')
- _python_version.stdout is version('3.5', '<')

- name: get download url (3.5)
ansible.builtin.set_fact:
pip_download_url: 'https://bootstrap.pypa.io/pip/3.5/get-pip.py'
when:
- _python_version.stdout is version('3.5', '>=')
- _python_version.stdout is version('3.6', '<')

- name: get download url (3.6)
ansible.builtin.set_fact:
pip_download_url: 'https://bootstrap.pypa.io/pip/3.6/get-pip.py'
when:
- _python_version.stdout is version('3.6', '>=')
- _python_version.stdout is version('3.7', '<')

- name: get download url (3.7)
ansible.builtin.set_fact:
pip_download_url: 'https://bootstrap.pypa.io/pip/3.7/get-pip.py'
when:
- _python_version.stdout is version('3.7', '>=')
- _python_version.stdout is version('3.8', '<')

- name: get download url (latest)
ansible.builtin.set_fact:
pip_download_url: 'https://bootstrap.pypa.io/pip/get-pip.py'
when: _python_version.stdout is version('3.8', '>=')

- name: download (latest) pip
ansible.builtin.get_url:
url: "{{ pip_download_url }}"
dest: "{{ pip_download_path }}/{{ pip_download_url | basename }}-{{ pip_python_version_major | string }}"
owner: root
group: root
mode: 0644
force: true
register: _download_latest
- name: install additional
ansible.builtin.apt:
name: "{{ pip_install }}"
state: "{{ apt_install_state | default('latest') }}"
tags:
- configuration
- pip
- pip-pip-download
- pip-install
- pip-install-additional

- name: install (latest) pip (setuptools and wheel)
- name: verify
ansible.builtin.command: >
python{{ pip_python_version_major | string }} {{ _download_latest.dest }}
when: _download_latest is changed
changed_when: true
pip --version
changed_when: false
tags:
- configuration
- pip
- pip-pip-install
- pip-install
- pip-install-verify
2 changes: 1 addition & 1 deletion tests/vars/_default.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# vars file
---
pip_python_version_major: 3
pip_install_method: native
3 changes: 3 additions & 0 deletions tests/vars/_noble.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# vars file
---
pip_install_method: get-pip
7 changes: 5 additions & 2 deletions vars/main.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# vars file
---
pip_dependencies:
pip_install_native_dependencies:
- "python{{ pip_python_version_major is version('3', '>=') | ternary('3', '') }}-pip"

pip_install_get_pip_dependencies:
- "python{{ pip_python_version_major is version('3', '>=') | ternary('3', '') }}"
- "python{{ pip_python_version_major is version('3', '>=') | ternary('3', '') }}-dev"
- curl

pip_download_path: /var/lib/ansible/pip/downloads
pip_install_get_pip_download_path: /var/lib/ansible/pip/downloads

0 comments on commit 88f48e8

Please sign in to comment.