Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from UCL-MIRSG/feat/remove-unnecessary-variables
Browse files Browse the repository at this point in the history
Simplify variables used in the role
  • Loading branch information
p-j-smith authored Jan 20, 2023
2 parents 9d35739 + 7f42406 commit 371ae08
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Ansible Role: mirsg.install_python

This role installs Python, pip, and setuptools on Debian and RedHat operating systems. It will also update pip to the latest version.
This role installs Python, pip, and setuptools on Debian and RedHat operating systems. It will also update pip to the latest version or a
user-specified version, and then install user-specified Python packages using pip.

## Requirements

Expand All @@ -24,11 +25,13 @@ If you would like to run Ansible Molecule to test this role, the requirements ar
- python-setuptools
```

`extra_python3_packages`: list of additional Python 3 packages to to be installed by the OS package manager, NOT by pip
`extra_python2_packages`: list of additional Python 2 packages to to be installed by the OS package manager, NOT by pip
The packages listed in the above variables will be installed by the OS package manager, NOT by pip.

`python3_pip_packages`: list of Python 3 packages to be installed by pip
`python2_pip_packages`: list of Python 2 packages to be installed by pip
`install_pip_version`: version of pip to update to, defaults to `latest`.

`python3_pip_packages`: list of Python 3 packages to be installed by pip, default to `[]`.

`python2_pip_packages`: list of Python 2 packages to be installed by pip, defaults to `[]`.

## Dependencies

Expand All @@ -41,6 +44,16 @@ There are no Ansible-Galaxy dependencies for this role.
This role uses the Ansible facts `ansible_os_family` and `ansible_distribution_major_version` to determine whether Python 2 or Python 3
should be installed on your managed host. As such, you will need to gather these facts when using this role:

```yaml
- name: Install Python 3
hosts: all
gather_facts: true
roles:
- mirsg.install_python
```

This will gather all facts before installing Python. If you would like to install only the necessary facts (`ansible_os_family` and `ansible_distribution_major_version`) to run the role and install Python, you can use `gather_subset`:

```yaml
- name: Install Python 3
hosts: all
Expand All @@ -54,8 +67,6 @@ should be installed on your managed host. As such, you will need to gather these
- mirsg.install_python
```

This will gather only the necessary facts (`ansible_os_family` and `ansible_distribution_major_version`) to run the role and install Python.

## License

[BSD 3-Clause License](https://github.com/UCL-MIRSG/ansible-role-install-python/blob/main/LICENSE).
Expand Down
4 changes: 1 addition & 3 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
install_python_version: 3
install_pip_version: "latest"

install_python3_packages:
- python3
Expand All @@ -11,8 +12,5 @@ install_python2_packages:
- python-pip
- python-setuptools

extra_python2_packages: [] # to be installed by the OS package manager, NOT by pip
extra_python3_packages: [] # to be installed by the OS package manager, NOT by pip

python2_pip_packages: [] # to be installed using pip
python3_pip_packages: [] # to be installed using pip3
20 changes: 15 additions & 5 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
---
# tasks file for ansible-role-install-python3
- name: Install Python for the current OS family
- name: Prepare to install Python for the current OS family
ansible.builtin.include_tasks: "{{ ansible_os_family }}.yml"

- name: Install Python 2 and associated packages if necessary
ansible.builtin.package:
name: "{{ item.name | default(item) }}"
update_cache: true
state: present
loop: "{{ install_python2_packages + extra_python2_packages }}"
loop: "{{ install_python2_packages }}"
when: install_python_version == 2

- name: Install Python 3 and associated packages if necessary
ansible.builtin.package:
name: "{{ item.name | default(item) }}"
update_cache: true
state: present
loop: "{{ install_python3_packages + extra_python3_packages }}"
loop: "{{ install_python3_packages }}"
when: install_python_version == 3

- name: Update pip to the latest version
ansible.builtin.pip:
name:
- pip
# executable: "{{ 'pip3' if install_python_version == 3 else 'pip' }}"
extra_args: --upgrade
version: "{{ install_pip_version }}"
executable: "{{ 'pip3' if install_python_version == 3 else 'pip' }}"

- name: Install Python 2 pip packages
ansible.builtin.pip:
name: "{{ python2_pip_packages }}"
when: install_python_version == 2

- name: Install Python 3 pip packages
ansible.builtin.pip:
name: "{{ python3_pip_packages }}"
when: install_python_version == 3

0 comments on commit 371ae08

Please sign in to comment.