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:
hostnamestr - The name of the hosthost_ipstr - The ip or fqdn of the hosthost_portint - 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_methodHostConnectionMethod - Value from the Enum, define user/password or user/keyfile connection methodusernamestr - user to connect on host using sshpass_or_keyfilestr - 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_usernamestr - username of a user with sudo privilegesudo_passwordstr - password of a user with sudo privilege
Raises:
ValueError- If there are missing value, raise the Exception
get_yml
def get_yml() -> strRender 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:
hostdict - 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) -> boolCheck if the hostname has already been added
Arguments:
new_hostnamestr - The hostname to check
Raises:
ValueError- If there are missing value, raise the Exception
Returns:
bool- True if the hostname is unique else False