[PATCH] guestfs: port status script to Ansible with libvirt URI fix

Daniel Gomez <[email protected]>
Newsgroups dev.linux.lists.kdevops
Message-ID <[email protected]>
From: Daniel Gomez <[email protected]>

Convert scripts/status_guestfs.sh shell script to Ansible implementation
in playbooks/roles/guestfs/tasks/status/main.yml. The new implementation:

- Uses proper libvirt_uri variable for LIBVIRT_DEFAULT_URI
- Integrates with DIY callback to show command output
- Provides idempotent Ansible approach with tagging support
- Fixes missing libvirt URI context that caused empty virsh output

Update scripts/guestfs.Makefile to call ansible-playbook with --tags status
instead of the shell script. Add missing output yaml to LIBVIRT_URI_PATH
for proper URI variable generation.

Generated-by: Claude AI
Signed-off-by: Daniel Gomez <[email protected]>
---
The conversion in combination with the DIY stdout callback support [1]
results in this nice human readable output:

Link: https://lore.kernel.org/kdevops/[email protected]/ [1]

make status
PLAY: MANAGE INFRASTRUCTURE LIFECYCLE AND SSH ACCESS WITH LIBVIRT/GUESTFS
TASK: Gathering Facts
start: [localhost]
start: [debian13-xarray]
ok: [localhost]
ok: [debian13-xarray]
TASK: Display VM status
start: [localhost]
 Id   Name              State
---------------------------------
 9    debian13-xarray   running
PLAYBOOK SUMMARY
================
STATUS        OK  CHANGED  FAILED  HOST
OK             1        0       0  debian13-xarray
OK             2        0       0  localhost

I think DIY should be the default stdout callback for kdevops. This
example is meant to encourage users to try it out, and if the feedback
is positive, we can move forward with a formal proposal (there's no
rush).
---
 kconfigs/Kconfig.libvirt                      | 1 +
 playbooks/roles/guestfs/tasks/main.yml        | 7 +++++++
 playbooks/roles/guestfs/tasks/status/main.yml | 9 +++++++++
 scripts/guestfs.Makefile                      | 5 ++++-
 scripts/status_guestfs.sh                     | 5 -----
 5 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/kconfigs/Kconfig.libvirt b/kconfigs/Kconfig.libvirt
index 25cb703f..af34a33c 100644
--- a/kconfigs/Kconfig.libvirt
+++ b/kconfigs/Kconfig.libvirt
@@ -118,6 +118,7 @@ endchoice
 
 config LIBVIRT_URI_PATH
 	string "Libvirt QEMU URI to use"
+	output yaml
 	default "qemu:///system" if LIBVIRT_URI_SYSTEM || LIBVIRT_URI_CUSTOM
 	default "qemu:///session" if LIBVIRT_URI_SESSION
 	help
diff --git a/playbooks/roles/guestfs/tasks/main.yml b/playbooks/roles/guestfs/tasks/main.yml
index 2ec44204..6618687e 100644
--- a/playbooks/roles/guestfs/tasks/main.yml
+++ b/playbooks/roles/guestfs/tasks/main.yml
@@ -77,3 +77,10 @@
   ansible.builtin.import_tasks:
     file: "{{ role_path }}/tasks/destroy.yml"
   delegate_to: localhost
+
+- name: Status VM tasks
+  tags:
+    - status
+  ansible.builtin.import_tasks:
+    file: "{{ role_path }}/tasks/status/main.yml"
+  delegate_to: localhost
diff --git a/playbooks/roles/guestfs/tasks/status/main.yml b/playbooks/roles/guestfs/tasks/status/main.yml
new file mode 100644
index 00000000..ad1e0348
--- /dev/null
+++ b/playbooks/roles/guestfs/tasks/status/main.yml
@@ -0,0 +1,9 @@
+---
+- name: Display VM status
+  ansible.builtin.shell: |
+    LIBVIRT_DEFAULT_URI="{{ libvirt_uri }}" virsh list --all
+  changed_when: false
+  delegate_to: localhost
+  run_once: true
+  vars:
+    ansible_callback_diy_runner_on_ok_msg: "{{ ansible_callback_diy.result.output.stdout }}"
diff --git a/scripts/guestfs.Makefile b/scripts/guestfs.Makefile
index 953c6149..68aa282f 100644
--- a/scripts/guestfs.Makefile
+++ b/scripts/guestfs.Makefile
@@ -91,7 +91,10 @@ bringup_guestfs: $(GUESTFS_BRINGUP_DEPS)
 PHONY += bringup_guestfs
 
 status_guestfs:
-	$(Q)scripts/status_guestfs.sh
+	$(Q)ansible-playbook $(ANSIBLE_VERBOSE) \
+		playbooks/guestfs.yml \
+		--extra-vars=@./extra_vars.yaml \
+		--tags status
 PHONY += status_guestfs
 
 destroy_guestfs:
diff --git a/scripts/status_guestfs.sh b/scripts/status_guestfs.sh
deleted file mode 100755
index 84cb26cf..00000000
--- a/scripts/status_guestfs.sh
+++ /dev/null
@@ -1,5 +0,0 @@
-#!/bin/bash
-# SPDX-License-Identifier: copyleft-next-0.3.1
-
-virsh list
-exit 0

---
base-commit: 4deb164b5926aaef3f864f3a49358ec6c1da8238
change-id: 20250917-status-script-e22d4ac7b009

Best regards,
--  
Daniel Gomez <[email protected]>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.