Skip to content

clone_ansible_roles.py

Clone ansible roles and collections.

This script will parse ansible-galaxy requirements files at the root of git repos and clone roles and collections which have a 'src' key defining git repos URL.

Other roles which do not have src key, will be added to a temporary requirements files which will then be used normally with the ansible-galaxy command.

Attributes:

Name Type Description
SCRIPT_DIR str

Absolute path to the directory where this script is

COLORS dict

Dictionary holding string allowing to color print text

src.clone_ansible_roles.COLORS: dict

src.clone_ansible_roles.SCRIPT_DIR: str

Classes

src.clone_ansible_roles.CloneRoles

Class handling cloning of ansible roles and collections.

Class responsible for download ansible roles and collections either using git or ansible-galaxy depending on the requirements.yaml content.

Attributes:

Name Type Description
REQUIREMENT_FILENAME str

String storing default requirements.yaml

REQUIREMENT_FILENAME_TEMP str

String storing temporary requirements.yaml

Attributes

src.clone_ansible_roles.CloneRoles.REQUIREMENT_FILENAME: str
src.clone_ansible_roles.CloneRoles.REQUIREMENT_FILENAME_TEMP: str
src.clone_ansible_roles.CloneRoles.delete_temp_file: bool property writable

Boolean to track installation types.

Boolean to know if installation of requirement with ansible-galaxy default value went right (True) or wrong(False).

src.clone_ansible_roles.CloneRoles.requirement_ansible_galaxy: dict property writable

Dictionary storing ansible-galaxy requirements.

src.clone_ansible_roles.CloneRoles.requirement_ansible_galaxy_temp: dict property writable

Dictionary storing temporary ansible-galaxy requirements.

Dictionary storing temporary ansible galaxy requirement that can not be cloned using git and will be downloaded normally.

Methods

src.clone_ansible_roles.CloneRoles.__init__(self) special

Initialisation method.

Source code in src/clone_ansible_roles.py
def __init__(self) -> None:
    """Initialisation method."""
    self.requirement_ansible_galaxy_temp = dict(roles=[], collections=[])
    self.requirement_ansible_galaxy = {}
    self.delete_temp_file = True
src.clone_ansible_roles.CloneRoles.process(self)

Process the ansible-galaxy requirements.

Main computation method which will process the ansible-galaxy requirements file at the root of a git repo.

Source code in src/clone_ansible_roles.py
def process(self) -> None:
    """Process the `ansible-galaxy` requirements.

    Main computation method which will process the `ansible-galaxy`
    requirements file at the root of a git repo.
    """
    nothing_to_do_msg = True
    git_root_dir = get_git_root(SCRIPT_DIR)
    self.requirement_ansible_galaxy = load_yaml(
        os.path.join(git_root_dir, self.REQUIREMENT_FILENAME)
    )
    for i_key in self.requirement_ansible_galaxy:
        for i_item in self.requirement_ansible_galaxy[i_key]:
            if not self._process_ansible_item(i_key, i_item):
                nothing_to_do_msg = False
    # Install roles that does not have 'src' key defining git repo using
    # ansible-galaxy default command.
    if self.requirement_ansible_galaxy:
        self._process_default_ansible_requirements(git_root_dir)

    if nothing_to_do_msg:
        print(
            COLORS["GREEN"]
            + "[INFO] Nothing to do, roles are already installed."
            + COLORS["END_COLORS"]
        )

Functions

src.clone_ansible_roles.get_git_root(path)

Return the root path of the git repo from a path given as arguments.

Parameters:

Name Type Description Default
path str

Path from which to find the git root path

required

Returns:

Type Description
str

Git root path

Source code in src/clone_ansible_roles.py
def get_git_root(path: str) -> str:
    """Return the root path of the git repo from a path given as arguments.

    Arguments:
        path: Path from which to find the git root path

    Return:
        Git root path
    """
    git_repo = git.Repo(path, search_parent_directories=True)
    return git_repo.git.rev_parse("--show-toplevel")

src.clone_ansible_roles.load_yaml(input_file)

Load content of yaml into_file into a dictionary.

Parameters:

Name Type Description Default
input_file str

Path to the input file to load.

required
Source code in src/clone_ansible_roles.py
def load_yaml(input_file: str) -> dict:
    """Load content of yaml `into_file` into a dictionary.

    Arguments:
        input_file: Path to the input file to load.

    """
    with open(input_file, "r", encoding="utf-8") as stream:
        try:
            output_dict = yaml.safe_load(stream)
        except yaml.YAMLError as exc:
            print(exc)
    return output_dict

src.clone_ansible_roles.main()

Main method.

Main method, simply create a CloneRoles object and run CloneRoles.process().

Source code in src/clone_ansible_roles.py
def main() -> None:
    """Main method.

    Main method, simply create a CloneRoles object and run CloneRoles.process().
    """
    clone_roles = CloneRoles()
    clone_roles.process()

src.clone_ansible_roles.write_yaml(data, output_file)

Write content of data into output_file in a yaml format.

Parameters:

Name Type Description Default
data dict

Dictionary which hold data to write.

required
output_file str

Path to the output file to which data will be written.

required
Source code in src/clone_ansible_roles.py
def write_yaml(data: dict, output_file: str) -> None:
    """Write content of `data` into `output_file` in a yaml format.

    Arguments:
        data: Dictionary which hold data to write.
        output_file: Path to the output file to which `data` will be written.
    """
    with open(output_file, "w", encoding="utf-8") as stream:
        try:
            yaml.dump(data, stream)
        except yaml.YAMLError as exc:
            print(exc)

Last update: April 23, 2021
Back to top