Cloud-init

Introduction

To start, please read first the basics of the Slices basic infrastructure service.

Cloud-init

In Starting your first Virtual or Baremetal Machine, you learned how to create your first basic infrastructure resource, a virtual or baremetal machine, on the Slices research infrastructure.

This tutorial will go deeper into a very cool tool, called cloud-init.

Cloud-init is a piece of software that starts automatically at boot on the virtual and bare metal machines of the basic infrastructure service. At start-up it fetches its configuration from a central server in the basic infrastructure service and installs and executes the needed software and scripts as described in the cloud-init data. As a user you can supply such cloud-init user data at creation time, which helps automating and reproducing your experiments.

A 2nd feature of cloud-init is that you can ask for certain data (e.g. location, hostname, …) in a generic way. In that way, your scripts can know on which machine they are running.

Both these features will be shown in this tutorial.

Basic infrastructure service dashboard

Please access the tutorial at https://basicinfrastructure.slices-ri.eu/. This is a Jupyter notebook environment which offers a terminal and notebooks with the Slices CLI pre-installed and where authentication is automated through the web login.

After logging in, and choosing the project in which you want to create resources, you will see the Launcher page. At the left, you see a folder templates. Double click this, and then double click the notebook BI-02 Cloud-init. The notebook is self explaining as well and it should be straight forward to copy the commands and eexcute them in a unix terminal.

Basic example

A basic example of cloud-init user data (as used in the tutorial) is shown below. You need a local file (where you run the slices cli) with the cloud-init user data, e.g.:

#cloud-config
package_update: false
package_upgrade: false
packages:
  - nginx

write_files:
- content: |
    <html>
    <body>
    <h1>This is my first webpage</h1>
    </body>
    </html>
  path: /var/www/html/index.html
  permissions: '0644'
  owner: root:root

runcmd:
  - sed -i 's/first/second/g' /var/www/html/index.html
  - echo "Cloud-init user data complete"

And then you use the --user-data option for the slices bi create command and specify that file:

 Usage: slices bi create [OPTIONS] RESOURCE

 Request basic infrastructure (BI) resource.

╭─ Arguments ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ *    resource      TEXT  Name of the resource. [required]                                                                                │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Options ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ *  --experiment            TEXT                  Experiment name or ID. [env var: SLICES_EXPERIMENT] [required]                          │
│    --image                 TEXT                  ID of the image. [default: Ubuntu 24.04.1]                                              │
│    --flavor                TEXT                  ID of the flavor [default: default per site]                                            │
│    --duration      -d      DURATION_OR_DATETIME  Lifetime of the resource. Can be extended later on. [default: 3h]                       │
│    --user-data             FILENAME              File with cloudinit user-data.                                                          │
│    --wait                                        Wait till resource is ready after creation.                                             │
│    --public-ipv4                                 Request a public IPv4.                                                                  │
│    --count                 INTEGER               Number of resources to create.                                                          │
│    --ssh-key               SSH-KEY               (Extra) SSH public key to register for login. [env var: SLICES_SSH_KEY]                 │
│    --ssh-key-file          FILENAME              (Extra) SSH public key file to use for login. [env var: SLICES_SSH_KEY_FILE]            │
│    --help                                        Show this message and exit.                                                             │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

More advanced cloud-init examples

In the above example we showed a #cloud-config configuration, which will be usable in a lot of cases. However, at https://cloudinit.readthedocs.io/en/latest/reference/examples.html you will find more complex examples, e.g. using ansible or chef or how to create users with ssh-keys installed, etc.