Skip to content

Commit

Permalink
Merge pull request #25 from Appsilon/feat/jupyter-installation
Browse files Browse the repository at this point in the history
[WIP] feat: add Jupyter installation
  • Loading branch information
koralowiec authored Feb 12, 2024
2 parents f1596be + 416a6ed commit 02a3812
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 23 deletions.
1 change: 1 addition & 0 deletions .ansible-lint
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ exclude_paths:
- .cache/
- molecule/default
- molecule/aws-ec2
- molecule/jupyter-system
offline: false
use_default_rules: true
parseable: true
Expand Down
8 changes: 7 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,16 @@ jobs:
include:
- distro: ubuntu2004
ansible-version: '>=2.11.5'
scenario: 'default'
- distro: ubuntu2204
ansible-version: '>=2.11.5'
scenario: 'default'
- distro: ubuntu2204
ansible-version: '>=2.11.5'
scenario: 'jupyter-system'
- distro: rockylinux8
ansible-version: '>=2.11.5'
scenario: 'default'

steps:
- name: Check out the codebase
Expand All @@ -65,7 +71,7 @@ jobs:

- name: Run Molecule tests
run: |
molecule test
molecule test -s "${{ matrix.scenario }}"
env:
ANSIBLE_FORCE_COLOR: '1'
ANSIBLE_VERBOSITY: '2'
Expand Down
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
# Ansible Python Install

Install Python on Ubuntu and RedHat based systems with Posit's pre-compiled binaries ([docs](https://docs.posit.co/resources/install-python/))
Install Python (with Jupyter) on Ubuntu and RedHat based systems with Posit's pre-compiled binaries ([docs](https://docs.posit.co/resources/install-python/))

