Vagrant tutorials#

This tutorial will let you get started using EnOSlib and vagrant. This will present you the bare minimum to start some machines and distribute them into the desired roles.

Hint

For a complete schema reference see Vagrant Schema

Installation#

$ pip install enoslib[vagrant]

Note

It’s a good practice to use a virtualenv or a python version manager like pyenv.

Note

The default backend used is libvirt, so you’ll also need to have the appropriate vagrant-libvirt plugin. Nowadays, it is shipped within a container image (alongside the vagrant program). See https://vagrant-libvirt.github.io/vagrant-libvirt/installation.html#docker–podman

Using the API#

From a dictionary#

The following tuto_vagrant.py implements the desired workflow.

 1import logging
 2
 3import enoslib as en
 4
 5en.init_logging(level=logging.INFO)
 6en.check()
 7
 8provider_conf = {
 9    "resources": {
10        "machines": [
11            {
12                "roles": ["control"],
13                "flavour": "tiny",
14                "number": 1,
15            },
16            {
17                "roles": ["control", "compute"],
18                "flavour": "tiny",
19                "number": 1,
20            },
21        ],
22        "networks": [{"roles": ["r1"], "cidr": "172.16.42.0/16"}],
23    }
24}
25
26# claim the resources
27conf = en.VagrantConf.from_dictionary(provider_conf)
28provider = en.Vagrant(conf)
29roles, networks = provider.init()
30print(roles)
31print(networks)
32
33# destroy the boxes
34provider.destroy()
  • The configuration is specified and passed to the provider. Here we want two machines. Each machine will be given some roles (control and/or compute). These two nodes will have one network card configured using the same network whose role is n1.

    Note

    Machine roles and network roles are transparent to the EnOSlib. The semantic is left to the application using it.

  • You can launch the script using :

    $ python tuto_vagrant.py
    
  • Additionally, there are two keys to personalize the vagrant configuration:

    1. The key config_extra enables customized expressions of the config variable in a Vagrant description. For example, in order to add a synchronised folder in the virtual machine the key may be set as follows:

      config_extra: |
        config.vm.synced_folder ".",
                                 "/vagrant",
                                 owner: "vagrant",
                                 group: "vagrant"
      
    2. The key name_prefix changes the default prefix of virtual machines’ names. This key can be set as a global attribute at the root level or it can be set at group level along side the description fo a machine. For example, the following combination is possible:

      backend: libvirt
      box: generic/ubuntu1804
      name_prefix: vm
      
      resources:
        machines:
          - roles: [CloudOne]
            name_prefix: CloudOne
          - roles: [CloudTwo]
            flavour: large
            number: 1
      

      Note

      When name_prefix is set and there is only one machine in the group (default behaviour) any counter is append to the name of the machine.

Programmatic way#

 1import logging
 2
 3import enoslib as en
 4
 5en.init_logging(level=logging.INFO)
 6en.check()
 7
 8conf = (
 9    en.VagrantConf()
10    .add_machine(roles=["control"], flavour="tiny", number=1)
11    .add_machine(roles=["control", "compute"], flavour="tiny", number=1)
12    .add_network(roles=["mynetwork"], cidr="192.168.42.0/24")
13)
14
15# claim the resources
16provider = en.Vagrant(conf)
17roles, networks = provider.init()
18print(roles)
19print(networks)
20
21# destroy the boxes
22provider.destroy()