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
24 changes: 12 additions & 12 deletions tasks/main-blivet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,10 @@
extra_pkgs:
- kpartx

- name: Get service facts
service_facts:
when: storage_skip_checks is not defined or
not "service_facts" in storage_skip_checks

# rejectattr required because the fix to service_facts is on Ansible > 2.12 only
# https://github.com/ansible/ansible/pull/75326
- name: Set storage_cryptsetup_services
set_fact:
- name: Manage storage devices and check for errors
vars:
# rejectattr required because the fix to service_facts is on Ansible > 2.12 only
# https://github.com/ansible/ansible/pull/75326
storage_cryptsetup_services: "{{
ansible_facts.services.values() |
selectattr('name', 'defined') |
Expand All @@ -72,10 +67,15 @@
rejectattr('status', 'match', '^failed$') |
map(attribute='name') |
select('match', '^systemd-cryptsetup@') |
list }}"

- name: Manage storage devices and check for errors
list
if 'services' in ansible_facts else [] }}"
block:
- name: Get service facts
service_facts:
when:
- not "services" in ansible_facts
- not "cryptsetup_services" in storage_skip_checks | d([])

- name: Mask the systemd cryptsetup services
systemd:
name: "{{ item }}"
Expand Down
58 changes: 58 additions & 0 deletions tests/action_plugins/merge_ansible_facts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-
#
# Ansible action plugin merge_ansible_facts.
# Merges saved facts with current Ansible facts on the controller and returns
# ansible_facts (saved_ansible_facts with current_ansible_facts overlaid).
#
# Copyright: (c) 2025, Linux System Roles
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function

__metaclass__ = type

from ansible.plugins.action import ActionBase


class ActionModule(ActionBase):

TRANSFERS_FILES = False

def run(self, tmp=None, task_vars=None):
if task_vars is None:
task_vars = {}

result = super(ActionModule, self).run(tmp, task_vars)
result["changed"] = False

current = self._task.args.get("current_ansible_facts")
saved = self._task.args.get("saved_ansible_facts")

if current is None:
result["failed"] = True
result["msg"] = "current_ansible_facts is required"
return result
if saved is None:
result["failed"] = True
result["msg"] = "saved_ansible_facts is required"
return result

# Template in case args were passed as raw Jinja
current = self._templar.template(current)
saved = self._templar.template(saved)

if not isinstance(current, dict):
result["failed"] = True
result["msg"] = "current_ansible_facts must be a dict"
return result
if not isinstance(saved, dict):
result["failed"] = True
result["msg"] = "saved_ansible_facts must be a dict"
return result

# Merge: start with a copy of saved, overlay current (same behavior as module)
merged = dict(saved)
merged.update(current)
result["ansible_facts"] = merged

return result
8 changes: 4 additions & 4 deletions tests/setup-snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
hosts: all
tasks:
- name: Set platform/version specific variables
include_role:
name: linux-system-roles.storage
tasks_from: set_vars.yml
public: true
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
__sr_tasks_from: set_vars.yml
__sr_public: true

- name: Install test packages
package:
Expand Down
38 changes: 38 additions & 0 deletions tests/tasks/run_role_with_clear_facts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
# Task file: save facts, clear_facts, run linux-system-roles.storage, then restore facts.
# Include this with include_tasks or import_tasks; ensure tests/library is in module search path.
- name: Save current ansible facts
set_fact:
saved_facts: "{{ ansible_facts }}"

- name: Clear facts
meta: clear_facts

# note that you can use failed_when with import_role but not with include_role
# so this simulates the __sr_failed_when false case
- name: Run the role with __sr_failed_when false
when:
- __sr_failed_when is defined
- not __sr_failed_when
block:
- name: Run the role
include_role:
name: linux-system-roles.storage
tasks_from: "{{ __sr_tasks_from | default('main') }}"
public: "{{ __sr_public | default(false) }}"
rescue:
- name: Ignore the failure when __sr_failed_when is false
debug:
msg: Ignoring failure when __sr_failed_when is false

- name: Run the role normally
include_role:
name: linux-system-roles.storage
tasks_from: "{{ __sr_tasks_from | default('main') }}"
public: "{{ __sr_public | default(false) }}"
when: __sr_failed_when | d(true)

- name: Reset ansible_facts (saved base with current overrides)
merge_ansible_facts:
current_ansible_facts: "{{ ansible_facts }}"
saved_ansible_facts: "{{ saved_facts }}"
16 changes: 5 additions & 11 deletions tests/tests_change_disk_fs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
else 'ext4' }}"
tasks:
- name: Run the role
include_role:
name: linux-system-roles.storage
include_tasks: tasks/run_role_with_clear_facts.yml

