ssg.ansible module

Common functions for processing Ansible in SSG

class ssg.ansible.AnsibleSnippetsProcessor(all_snippets)[source]

Bases: object

Processes Ansible snippets to optimize package management tasks.

This class processes a collection of Ansible snippets by: - Collecting package-related tasks for batch processing - Skipping redundant package_facts tasks - Handling block tasks recursively - Preserving other tasks as-is

get_ansible_tasks()[source]

Get the final list of processed Ansible tasks.

Package facts tasks are added at the beginning and end of package tasks, then combined with other tasks.

Returns:

Combined list of all processed tasks.

Return type:

list

process_snippets()[source]

Process all snippets provided during initialization.

ssg.ansible.add_minimum_version(ansible_src)[source]

Adds a minimum Ansible version requirement to an Ansible script.

This function inserts a pre_task into the provided Ansible script to assert that the Ansible version is greater than or equal to a specified minimum version. If the script already contains a pre_task or the version check, it will return the original script. If a pre_task exists but does not contain the version check, it raises a ValueError.

Parameters:

ansible_src (str) – The source code of the Ansible script.

Returns:

The modified Ansible script with the minimum version requirement added.

Return type:

str

Raises:

ValueError – If a pre_task already exists in the Ansible script but does not contain the version check.

ssg.ansible.remove_too_many_blank_lines(ansible_src)[source]

Condenses three or more consecutive empty lines into two empty lines.

Parameters:

ansible_src (str) – The source string from an Ansible file.

Returns:

The modified string with excessive blank lines reduced.

Return type:

str

ssg.ansible.remove_trailing_whitespace(ansible_src)[source]

Remove trailing whitespace from each line in the given Ansible source string.

Parameters:

ansible_src (str) – The Ansible source code as a string.

Returns:

The Ansible source code with trailing whitespace removed from each line.

Return type:

str

ssg.ansible.task_is(task, names)[source]

Check if the task is one of the given names.

Parameters:
  • task (dict) – The task to check.

  • names (list) – List of task names to check against.

Returns:

True if the task contains any of the given names as keys, False otherwise.

Return type:

bool