<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Zackery on Medium]]></title>
        <description><![CDATA[Stories by Zackery on Medium]]></description>
        <link>https://medium.com/@zackeryb?source=rss-2b902a3c2b0f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*IkQKta0xgws9-yuC</url>
            <title>Stories by Zackery on Medium</title>
            <link>https://medium.com/@zackeryb?source=rss-2b902a3c2b0f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 20:28:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@zackeryb/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Creating a Simple Jupyter Lab Server using Ansible]]></title>
            <link>https://medium.com/@zackeryb/creating-a-simple-jupyter-lab-server-using-ansible-c0f9f8be9a5b?source=rss-2b902a3c2b0f------2</link>
            <guid isPermaLink="false">https://medium.com/p/c0f9f8be9a5b</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[jupyter-notebook]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[ansible]]></category>
            <category><![CDATA[linux]]></category>
            <dc:creator><![CDATA[Zackery]]></dc:creator>
            <pubDate>Thu, 19 Oct 2023 15:54:41 GMT</pubDate>
            <atom:updated>2024-10-15T20:12:17.571Z</atom:updated>
            <content:encoded><![CDATA[<p>JupyterLab is a powerful tool for data scientists and researchers. It provides a user-friendly web interface in which to edit, create, and test data pipelines and workflows. In my opinion, the most powerful aspect of Jupyter is being able to work on the code/project remotely while still being able to utilize computational resources of clusters or high-perfomance computers.</p><p>In this article, I will present a method — using the IT automation tool Ansible — to create and launch a jupyterlab instance on a remote machine and access the server on either a laptop or another computer through a web browser.</p><h3>Getting started</h3><p>To install Ansible, we first need python installed on our machine. In this example, I am using Ubuntu 20.04.6 LTS on both the client machine (My laptop) and the server. To install python, we need to run the following commands in the terminal — skip this part if you already have a python installation you want to use.</p><pre>sudo add-apt-repository ppa:deadsnakes/ppa<br>sudo apt update<br>sudo apt install python3.8 python3.8-venv</pre><p>The first command adds the python repository to the machine, and the final command installs both python3.8 and the venv module.</p><p>Once installed, I typically create a virtual environment for ansible.</p><pre>python3.8 -m venv ansible-venv<br>source ansible-venv/bin/activate<br>python -m pip install --upgrade pip<br>pip install ansible</pre><p>Before creating the Ansible playbook, it may be useful to create a public key on the server machine. This key allows a user to ssh into the server without providing a password every time. Below is a simple way to create a private/public key pair for user zab1 on the made up server 192.168.1.1.</p><pre>ssh-keygen -t rsa<br>scp ~/.ssh/id_rsa.pub zab1@192.168.1.1:~/<br>ssh zab1@192.168.1.1<br>mkdir ~/.ssh &amp; touch authorized_keys<br>cat id_rsa.pub &gt; authorized_keys</pre><h3>Creating the Ansible Playbook</h3><p>Ansible is a powerful IT automation tool that allows a user to run scripts and modules on remote machines via ssh. Typically, this is done using the ansible-playbook and passing a correspond YAML file that contains the tasks, variables, etc that will be used. To get started, we will create a yaml file called playbook.yml that will contain 3 parts — variables, handlers, and tasks. We first start by specifying the remote machine that ansible will connect to</p><pre>---<br>- name: launching a simple jupyterlab instance<br>  hosts: 192.168.1.1<br>  remote_user: zab1</pre><p>This is the start of the playbook. Here, host is the IP of the server we want to use to run JupyterLab, and my username on that machine is defined in remote_user. Next, we define the relevant variables and handlers that will be used in the playbook.</p><pre>  vars:<br>    - VENV: &quot;python-jupyter-venv&quot;<br>    - PORT: 8888<br>    - IP:   0.0.0.0<br>    - RESTART: true<br>    - SHUTDOWN: false<br><br>  handlers:<br>    - name: shutdown server<br>      shell: &quot;kill {{ PID.stdout }}&quot;<br>  <br>    - name: start server<br>      shell: &quot;~/{{ VENV }}/bin/jupyter lab --no-browser --port={{ PORT }} --ip={{ IP }} &gt; jupyter.log 2&gt;&amp;1 &amp;&quot;<br>      async: 2</pre><p>Here, the variables are the name of the python environment and the port that JupyterLab will use. The IP of 0.0.0.0 allows other machines to access the notebook (i.e. my laptop). RESTART and SHUTDOWN are just boolean variables to force restart or shutdown the server.</p><p>Handlers are routines that run at the end of the playbook if certain tasks return a [changed] result. Here, we have shutdown server and start server as handlers to run at the end of the playbook depending on the values of RESTART and SHUTDOWN server. Note that the order matters here, shutdown server needs to be before start server because they will run in that order if both are called.</p><h3>Playbook Tasks</h3><p>A task in Ansible is a routine that runs on the remote server. They are run sequentially, and will return [ok] or [changed] depending on if the task changes the server or not. A simple task starts with a name and then a module to run such as the apt module to install packages. The first set of tasks we need are installing python and creating the virtual environment.</p><pre>- tasks:<br>    - name: adding python repos<br>      become: true<br>      apt_repository:<br>        repo: &quot;ppa:deadsnakes/ppa&quot;<br>        state: present<br><br>    - name: installing python3.8<br>      become: true<br>      apt:<br>        name: &quot;{{ item }}&quot;<br>        state: present<br>      with_items:<br>        - python3.8<br>        - python3.8-venv<br><br>    - name: getting pythonpath<br>      shell: &quot;which python3.8&quot;<br>      register: python_path<br>      changed_when: false<br>    <br>    - name: creating the python virtual environment<br>      shell: &quot;{{ python_version.stdout }} -m venv {{ VENV }}&quot;<br>      args:<br>        creates: &quot;{{ VENV }}&quot;<br><br>    - name: downloading python packages<br>      pip:<br>        executable: &quot;~/{{ VENV }}/bin/pip&quot;<br>        name:<br>          - jupyterlab<br>          - notebook</pre><p>These tasks are Ansible’s way to run the commands on the server that I would normally run through ssh. For example, the following Ansible task</p><pre>- name: installing python3.8<br>      become: true<br>      apt:<br>        name: &quot;{{ item }}&quot;<br>        state: present<br>      with_items:<br>        - python3.8<br>        - python3.8-venv</pre><p>Is equivalent to this command</p><pre>sudo apt install python3.8 python3.8-venv</pre><h3>Launching Jupyter Lab</h3><p>Now that the python environment is installed, we are ready to make the tasks to shutdown, restart, and start the jupyter server. First, we need to be able to get the process ID (PID) of jupyterlab using the following task</p><pre>    - name: Checking server<br>      shell: &quot;lsof -n -i :{{ PORT }} | grep LISTEN | awk &#39;{print $2}&#39;&quot;<br>      register: PID<br>      changed_when: false</pre><p>This task uses the shell module to run a command to get the PID. Here, the PID is stored in the stdout attribute. Next, we write the tasks to shutdown, start, and restart the server.</p><pre>    - name: shutting down server<br>      debug:<br>        msg: &quot;&quot;<br>      changed_when: true<br>      when:<br>        - PID.stdout|length != 0<br>        - SHUTDOWN<br>      notify: shutdown server<br><br>    - name: restarting server<br>      debug:<br>        msg: &quot;&quot;<br>      changed_when: true<br>      when:<br>        - not SHUTDOWN<br>        - RESTART<br>        - PID.stdout|length != 0<br>      notify:<br>        - shutdown server<br>        - start server<br><br>    - name: starting server<br>      debug: <br>        msg: &quot;&quot;<br>      changed_when: true<br>      when:<br>        - PID.stdout|length == 0<br>        - not SHUTDOWN<br>      notify: start server</pre><p>These are empty debug tasks that will return changed if the when conditions are satisfied. This is possible with the “changed_when: true” line. This allows us to call the relevant handlers for starting and shutting down the server. Note that the PID is used to make sure we don’t start a server that is already running, or shutdown a server that doesn’t exist.</p><p>The final step before we run the playbook is to return the token that will allow the user to access the notebook. First, we need to force the handlers to start the server by using the meta module. Then we extract the token using a shell command and print it out using the debug module.</p><pre>    - name: forcing handlers<br>      meta: flush_handlers<br><br>    - name: getting token<br>      shell: &quot;cat jupyter.log | grep token&quot;<br>      register: token<br>    <br>    - name: printing token<br>      debug:<br>        msg: &quot;{{ token.stdout_lines[-1] }}&quot;</pre><p>Once the playbook is run, the user can take the token and past it into the password section of the notebook to access Jupyterlab.</p><h3>Running the playbook</h3><p>Putting this all together, we have a completed Ansible playbook ( full playbook is at the end of the article ). To run the playbook, we need to get into our Ansible virtual environment and run the following commands.</p><pre>source ansible-venv/bin/activate<br>ansible-playbook playbook.yml -K</pre><p>Here, the -K is required to become a sudo user when adding the python repositories and installing python. It will ask you to type in the sudo password. To access the notebook, simply open a web browser and type in the following into the address bar.</p><p>192.168.1.1:8888</p><p>Obviously, you need to put in the IP of the server that you are using, as well as the port you are using defined in the vars section of the playbook.</p><p>You may need to refresh a few times in order to load the notebook. Then, copy the token printed out after running the playbook into the password section and you are ready to use the notebook.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lV5VYb-1NXk2GL_vscrdwg.png" /></figure><p>Here is the full playbook</p><pre>---<br>- name: launching a simple jupyterlab instance<br>  hosts: 192.168.1.1<br>  remote_user: zab1<br>  <br>  vars:<br>    - VENV: &quot;python-jupyter-venv&quot;<br>    - PORT: 8888<br>    - IP:   0.0.0.0<br>    - RESTART: true<br>    - SHUTDOWN: false<br><br>  handlers:<br>    - name: shutdown server<br>      shell: &quot;kill {{ PID.stdout }}&quot;<br>  <br>    - name: start server<br>      shell: &quot;~/{{ VENV }}/bin/jupyter lab --no-browser --port={{ PORT }} --ip={{ IP }} &gt; jupyter.log 2&gt;&amp;1 &amp;&quot;<br>      async: 2<br><br>  tasks:<br>    - name: adding python repos<br>      become: true<br>      apt_repository:<br>        repo: &quot;ppa:deadsnakes/ppa&quot;<br>        state: present<br><br>    - name: installing python3.8<br>      become: true<br>      apt:<br>        name: &quot;{{ item }}&quot;<br>        state: present<br>      with_items:<br>        - python3.8<br>        - python3.8-venv<br><br>    - name: getting pythonpath<br>      shell: &quot;which python3.8&quot;<br>      register: python_path<br>      changed_when: false<br>    <br>    - name: creating the python virtual environment<br>      shell: &quot;{{ python_version.stdout }} -m venv {{ VENV }}&quot;<br>      args:<br>        creates: &quot;{{ VENV }}&quot;<br><br><br>    - name: downloading python packages<br>      pip:<br>        executable: &quot;~/{{ VENV }}/bin/pip&quot;<br>        name:<br>          - jupyterlab<br>          - notebook<br>    <br>    - name: Checking server<br>      shell: &quot;lsof -n -i :{{ PORT }} | grep LISTEN | awk &#39;{print $2}&#39;&quot;<br>      register: PID<br>      changed_when: false<br><br>    - name: shutting down server<br>      debug:<br>        msg: &quot;&quot;<br>      changed_when: true<br>      when:<br>        - PID.stdout|length != 0<br>        - SHUTDOWN<br>      notify: shutdown server<br><br>    - name: restarting server<br>      debug:<br>        msg: &quot;&quot;<br>      changed_when: true<br>      when:<br>        - not SHUTDOWN<br>        - RESTART<br>        - PID.stdout|length != 0<br>      notify:<br>        - shutdown server<br>        - start server<br><br>    - name: starting server<br>      debug: <br>        msg: &quot;&quot;<br>      changed_when: true<br>      when:<br>        - PID.stdout|length == 0<br>        - not SHUTDOWN<br>      notify: start server<br><br>    - name: forcing handlers<br>      meta: flush_handlers<br><br>    - name: getting token<br>      shell: &quot;cat jupyter.log | grep token&quot;<br>      register: token<br>    <br>    - name: printing token<br>      debug:<br>        msg: &quot;{{ token.stdout_lines[-1] }}&quot;</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c0f9f8be9a5b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>