--- # check if meshvol mounted or for an abandoned mesh vol on the current AZ # at the end of this module we should have mesh_vol set if there is one to find # if we dont have mesh_vol then we need to either hydrate one from snap or make a new/empty one # first, try to set mesh_vol from currently mounted volume - name: create list of current mounts ansible.builtin.set_fact: mounts_list: "{{ ansible_mounts | map(attribute='mount') | list }}" # - name: discover mesh volume device stat # ansible.builtin.stat: # path: '/dev/xvdd' # follow: no # get_attributes: yes # register: mesh_device_stat - name: pull info for this_instance from instanceFacts ansible.builtin.set_fact: this_instance: "{{ instanceFacts.instances | first }}" - name: create list of current block devices ansible.builtin.set_fact: devices_list: "{{ this_instance.block_device_mappings | map(attribute='device_name') | list }}" - name: discover mesh volume device stat ansible.builtin.stat: path: '/dev/xvdd' follow: no get_attributes: yes register: mesh_device_stat - name: describe mesh volume true device ansible.builtin.set_fact: mesh_device: "{{ mesh_device_stat.stat['lnk_source'] }}" ignore_errors: yes when: '"lnk_source" in mesh_device_stat.stat' # lazy guess at mesh_vol - # TODO make this better - name: set mesh_vol from currently mounted device ansible.builtin.set_fact: mesh_vol: "{{ this_instance | community.general.json_query(vol_id_query) | first }}" vars: vol_id_query: "block_device_mappings[?(@.device_name=='/dev/xvdd')].ebs.volume_id" when: (mesh_device is defined) and (mesh_device|length > 0) - name: shall we check for abandoned? ansible.builtin.set_fact: abandoned_check: "{{ ((mesh_vol is not defined) or (mesh_vol|length == 0)) | ternary(true, false) }}" # second, if we dont have a mesh_vol yet, try to set it from abandoned - block: - name: check for abandoned ebs vols of this stack in current AZ amazon.aws.ec2_vol_info: region: "{{ ansible_ec2_placement_region }}" filters: status: 'available' "tag:service_name": "{{ atl_aws_stack_name }}" "tag:Name": "{{ atl_aws_stack_name }} mesh volume {{ ansible_ec2_placement_availability_zone }}" register: orphan_vol_info tags: notest # doesn't work in molecule - name: Extract orphan volume_id values using json_query ansible.builtin.set_fact: orphan_vol_list: "{{ orphan_vol_info | json_query('volumes[*].id') }}" - name: loop to get an orphan_lease_lock (temporary lock on volume cleaned up in handler) ansible.builtin.include_tasks: orphan_lease_lock.yml with_items: "{{ orphan_vol_list }}" loop_control: loop_var: test_orphan_vol when: orphan_vol is not defined - name: set mesh_vol from orphan_vol ansible.builtin.set_fact: mesh_vol: "{{ orphan_vol }}" when: - orphan_vol is defined - orphan_vol | length > 0 - name: attach abandoned mesh vol to this ec2 amazon.aws.ec2_vol: instance: "{{ ansible_ec2_instance_id }}" id: "{{ mesh_vol }}" region: "{{ ansible_ec2_placement_region }}" device_name: /dev/xvdd delete_on_termination: no when: - mesh_vol is defined - mesh_vol | length > 0 when: abandoned_check # third, if no mesh_vol and this is a clone, set mesh_snap - block: - name: create az dict attributes ansible.builtin.set_fact: my_attributes: - key: "{{ atl_mesh_azname_az1 }}" value: "{{ atl_mesh_snapshot_az1 }}" - key: "{{ atl_mesh_azname_az2 }}" value: "{{ atl_mesh_snapshot_az2 }}" - key: "{{ atl_mesh_azname_az3 }}" value: "{{ atl_mesh_snapshot_az3 }}" - name: create az dict if this stack is a mesh clone ansible.builtin.set_fact: azsnapdict: "{{ my_attributes | items2dict }}" - name: set mesh clone node snapshot fact if this stack is a mesh clone ansible.builtin.set_fact: mesh_snapshot: "{{ azsnapdict[ansible_ec2_placement_availability_zone] }}" ignore_errors: yes when: - (mesh_vol is not defined) or (mesh_vol|length == 0) - mesh_clone