chore(ansible): use namespace in jinja template for source reasons

This commit is contained in:
Michele Cereda
2024-08-19 19:12:46 +02:00
parent 8e600c679c
commit 58c0bd62ae
2 changed files with 43 additions and 37 deletions

View File

@@ -53,13 +53,15 @@
tags:
Name: Clone EC2 Instance AMI
root_device_name: "{{ original_instance_information.instances[0].root_device_name }}"
device_mapping: >-
{%- set devices_list = [] -%}
device_mapping:
# Refer https://jinja.palletsprojects.com/en/3.0.x/templates/#assignments for the namespace object's reason
>-
{%- set ns = namespace(devices_list = []) -%}
{%- for result in original_instance_snapshots.results -%}
{%- for device in original_instance_information.instances[0].block_device_mappings
| selectattr('ebs.volume_id', 'equalto', result.volume_id) -%}
{{-
devices_list.append({
ns.devices_list.append({
'device_name': device.device_name,
'snapshot_id': result.snapshots | sort(attribute='start_time') | last | json_query('snapshot_id'),
'volume_type': 'gp3',
@@ -68,7 +70,7 @@
-}}
{%- endfor -%}
{%- endfor -%}
{{ devices_list }}
{{ ns.devices_list }}
register: original_instance_ami
- name: Use the AMI to launch a clone
tags:

View File

@@ -245,14 +245,16 @@
- postgis
- pg_stat_statements
ansible.builtin.set_fact:
db_extension_pairs: >-
{%- set output = [] -%}
db_extension_pairs:
# Refer https://jinja.palletsprojects.com/en/3.0.x/templates/#assignments for the namespace object's reason
>-
{%- set ns = namespace(output = []) -%}
{%- for db in db_extensions.keys() -%}
{%- for extension in db_extensions[db] -%}
{{- output.append({'db':db, 'extension': extension}) -}}
{{- ns.output.append({'db':db, 'extension': extension}) -}}
{%- endfor -%}
{%- endfor -%}
{{- output -}}
{{- ns.output -}}
- name: Get the device name and last snapshot id for all block devices in an EC2 instance
# Useful to create AMIs from instance snapshots
tags:
@@ -261,20 +263,22 @@
- snapshot
- ami
ansible.builtin.set_fact:
last_snap_for_device: >-
{%- set devices_list = [] -%}
last_snap_for_device:
# Refer https://jinja.palletsprojects.com/en/3.0.x/templates/#assignments for the namespace object's reason
>-
{%- set ns = namespace(devices_list = []) -%}
{%- for result in current_instance_snapshots.results -%}
{%- for device in current_instance_information.instances[0].block_device_mappings
| selectattr('ebs.volume_id', 'equalto', result.volume_id) -%}
{{-
devices_list.append({
ns.devices_list.append({
'device_name': device.device_name,
'snapshot_id': result.snapshots | sort(attribute='start_time') | last | json_query('snapshot_id'),
})
-}}
{%- endfor -%}
{%- endfor -%}
{{ devices_list }}
{{ ns.devices_list }}
- name: "Use the users' home directory for something"
block: