Provider::Vagrant

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

Note

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

Using the API

From a dictionnary

The following tuto_vagrant.py implements the desired workflow.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from enoslib.infra.enos_vagrant.provider import Enos_vagrant
from enoslib.infra.enos_vagrant.configuration import Configuration

import logging

logging.basicConfig(level=logging.INFO)

provider_conf = {
    "resources": {
        "machines": [{
            "roles": ["control"],
            "flavour": "tiny",
            "number": 1,
        },{
            "roles": ["control", "compute"],
            "flavour": "tiny",
            "number": 1,
        }],
        "networks": [{"roles": ["r1"], "cidr": "172.16.42.0/16"}]
    }
}

# claim the resources
conf = Configuration.from_dictionnary(provider_conf)
provider = Enos_vagrant(conf)
roles, networks = provider.init()
print(roles)
print(networks)

# destroy the boxes
provider.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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from enoslib.infra.enos_vagrant.provider import Enos_vagrant
from enoslib.infra.enos_vagrant.configuration import Configuration

import logging
import os

logging.basicConfig(level=logging.INFO)

conf = (
    Configuration()
    .add_machine(
       roles=["control"],
       flavour="tiny",
       number=1
    )
    .add_machine(
       roles=["control", "compute"],
       flavour="tiny",
       number=1
    )
    .add_network(
       roles=["mynetwork"],
       cidr="192.168.42.0/24"
    )
    .finalize()
)

# claim the resources
provider = Enos_vagrant(conf)
roles, networks = provider.init()
print(roles)
print(networks)

# destroy the boxes
provider.destroy()