diff --git a/examples/ansible/ec2.clone-instance.yml b/examples/ansible/ec2.clone-instance.yml index fc569bd..212f804 100644 --- a/examples/ansible/ec2.clone-instance.yml +++ b/examples/ansible/ec2.clone-instance.yml @@ -53,22 +53,24 @@ tags: Name: Clone EC2 Instance AMI root_device_name: "{{ original_instance_information.instances[0].root_device_name }}" - device_mapping: >- - {%- set 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({ - 'device_name': device.device_name, - 'snapshot_id': result.snapshots | sort(attribute='start_time') | last | json_query('snapshot_id'), - 'volume_type': 'gp3', - 'delete_on_termination': true, - }) - -}} - {%- endfor -%} - {%- endfor -%} - {{ 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) -%} + {{- + 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', + 'delete_on_termination': true, + }) + -}} + {%- endfor -%} + {%- endfor -%} + {{ ns.devices_list }} register: original_instance_ami - name: Use the AMI to launch a clone tags: diff --git a/snippets/ansible/tasks.yml b/snippets/ansible/tasks.yml index 2839c4a..7024b65 100644 --- a/snippets/ansible/tasks.yml +++ b/snippets/ansible/tasks.yml @@ -245,14 +245,16 @@ - postgis - pg_stat_statements ansible.builtin.set_fact: - db_extension_pairs: >- - {%- set output = [] -%} - {%- for db in db_extensions.keys() -%} - {%- for extension in db_extensions[db] -%} - {{- output.append({'db':db, 'extension': extension}) -}} + 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] -%} + {{- ns.output.append({'db':db, 'extension': extension}) -}} + {%- endfor -%} {%- 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 = [] -%} - {%- 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({ - 'device_name': device.device_name, - 'snapshot_id': result.snapshots | sort(attribute='start_time') | last | json_query('snapshot_id'), - }) - -}} - {%- endfor -%} - {%- endfor -%} - {{ 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) -%} + {{- + ns.devices_list.append({ + 'device_name': device.device_name, + 'snapshot_id': result.snapshots | sort(attribute='start_time') | last | json_query('snapshot_id'), + }) + -}} + {%- endfor -%} + {%- endfor -%} + {{ ns.devices_list }} - name: "Use the users' home directory for something" block: