Files
dc-deployments-automation/roles/bitbucket_mesh_config/tasks/meshvol_check.yml
Lee Goolsbee e2ea50d4f7 more fixes
2024-03-26 09:32:20 -05:00

116 lines
4.1 KiB
YAML

---
# 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: Retrieve info for this instance from EC2
amazon.aws.ec2_instance_info:
region: "{{ ansible_ec2_placement_region }}"
instance_ids:
- "{{ ansible_ec2_instance_id }}"
retries: 10
delay: 10
register: ec2_instance_facts
until: ec2_instance_facts.error is not defined
- name: Create list of current block devices attached to this instance
ansible.builtin.set_fact:
devices_list: "{{ ec2_instance_facts.instances[0].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: "{{ ec2_instance_facts.instances[0] | 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