[![CI](https://github.com/Appsilon/ansible-python-install/workflows/CI/badge.svg)](https://github.com/Appsilon/ansible-python-install/actions/workflows/ci.yml)
[![Ansible Galaxy](https://img.shields.io/badge/ansible--galaxy-appsilon.python_install-blue.svg)](https://galaxy.ansible.com/appsilon/python_install)

## Requirements

None.
If you want to install Workbench's plugins, then the Python used for Jupyter installation needs to be >= 3.7

## Role Variables

| Variable | Required | Default | Choices | Comments |
|-----------------|----------|------------------|-----------------------------------|---------------------------------------|
| python_versions | yes | [3.10.6, 3.9.13] | Array with Python versions >= 3.7 | Version have to be specified as 3.x.y |
| python_jupyter_kernel | no | true | Boolean: true, false | Makes Python available as a Jupyter Kernel |
| Variable | Default | Choices | Comments |
|-----------------|------------------|-----------------------------------|---------------------------------------|
| python_versions | [3.10.6, 3.9.13] | Array with Python versions >= 3.7 | Version have to be specified as 3.x.y |
| python_jupyter_kernel | true | Boolean: true, false | Makes Python available as a Jupyter Kernel |
| python_jupyter_install | true | Boolean: true, false | Whether to install Jupyter |
| python_jupyter_install_method | virtualenv | String: virtualenv, system | Method of Jupyter installation |
| python_jupyter_python_version | `"{{ python_versions[0] }}"` | One of the versions passed in python_versions | Used only for virtualenv method |
| python_jupyter_workbench | true | Boolean: true, false | Whether to install Workbench's plugins for Jupyter |

## Dependencies

Expand Down
14 changes: 14 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,17 @@ python_versions:
- 3.10.6
- 3.9.13
python_jupyter_kernel: true
python_jupyter_install: true
# Possible values: system, virtualenv
python_jupyter_install_method: virtualenv
# Python version to use for Jupyter installation
# when using virtualenv method
python_jupyter_python_version: "{{ python_versions[0] }}"
python_jupyter_virtualenv_path: /opt/jupyter
python_jupyter_packages:
- jupyterlab
- notebook>=6.0.0,<7.0.0
python_jupyter_workbench: true
python_jupyter_workbench_packages:
- rsp_jupyter
- workbench_jupyterlab==1.1.315
11 changes: 11 additions & 0 deletions molecule/jupyter-system/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
- name: Converge
hosts: all
become: true
pre_tasks:
- name: Include main vars
include_vars: "{{ playbook_dir }}/../../tests/vars/main.yml"
- name: Include jupyter-system vars
include_vars: "{{ playbook_dir }}/../../tests/vars/jupyter-system.yml"
roles:
- "{{ playbook_dir }}/../../"
21 changes: 21 additions & 0 deletions molecule/jupyter-system/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: instance-jupyter-system
image: "geerlingguy/docker-${MOLECULE_DISTRO:-ubuntu2004}-ansible:latest"
command: ${MOLECULE_DOCKER_COMMAND:-""}
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
privileged: true
cgroupns_mode: host
pre_build_image: true
platform: linux/amd64
provisioner:
name: ansible
playbooks:
prepare: prepare.yml
converge: converge.yml
verify: verify.yml
5 changes: 5 additions & 0 deletions molecule/jupyter-system/prepare.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- name: Prepare
hosts: all
become: true
tasks: []
10 changes: 10 additions & 0 deletions molecule/jupyter-system/verify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- name: Verify
hosts: all
become: true
pre_tasks:
- name: Include vars
ansible.builtin.include_vars: "{{ playbook_dir }}/../../tests/vars/main.yml"
tasks:
- name: Include tasks
ansible.builtin.include_tasks: "{{ playbook_dir }}/../../tests/tasks/post.yml"
8 changes: 4 additions & 4 deletions tasks/debian/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
- name: Install additional python versions | Install system dependencies
- name: Install Python | Install system dependencies
ansible.builtin.apt:
pkg: "{{ python_system_dependecies }}"
state: "{{ apt_install_state | default('latest') }}"
Expand All @@ -8,15 +8,15 @@
tags:
- python-install-system-dependecies

- name: Install additional python versions | Set release version
- name: Install Python | Set release version
ansible.builtin.set_fact:
ubuntu_release: "{{ ansible_facts['distribution_version'] | replace('.', '') }}"
when: ansible_facts['distribution'] == "Ubuntu"
tags:
- python-install-set-facts
- python-install-setup-versions

- name: Install additional python versions | Download deb package
- name: Install Python | Download deb package
ansible.builtin.get_url:
url: "https://cdn.rstudio.com/python/ubuntu-{{ ubuntu_release }}/pkgs/python-{{ item }}_1_amd64.deb"
dest: "/tmp/Python-{{ item }}.deb"
Expand All @@ -26,7 +26,7 @@
- python-install-download-archives
- python-install-setup-versions

- name: Install additional python versions | Install deb package
- name: Install Python | Install deb package
ansible.builtin.apt:
deb: "/tmp/Python-{{ item }}.deb"
loop: "{{ python_versions }}"
Expand Down
39 changes: 39 additions & 0 deletions tasks/install-jupyter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
- name: Install Python | Combine Jupyter packages
ansible.builtin.set_fact:
python_jupyter_packages: "{{ python_jupyter_packages + python_jupyter_workbench_packages }}"
when: python_jupyter_workbench

- name: Install Python | Install Jupyter packages (virtualenv method)
ansible.builtin.pip:
name: "{{ python_jupyter_packages }}"
virtualenv_command: "/opt/python/{{ python_jupyter_python_version }}/bin/python -m venv"
virtualenv: "{{ python_jupyter_virtualenv_path }}"
when: python_jupyter_install_method == "virtualenv"

- name: Install Python | Install Jupyter packages (system method)
ansible.builtin.pip:
name: "{{ python_jupyter_packages }}"
executable: pip3
when: python_jupyter_install_method == "system"

- name: Install Python | Set path to jupyter-nbextension
ansible.builtin.set_fact:
jupyter_nbextension_path: "{{ (python_jupyter_install_method == 'virtualenv') | ternary(python_jupyter_virtualenv_path + '/bin/', '') }}jupyter-nbextension"
jupyter_etc_nbconfig: "{{ (python_jupyter_install_method == 'virtualenv') | ternary('/opt/jupyter', '/usr') }}/etc/jupyter/nbconfig"

- name: Install Python | Install Workbench's Jupyter plugin
ansible.builtin.command: |
{{ jupyter_nbextension_path }} install --sys-prefix --py rsp_jupyter
register: command_result
# jupyter-nbextension returns the needed information in stderr
changed_when: "'Copying:' in command_result.stderr"
when: python_jupyter_workbench

- name: Install Python | Enable Workbench's Jupyter plugin
ansible.builtin.shell: |
grep 'rsp_jupyter/index' {{ jupyter_etc_nbconfig }}/notebook.json || echo "File doesn't exist yet"
{{ jupyter_nbextension_path }} enable --sys-prefix --py rsp_jupyter
register: command_result
changed_when: "'\"rsp_jupyter/index\": true' not in command_result.stdout"
when: python_jupyter_workbench
19 changes: 11 additions & 8 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# tasks file
---
- name: Include variables
- name: Install Python | Include variables
ansible.builtin.include_vars: "{{ item }}"
with_first_found:
- "_{{ ansible_distribution_release }}.yml"
Expand All @@ -10,18 +10,18 @@
tags:
- configuration

- name: Create download directory
- name: Install Python | Create download directory
ansible.builtin.file:
path: "{{ downloads_path }}"
state: directory
owner: root
group: root
mode: 0755

- name: Include tasks
- name: Install Python | Include installation tasks
ansible.builtin.include_tasks: '{{ ansible_os_family | lower }}/main.yml'

- name: Install python | Upgrade tools
- name: Install Python | Upgrade tools
ansible.builtin.command: >
/opt/python/{{ item }}/bin/pip install --upgrade \
pip setuptools wheel
Expand All @@ -31,24 +31,24 @@
tags:
- python-install-upgrade-tools

- name: Install python | Make available as a Jupyter Kernel
- name: Install Python | Make available as a Jupyter Kernel
when: python_jupyter_kernel
tags:
- python-install-jupyter-kernel
block:
- name: Install python | Install ipykernel
- name: Install Python | Install ipykernel
ansible.builtin.pip:
executable: "/opt/python/{{ item }}/bin/pip"
name: ipykernel
loop: "{{ python_versions }}"

- name: Install python | Check if kernel.json exists
- name: Install Python | Check if kernel.json exists
ansible.builtin.stat:
path: "/usr/local/share/jupyter/kernels/py{{ item }}/kernel.json"
loop: "{{ python_versions }}"
register: kernel_json

- name: Install python | Make available as a Jupyter Kernel
- name: Install Python | Run ipykernel install
ansible.builtin.command: >
/opt/python/{{ item }}/bin/python -m ipykernel install \
--name py{{ item }} --display-name "Python {{ item }}"
Expand All @@ -57,3 +57,6 @@
index_var: index
register: command_result
changed_when: kernel_json.results[index].stat.exists == false

- name: Install Python | Include Jupyter installation tasks
ansible.builtin.include_tasks: './install-jupyter.yml'
7 changes: 3 additions & 4 deletions tasks/redhat/main.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
---

- name: Install python | Install dependecies
- name: Install Python | Install dependecies
ansible.builtin.yum:
name: "{{ system_dependecies }}"

- name: Install python | Download rpm package
- name: Install Python | Download rpm package
ansible.builtin.get_url:
url: "{{ python_download_url }}/python-{{ item }}-1-1.x86_64.rpm"
dest: "{{ downloads_path }}/python-{{ item }}-1-1.x86_64.rpm"
Expand All @@ -14,7 +13,7 @@
- python-install-download-archives
- python-install-setup-versions

- name: Install python | Install rpm package
- name: Install Python | Install rpm package
ansible.builtin.yum:
name: "{{ downloads_path }}/python-{{ item }}-1-1.x86_64.rpm"
disable_gpg_check: true
Expand Down
3 changes: 3 additions & 0 deletions tests/vars/jupyter-system.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# vars file
---
python_jupyter_install_method: system

0 comments on commit 02a3812

Please sign in to comment.