Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion playbooks/site.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- hosts: all
tags: [frontend, node, grafana, stage1, stage2, stage3]
tags: [frontend, node, grafana, stage1, stage2, stage3, stage4]
collections:
- opennebula.deploy
roles:
Expand Down Expand Up @@ -101,6 +101,17 @@
tags: [prometheus]
when: *prometheus

- hosts: "{{ node_group | d('node') }}"
tags: [node, stage2]
collections:
- opennebula.deploy
roles:
# NOTE: If datastore IDs are not "predicted" for the generic mode, we try to post-patch
# symlinks iff IDs are merged back into the ds_dict (otherwise nothing should happen).
- role: datastore/node
tags: [datastore]
when: ds.mode is defined and ds.mode == 'generic'

- hosts: "{{ grafana_group | d('grafana') }}"
collections:
- opennebula.deploy
Expand Down
24 changes: 24 additions & 0 deletions roles/datastore/generic/tasks/_init.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
- name: Compute helper facts
ansible.builtin.set_fact:
ds_names: >-
{{ hostvars[leader | d(omit)].ds_names | d(_items | map(attribute='key') | list) }}
ds_dict: >-
{{ hostvars[leader | d(omit)].ds_dict | d(_items | items2dict) }}
vars:
_ds: >-
{{ ds_defaults | combine(ds.config | d({}), recursive=true) }}
_items: >-
{{ _ds | dict2items
| map(attribute='value')
| map('dict2items')
| flatten
| rejectattr('value.managed', 'false') }}

- name: Ensure /var/lib/one/datastores/ exists
ansible.builtin.file:
path: /var/lib/one/datastores/
state: directory
owner: 9869 # oneadmin
group: 9869 # oneadmin
mode: u=rwx,g=rx,o=
48 changes: 48 additions & 0 deletions roles/datastore/generic/tasks/_link.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
- name: Convert datastore directories into symlinks
ansible.builtin.shell:
cmd: |
set -o errexit

if [[ -L '{{ _base_path }}' ]]; then
exit 0
fi

if ! [[ -d '{{ _mount_path }}' ]]; then
echo 'Symlink target {{ _mount_path }} does not exist or is not a directory.' >&2
exit 1
fi

if [[ -d '{{ _base_path }}' ]] && ! rmdir '{{ _base_path }}'; then
exit 2
fi

if ! ln -s '{{ _mount_path }}' '{{ _base_path }}'; then
exit 3
fi

exit 78 # EREMCHG
executable: /bin/bash
when:
- inventory_hostname in _hosts
- ds_dict[item].id is defined
# NOTE: It doesn't really make sense to symlink /var/lib/one/datastores/,
# so we use that as a skip condition.
- _mount_path != '/var/lib/one/datastores'
vars:
# This isn't strictly a "mountpoint".
_mount_path: >-
{{ ds_dict[item].symlink.src | d('/var/lib/one/datastores/') | normpath }}
_base_path: >-
/var/lib/one/datastores/{{ ds_dict[item].id }}
_hosts: >-
{{ ds_dict[item].symlink.groups | d([])
| map('extract', groups)
| flatten
| unique }}
register: shell
changed_when:
- shell.rc in [78]
failed_when:
- shell.rc not in [0, 78]
loop: "{{ ds_names }}"
72 changes: 0 additions & 72 deletions roles/datastore/generic/tasks/common.yml

This file was deleted.

64 changes: 44 additions & 20 deletions roles/datastore/generic/tasks/frontend.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
---
- ansible.builtin.import_tasks:
file: "{{ role_path }}/tasks/common.yml"
file: "{{ role_path }}/tasks/_init.yml"

- ansible.builtin.include_role:
name: opennebula/leader
when: leader is undefined

- when: inventory_hostname == leader
- delegate_to: "{{ leader }}"
run_once: true
block:
- name: Get Datastores
ansible.builtin.shell:
Expand All @@ -15,22 +16,20 @@
changed_when: false
register: shell

- name: Parse Datastores
ansible.builtin.set_fact:
ds_names_parsed: >-
{{ _names }}
ds_dict_parsed: >-
{{ dict(_names | zip(_datastores)) }}
vars:
_document: >-
{{ shell.stdout | from_json }}
_datastores: >-
{{ [_document.DATASTORE_POOL.DATASTORE | d([])] | flatten }}
_names: >-
{{ _datastores | map(attribute='NAME') | d([]) | list }}
- name: Parse Datastores
ansible.builtin.set_fact:
ds_names_parsed: >-
{{ _names }}
ds_dict_parsed: >-
{{ dict(_names | zip(_datastores)) }}
vars:
_document: >-
{{ shell.stdout | from_json }}
_datastores: >-
{{ [_document.DATASTORE_POOL.DATASTORE | d([])] | flatten }}
_names: >-
{{ _datastores | map(attribute='NAME') | d([]) | list }}

- when: inventory_hostname == leader
block:
# NOTE: Only "system" datastores can be enabled or disabled.
- name: Update Datastores
ansible.builtin.shell:
Expand All @@ -49,9 +48,11 @@
{{ _combined | opennebula.deploy.to_one }}
executable: /bin/bash
changed_when: true
when: (_combined | opennebula.deploy.to_one != ds_dict_parsed[item].TEMPLATE | opennebula.deploy.to_one)
or
(ds_dict[item].enabled is defined and (ds_dict[item].enabled != (ds_dict_parsed[item].STATE | int == 0)))
when:
- ds_dict[item].id is defined
- (_combined | opennebula.deploy.to_one != ds_dict_parsed[item].TEMPLATE | opennebula.deploy.to_one)
or
(ds_dict[item].enabled is defined and (ds_dict[item].enabled != (ds_dict_parsed[item].STATE | int == 0)))
loop: "{{ _existing }}"
vars:
_existing: >-
Expand All @@ -73,6 +74,29 @@
executable: /bin/bash
changed_when: true
loop: "{{ _missing }}"
register: shell_create_datastores
vars:
_missing: >-
{{ ds_names | reject('in', ds_names_parsed) }}

- name: Merge back datastore IDs into ds_dict
ansible.builtin.set_fact:
ds_dict: >-
{{ ds_dict | combine(_update1, _update2, recursive=true) }}
vars:
# IDs can be sourced either from initial parse or immediately after new datastores are created.
_update1: >-
{%- set output = [] -%}
{%- for item in ds_dict_parsed.items() -%}
{{- output.append([item.0, {"id": item.1.ID | int}]) -}}
{%- endfor -%}
{{- dict(output) -}}
_update2: >-
{%- set output = [] -%}
{%- for item in shell_create_datastores.results -%}
{{- output.append([item.item, {"id": item.stdout_lines[-1].split(':')[-1] | trim | int}]) -}}
{%- endfor -%}
{{- dict(output) -}}

- ansible.builtin.import_tasks:
file: "{{ role_path }}/tasks/_link.yml"
5 changes: 4 additions & 1 deletion roles/datastore/generic/tasks/node.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
---
- ansible.builtin.import_tasks:
file: "{{ role_path }}/tasks/common.yml"
file: "{{ role_path }}/tasks/_init.yml"

- ansible.builtin.import_tasks:
file: "{{ role_path }}/tasks/_link.yml"