Laptop As Code

A stateless computer

Summary

  • Laptop As Code?

  • Bash

  • Ansible

  • Laptop As Code with Ansible (& Demo)

⚠️ Not in this presentation

  • OS installation

  • OS Ghosts

  • local/remote VM as desktop

  • IDE in browser

Who am I?

Mehdi Rebiai

Laptop As Code?

code

Once upon a time…​

You have a new laptop

🎉 💻 🥳 🥰

…​ and then…​

  • Some installations…​ 🧰 ⚒️

  • Some customizations…​ 🔧 🦄

  • And sometimes…​ 🥵

…​ and one day 🔥 !

computer on fire

Or lost, stolen, broken, …​

…​ finally

Solution 🪄 ⇒ Documentation about installation

the end

…​ but

  • My personal documents / configurations ?

  • Is the documentation up-to-date ? 🤣

  • Any fresh installation (new incomer/computer) = documentation + 🥵 ?

Another point of view (1/2)

Your laptop deserves the same attention as your production code !

Another point of view (2/2)

evolution

A computer is stateless !

  • (Almost) Everything as Code in SCM

git logo

  • Otherwise ⇒ Cloud storage

onedrive logo google docs

And my passwords 😱 ?

  • Keepass file in SCM

  • Lastpass, 1Password, …​

My new challenge : rebuild my 💻 in 30 minutes !

challenge
OS already installed

Bash

press any key

my-env/run.sh bash logo

install() {
  installCommon
  installSDK
  installGit
  installDocker
  installKubernetes
  ...
  # TODO To be continued
}
installCommon() {
  ...
  sudo apt -y install git graphviz gimp curl git-gui aspell-fr openssh-server filezilla nodejs npm wget pdfshuffler tree gtk-recordmydesktop terminator ethtool jq snapdi tig kakoune ranger httpie ffmpeg keepassxc
  # TODO To be continued
}
...

my-env/run.sh workflow

flowchart my env

A good scripting?

notbad
But
  • Many SCM forks & customization

  • Maintainability ?

New challenge(s)

  • Shareable scripting

  • Easy customization

  • Readable configuration

  • Do you know Ansible?

Ansible

magic

Ansible (1/9)

Ansible automates the management of remote systems and controls their desired state

Ansible (2/9)

ansible basic
  • Ansible on Control node

  • Python on Managed Nodes

  • Inventory YAML or INI

  • SSH connections

  • YAML task description

  • Multi-OS

Ansible (3/9)

Inventory
[webservers]
node1.example.com
node2.example.com

[dbservers]
node3.example.com

Ansible (4/9)

ansible-playbook playbook.yml
---
- name: Update web servers
  hosts: webservers
  remote_user: root
  tasks:
  - name: Ensure apache is at the latest version
    tags: httpd
    ansible.builtin.yum:
      name: httpd
      state: latest
    when: ansible_distribution == 'CentOS'
  - name: Write the apache config file
    tags: httpd
    ansible.builtin.template:
      src: /srv/httpd.j2
      dest: /etc/httpd.conf

- name: Update db servers
  hosts: dbservers
  remote_user: root
  tasks:
  - name: Ensure postgresql is at the latest version
    tags: pg
    ansible.builtin.yum:
      name: postgresql
      state: latest
  - name: Ensure that postgresql is started
    tags: pg
    ansible.builtin.service:
      name: postgresql
      state: started

Ansible (5/9)

  • A Task calls a Module (yum, apt, service…​) ⇒ Built-In index

  • Every Task can contain Tag(s)

  • Every Task can have a When clause (Skipped if false)

  • Some Tasks can be defined in Roles

  • A Playbook can call Tasks and/or Roles

Ansible (6/9)

Ansible Galaxy
The Hub is not mandatory!

Ansible (7/9)

Role
tasks/
  main.yml     #  <-- tasks file can include smaller files if warranted
handlers/      #
  main.yml     #  <-- handlers file
templates/     #  <-- files for use with the template resource
  ntp.conf.j2  #  <-- templates end in .j2
files/         #
  bar.txt      #  <-- files for use with the copy resource
  foo.sh       #  <-- script files for use with the script resource
vars/          #
  main.yml     #  <-- variables associated with this role
defaults/      #
  main.yml     #  <-- default lower priority variables for this role
meta/          #
  main.yml     #  <-- role dependencies

Ansible (8/9)

Laptop As Code with Ansible
  • 127.0.0.1 in Inventory

  • Ansible local connection

  • ansible-pull -U <giturl> …​ =

    • git clone/pull …​

    • ansible-playbook …​

Ansible (9/9)

Laptop As Code with Ansible

flowchart ansible pull

Sources - LearnLinuxTV

Laptop As Code with Ansible

cool

Step by step…​ with a demo 🤞

Step 1 - Add a bootstrap (1/2)

  • SSH & Git configuration

  • Ansible installation

  • Inventory creation, having some environment variables (asking some questions…​)

Step 1 - Add a bootstrap (2/2)

flowchart bootstrap

Step 2 - A Playbook…​ without external Roles

  • Shareable scripting ✅

  • Easy customization ❌

  • Readable configuration ✅

Step 3 - A Playbook…​ with external Roles

  • Shareable scripting ✅

  • Easy customization ✅

  • Readable configuration ✅

What strategy for external Roles 🤔 ? How to install them locally 🤔🤔 ?
How to keep a unique Playbook 🤔🤔🤔 ?

Step 4 - A generic user role, forked by user

  • The Bootstrap asks you your user Role and installs it (with dependencies)

  • The Playbook calls the role by its name, and it will be yours

  • Your user Role calls other Roles (and their Tasks)

User Role Installation locally with dependencies :
ansible-galaxy install git+<gitUrlToUserRole>,<version> --force-with-deps

Wait a minute, I need a concrete example

what

Scenario - 3 users

Alice
alice
backend
developer

Bob
bob
employee?

Carol
carol
full stack
developer

Role Strategy

flowchart role example

🎉 Demo 🥳

🍀 🤞

Lectra example (today)

flowchart lectra

Conclusion

  • Good feedbacks

  • Only Linux today but

    • some Windows users are interested in migrating to Linux

    • Ansible Roles can be improved to manage MacOS

  • Other interesting tools : Nix Packages, NixOS

Thank you !