utils.hosts_selected.py

SingletonHostsSelected Objects

class SingletonHostsSelected()

This class is a sigleton object for HostsSelected class

Host Objects

class Host()

This class represents one host object to export it to yml for the ansible inventory

__init__

def __init__(hostname: str, host_ip: str, host_port: int)

Create Host instance and fill hostname, host_ip and host_port

Arguments:

  • hostname str - The name of the host
  • host_ip str - The ip or fqdn of the host
  • host_port int - The ssh port of the host

Raises:

  • ValueError - If there are missing value, raise the Exception

set_connection_method

def set_connection_method(connection_method: int, username: str,
                          pass_or_keyfile: str)

Fill connection_method, username and pass_or_keyfile.

Arguments:

  • connection_method HostConnectionMethod - Value from the Enum, define user/password or user/keyfile connection method
  • username str - user to connect on host using ssh
  • pass_or_keyfile str - password or the path of the keyfile to connect on host using ssh

Raises:

  • ValueError - If there are missing value, raise the Exception

set_sudo_access

def set_sudo_access(sudo_username: str, sudo_password: str)

Fill sudo_username and sudo_password to permits privilege escalation

Arguments:

  • sudo_username str - username of a user with sudo privilege
  • sudo_password str - password of a user with sudo privilege

Raises:

  • ValueError - If there are missing value, raise the Exception

get_yml

def get_yml() -> str

Render the Host instance into a string with yml syntax for the Ansible inventory file

Raises:

  • ValueError - If the value of connection_method is not in the Enum

Returns:

  • str - The yml string

HostsSelected Objects

class HostsSelected(SingletonHostsSelected)

This class keep in memory which hosts are selected and their configuration

add_host

def add_host(host: dict)

Create a host and add it to the list of hosts

Arguments:

  • host dict - Dict that contains value to add host

Raises:

HostAlreadyAdded : If the hostname already exists

  • ValueError - If there are missing value, raise the Exception

is_hostname_unique

def is_hostname_unique(new_hostname: str) -> bool

Check if the hostname has already been added

Arguments:

  • new_hostname str - The hostname to check

Raises:

  • ValueError - If there are missing value, raise the Exception

Returns:

  • bool - True if the hostname is unique else False