Ansible Python Helpers
- ansible.roles.build_kvm.files.helpers.download_or_retrieve_file(url_or_file)[source]
Download a file from a URL or file path.
Parameters
- url_or_filestr
The URL of the file to download or the local file path.
Returns
- str
The absolute file path where the downloaded file is saved.
Notes
This function supports downloading files from HTTP, as well as handling local file paths. The function prints the file path and the source (URL or local file) for logging purposes.
- ansible.roles.build_kvm.files.helpers.download_extract_and_rename_qcow2(url_or_file, extract_path, new_qcow_file=None, loc_dir=None)[source]
Download, extract, and optionally rename a qcow2 file.
Parameters
- url_or_filestr
The URL or file path of the qcow2 file.
- extract_pathstr
The path to extract the contents of the qcow2 file.
- new_qcow_filestr, optional
The new name for the qcow2 file (default is None).
- loc_dirstr, optional
The directory within the extracted path where the qcow2 file will be placed (default is None).
Returns
- Tuple[str, Path, str]
A tuple containing the path of the qcow2 file, the extraction path, and the file path of the downloaded file.
Notes
This function downloads a qcow2 file from a URL or uses a file path if provided. It extracts the contents of the qcow2 file to the specified extraction path. If a new name is specified, it renames the qcow2 file accordingly. The function returns a tuple containing the path of the qcow2 file, the extraction path, and the file path of the downloaded file.
- ansible.roles.build_kvm.files.helpers.generate_cloud_init_files(vm_username, vm_hashed_password, cloud_init_user_data_output, cloud_init_meta_data_output, vm_hostname, vm_static_ip_address=None, vm_ip_gateway=None, vm_ip_netmask=None, vm_dns_server_1=None, vm_dns_server_2=None, vm_nameserver=None)[source]
Generate cloud-init scripts for configuring a virtual machine.
Parameters
- vm_usernamestr
The username for the virtual machine.
- vm_hashed_passwordstr
The hashed password for the virtual machine user.
- cloud_init_user_data_outputstr
The path to the output file for cloud-init user data.
- cloud_init_meta_data_outputstr
The path to the output file for cloud-init metadata.
- vm_hostnamestr
The hostname for the virtual machine.
- vm_static_ip_addressstr, optional
The static IP address for the virtual machine (default is None).
- vm_ip_gatewaystr, optional
The IP gateway for the virtual machine (default is None).
- vm_ip_netmaskstr, optional
The IP netmask for the virtual machine (default is None). Supports dotted-decimal (eg. ‘255.255.255.255’) and CIDR (eg. ‘/32’) notations.
- vm_dns_server_1str, optional
The primary DNS server for the virtual machine (default is None).
- vm_dns_server_2str, optional
The secondary DNS server for the virtual machine (default is None).
- vm_nameserver: str, optional
The nameserver for the virtual machine (default is None). Multiple can be specified comma-seperated.
Returns
None
Notes
This function generates cloud-init scripts for configuring a virtual machine. It creates a cloud-init user data template based on the provided parameters, including the user, password, hostname, and networking settings. The generated user data template is written to the specified cloud_init_user_data_output file, while the metadata template containing only the hostname is written to the cloud_init_meta_data_output file.
- ansible.roles.build_kvm.files.helpers.generate_cloud_init_iso(cloud_init_iso_name, user_data_directory, meta_data_directory)[source]
Generate a cloud-init ISO image.
Parameters
- cloud_init_iso_namestr
The name of the cloud-init ISO image to generate.
- user_data_directorystr
The directory containing the user data file.
- meta_data_directorystr
The directory containing the metadata file.
Returns
None
Notes
This function generates a cloud-init ISO image using the genisoimage command-line tool. It combines the user data and metadata files from the specified directories into the ISO image. The generated ISO image is saved with the provided cloud_init_iso_name.
- ansible.roles.build_kvm.files.helpers.clean_up_sensitive_info(files_to_delete: list)[source]
Delete sensitive files from the system.
Parameters
- files_to_deletelist
A list of file paths to delete.
Returns
None
Notes
This function iterates through the list of file paths provided in files_to_delete and attempts to delete each file using the os.remove function. If deletion is successful, a message is printed indicating the file was deleted. If deletion fails due to an OSError, an error message is printed.