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 #4 from UCL-MIRSG/simplify-role
Browse files Browse the repository at this point in the history
Simplify role
  • Loading branch information
p-j-smith authored Feb 9, 2023
2 parents ff9c5d9 + 3eb498e commit da6caca
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 84 deletions.
49 changes: 13 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,37 @@ If you would like to run Ansible Molecule to test this role, the requirements ar

## Role Variables

`install_python_3_packages`: list of packages to be installed when Python 3 is the default interpreter. This defaults to:
`install_python` is a dictionary that contains the following varialbes:

`version`: the version of Python to install. This defaults to `"3"`.

`pip_version`: the version of pip to update to. This defaults to `"21.3.1"`.

`pip_executable`: path to the pip executalbe to use for installing packages. This defaults to `"pip3"`

`system_packages`: list of system packages to be installed along with Python. This defaults to:

```yaml
- python3
- python3-pip
- python3-setuptools
```
`install_python2_packages`: list of packages to be installed when Python 2 is the default interpreter. This defaults to:

```yaml
- python
- python-pip
- python-setuptools
```

The packages listed in the above variables will be installed by the OS package manager, NOT by pip.

`install_pip_version`: version of pip to update to, defaults to `latest`.
The packages listed in `install_python.system_packages` will be installed by the OS package manager, NOT by pip.

`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 `[]`.
`pip_packages`: list of Python packages to be installed by pip. This defaults to `[]`.

## Dependencies

There are no Ansible-Galaxy dependencies for this role.

## Example Playbook

### Basic usage

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`:
This role will install Python on a managed host. To used this role, add it to the list of roles in a play:

```yaml
- name: Install Python 3
- name: Install Python
hosts: all
gather_facts: true
gather_subset:
- "os_family"
- "distribution_major_version"
- "!min"
- "!all"
roles:
- mirsg.install_python
```
Expand Down
24 changes: 9 additions & 15 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
---
install_python_version: 3
install_pip_version: "latest"

install_python3_packages:
- python3
- python3-pip
- python3-setuptools

install_python2_packages:
- python
- python-pip
- python-setuptools

python2_pip_packages: [] # to be installed using pip
python3_pip_packages: [] # to be installed using pip3
install_python:
version: "3"
pip_version: "21.3.1"
pip_executable: "pip3"
system_packages:
- python3
- python3-pip
- python3-setuptools
pip_packages: []
12 changes: 6 additions & 6 deletions molecule/default/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
- "!min"
- "!all"
pre_tasks:
- name: Set Python install version to 2 for older OSes
- name: Check the major Python version for the OS
ansible.builtin.include_role:
name: "mirsg.install_python"
tasks_from: "check_default_version"
- name: Set install_python variable based on the version to be installed
ansible.builtin.set_fact:
install_python_version: 2
when: >
(ansible_os_family == 'RedHat') and (ansible_distribution_major_version | int < 8) or
(ansible_distribution == 'Debian') and (ansible_distribution_major_version | int < 10) or
(ansible_distribution == 'Ubuntu') and (ansible_distribution_major_version | int < 18)
install_python: "{{ install_python2 if default_python_version is version('2') else install_python3 }}"
tasks:
- name: Install Python, pip, and setuptools
ansible.builtin.include_role:
Expand Down
7 changes: 2 additions & 5 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ provisioner:
defaults:
callbacks_enabled: profile_tasks, timer, yaml
inventory:
group_vars:
all:
additional_networks: []
default_networks:
- "0.0.0.0/0"
links:
group_vars: ../resources/inventory/group_vars/
env:
ANSIBLE_VERBOSITY: "1"

Expand Down
24 changes: 24 additions & 0 deletions molecule/resources/inventory/group_vars/all/vars
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
install_python3:
version: "3"
pip_version: "21.3.1"
pip_executable: "pip3"
system_packages:
- python3
- python3-pip
- python3-setuptools
pip_packages:
- cryptography

install_python2:
version: "2"
pip_version: "20.3.4"
pip_executable: "pip"
system_packages:
- python
- python-pip
- python-setuptools
pip_packages:
- cryptography

install_python: "{{ install_python_3 }}" # default to Python 3
16 changes: 16 additions & 0 deletions tasks/check_default_version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
- name: Check if Python 2 is the default version for the OS
ansible.builtin.set_fact:
default_python_version: "2"
when: >
(ansible_os_family == 'RedHat') and (ansible_distribution_major_version | int < 8) or
(ansible_distribution == 'Debian') and (ansible_distribution_major_version | int < 10) or
(ansible_distribution == 'Ubuntu') and (ansible_distribution_major_version | int < 18)
- name: Check if Python 3 is the default version for the OS
ansible.builtin.set_fact:
default_python_version: "3"
when: >
(ansible_os_family == 'RedHat') and (ansible_distribution_major_version | int >= 8) or
(ansible_distribution == 'Debian') and (ansible_distribution_major_version | int >= 10) or
(ansible_distribution == 'Ubuntu') and (ansible_distribution_major_version | int >= 18)
29 changes: 7 additions & 22 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,22 @@
---
# tasks file for ansible-role-install-python3
# tasks file for ansible-role-install-python
- 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
- name: Install Python and associated system packages
ansible.builtin.package:
name: "{{ item.name | default(item) }}"
update_cache: true
state: present
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 }}"
when: install_python_version == 3
loop: "{{ install_python.system_packages }}"

- name: Update pip
ansible.builtin.pip:
name:
- pip
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
version: "{{ install_python.pip_version }}"
executable: "{{ install_python.pip_executable }}"

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

0 comments on commit da6caca

Please sign in to comment.