Mentions légales du service

Skip to content

Introduce play_on context manager

SIMONIN Matthieu requested to merge msimonin/enoslib:custom_actions into master
Introduce play_on context manager

This brings Ansible experience in python directly.

Rationale:

For a small set of tasks you probably don't need to write separated playbooks
and call `run_ansible` on them. Instead you'd like to describe and run the
actions directly from your python code.
It also seems that writing ansible code, and switching from the Ansible world
and Python worl is a showstopper for some users (and that's understandable).

Implementation:

A context manager is introduced. It gives the syntactic sugar needed to the
user to use ansible module directly from python. Behind the scene a playbook is
built and run when the context manager exits.

Example:

The following is a complete example of the feature.

```
from enoslib.api import play_on
from enoslib.infra.enos_vagrant.provider import Enos_vagrant
from enoslib.infra.enos_vagrant.configuration import Configuration

import logging

logging.basicConfig(level=logging.DEBUG)

conf = Configuration.from_settings(backend="libvirt",
                                   box="generic/debian9")\
                    .add_machine(roles=["control"],
                                 flavour="tiny",
                                 number=1)\
                    .add_machine(roles=["compute"],
                                 flavour="tiny",
                                 number=1)\
                    .add_network(roles=["mynetwork"],
                                 cidr="192.168.42.0/24")

provider = Enos_vagrant(conf)
roles, networks = provider.init()
with play_on("all", roles=roles) as p:
    p.apt(name=["apt-transport-https", "ca-certificates", "curl"])
    p.shell(" which docker || (curl -sSL https://get.docker.com/ | sh)")
    p.apt(name="python-pip", state="present")
    p.pip(name="docker", state="present")
```
Edited by SIMONIN Matthieu

Merge request reports