simple_automation.manager.Manager

class simple_automation.manager.Manager(inventory_class, main_directory=None)

Bases: simple_automation.vars.Vars

A class that manages a set of global variables, hosts, groups, and tasks. It provides the CLI interface and represents the main entry point for a simple automation script.

All relative paths (mainly files used in basic.template() or basic.copy()) will be interpreted relative from the location of the initially executed script. If you want to change this behavior, you can either set main_directory to a relative path, which will then be appended to that location, or to an absolute path.

Parameters
  • inventory_class (cls) – The inventory class to instanciate.

  • main_directory (str, optional) – The main directory of the script. Will be used to determine relative paths. If set to None, it will be set to the directory of the executed script file.

Methods

add_group

Registers a new group.

add_host

Registers a new host.

add_task

Registers a given task class.

add_vault

Registers a vault of the given class, with its storage at file.

copy

Copies a value from another vars object into this one.

get

Retrieves a variable by the given key.

main

The main program entry point.

set

Sets the given variable.

add_group(identifier: str)

Registers a new group.

Parameters

identifier (str) – The identifier for the new group.

Returns

The newly created group

Return type

Group

add_host(identifier: str, ssh_host: str)

Registers a new host.

Parameters
  • identifier (str) – The identifier for the new host.

  • ssh_host (str) – The ssh host.

Returns

The newly created host.

Return type

Host

add_task(task_class)

Registers a given task class. This allows the task to register variable defaults. You can either save the returned instance yourself and call task.exec() when you want to run it, or you can use context.run_task(task_class) to run a registered task automatically.

Parameters

identifier (str) – The identifier for the new task.

Returns

The newly created task.

Return type

Task

add_vault(vault_class, file: str, **kwargs)

Registers a vault of the given class, with its storage at file. Additional parameters are forwarded to the vault constructor.

Parameters
  • vault_class (class(Vault)) – The vault class to instanciate.

  • file (str) – A file relative to the project directory that will be passed onto the vault.

  • **kwargs – Will be forwarded to the Vault constructor.

Returns

The newly created vault

Return type

Vault

copy(key, other_vars)

Copies a value from another vars object into this one. Same as calling self.set(key, other_vars.get(key))

Parameters
  • key (str) – The key that should be copied.

  • other_vars (Vars) – The source variable storage where the key is copied from.

get(key, default=None)

Retrieves a variable by the given key. If no such key exists, it returns the given default value or throws a KeyError if no default is set.

Parameters
  • key (str) – The key that should be read.

  • default (Any, optional) – If not None, this will be returned in case the key is unset. By default None.

Returns

The stored object.

Return type

Any

main()

The main program entry point. This will parse arguments and call the user-supplied function on the defined inventory, when the script should be executed.

set(key, value)

Sets the given variable.

Parameters
  • key (str) – The key that should be read.

  • value (Any, optional) – The value to be stored. Must be json (de-)serializable.