- name: Mark tasks to be skipped
set_fact:
Expand All @@ -22,7 +21,6 @@
- "{{ (lookup('env',
'SYSTEM_ROLES_REMOVE_CLOUD_INIT') in ['', 'false']) |
ternary('packages_installed', '') }}"
- service_facts

- name: Get unused disks
include_tasks: get_unused_disk.yml
Expand All @@ -31,8 +29,7 @@
max_return: 1

- name: Create a disk device with the default file system type
include_role:
name: linux-system-roles.storage
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
storage_volumes:
- name: test1
Expand All @@ -44,8 +41,7 @@
include_tasks: verify-role-results.yml

- name: Change the disk device file system type to {{ fs_type_after }}
include_role:
name: linux-system-roles.storage
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
storage_volumes:
- name: test1
Expand All @@ -58,8 +54,7 @@
include_tasks: verify-role-results.yml

- name: Repeat the previous invocation to verify idempotence
include_role:
name: linux-system-roles.storage
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
storage_volumes:
- name: test1
Expand All @@ -72,8 +67,7 @@
include_tasks: verify-role-results.yml

- name: Clean up
include_role:
name: linux-system-roles.storage
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
storage_volumes:
- name: test1
Expand Down
16 changes: 5 additions & 11 deletions tests/tests_change_disk_mount.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
changed_when: false

- name: Run the role
include_role:
name: linux-system-roles.storage
include_tasks: tasks/run_role_with_clear_facts.yml

- name: Mark tasks to be skipped
set_fact:
Expand All @@ -30,7 +29,6 @@
- "{{ (lookup('env',
'SYSTEM_ROLES_REMOVE_CLOUD_INIT') in ['', 'false']) |
ternary('packages_installed', '') }}"
- service_facts

- name: Get unused disks
include_tasks: get_unused_disk.yml
Expand All @@ -39,8 +37,7 @@
max_return: 1

- name: Create a disk device mounted at "{{ mount_location_before }}"
include_role:
name: linux-system-roles.storage
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
storage_volumes:
- name: test1
Expand All @@ -52,8 +49,7 @@
include_tasks: verify-role-results.yml

- name: Change the disk device mount location to {{ mount_location_after }}
include_role:
name: linux-system-roles.storage
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
storage_volumes:
- name: test1
Expand All @@ -65,8 +61,7 @@
include_tasks: verify-role-results.yml

- name: Repeat the previous invocation to verify idempotence
include_role:
name: linux-system-roles.storage
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
storage_volumes:
- name: test1
Expand All @@ -78,8 +73,7 @@
include_tasks: verify-role-results.yml

- name: Clean up
include_role:
name: linux-system-roles.storage
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
storage_volumes:
- name: test1
Expand Down
23 changes: 7 additions & 16 deletions tests/tests_change_fs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

tasks:
- name: Run the role
include_role:
name: linux-system-roles.storage
include_tasks: tasks/run_role_with_clear_facts.yml

- name: Mark tasks to be skipped
set_fact:
Expand All @@ -24,7 +23,6 @@
- "{{ (lookup('env',
'SYSTEM_ROLES_REMOVE_CLOUD_INIT') in ['', 'false']) |
ternary('packages_installed', '') }}"
- service_facts

- name: Get unused disks
include_tasks: get_unused_disk.yml
Expand All @@ -33,8 +31,7 @@
max_return: 1

- name: Create a LVM logical volume with default fs_type
include_role:
name: linux-system-roles.storage
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
storage_pools:
- name: foo
Expand All @@ -48,8 +45,7 @@
include_tasks: verify-role-results.yml

- name: Change the file system signature on the logical volume created above
include_role:
name: linux-system-roles.storage
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
storage_pools:
- name: foo
Expand All @@ -64,8 +60,7 @@
include_tasks: verify-role-results.yml

- name: Re-run the role on the same volume without specifying fs_type
include_role:
name: linux-system-roles.storage
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
storage_pools:
- name: foo
Expand All @@ -86,8 +81,7 @@
include_tasks: verify-role-results.yml

- name: Repeat the previous invocation to verify idempotence
include_role:
name: linux-system-roles.storage
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
storage_pools:
- name: foo
Expand All @@ -102,8 +96,7 @@
include_tasks: verify-role-results.yml

- name: Remove the FS
include_role:
name: linux-system-roles.storage
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
storage_pools:
- name: foo
Expand All @@ -116,10 +109,8 @@
- name: Verify role results - 5
include_tasks: verify-role-results.yml


- name: Clean up
include_role:
name: linux-system-roles.storage
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
storage_pools:
- name: foo
Expand Down
Loading
Loading