From bd896dfd98342d4096dfb049228a2988a858b2bc Mon Sep 17 00:00:00 2001
From: Bruno Travouillon <bruno.travouillon@mila.quebec>
Date: Wed, 14 Sep 2022 16:22:12 -0400
Subject: [PATCH] fix: add condition to execute socket services

When listen_address is not defined (default), the drop-in files are not
created. Make sure the role will not try to manage the non-existing
services.
---
 handlers/main.yml |  3 +++
 tasks/config.yml  | 10 ++++------
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/handlers/main.yml b/handlers/main.yml
index 183bcc9..9d787ed 100644
--- a/handlers/main.yml
+++ b/handlers/main.yml
@@ -20,6 +20,9 @@
     state: "{{ item.enabled | bool | ternary('started', 'stopped') }}"
   become: true
   loop: "{{ _libvirt_socket_services }}"
+  when:
+    - item.listen_address is not none
+    - item.listen_address | length > 0
   loop_control:
     label: "{{ item.service }}"
   listen:
diff --git a/tasks/config.yml b/tasks/config.yml
index 7817377..45beb5f 100644
--- a/tasks/config.yml
+++ b/tasks/config.yml
@@ -139,15 +139,13 @@
     name: "{{ item.service }}"
     state: "{{ item.enabled | bool | ternary('started', 'stopped') }}"
     enabled: "{{ item.enabled | bool }}"
-  become: True
-  loop: "{{ _libvirt_services }}"
+  become: true
+  loop: "{{ _libvirt_services + _libvirt_socket_services }}"
+  when: item.listen_address is not defined or
+        (item.listen_address is not none and item.listen_address | length > 0)
   loop_control:
     label: "{{ item.service }}"
   vars:
     _libvirt_services:
-      - service: libvirtd-tcp.socket
-        enabled: "{{ libvirt_host_tcp_listen | bool }}"
-      - service: libvirtd-tls.socket
-        enabled: "{{ libvirt_host_tls_listen | bool }}"
       - service: libvirtd
         enabled: true