From 20bb55ec5f7eb3e9aaf67e9546add44246cd2688 Mon Sep 17 00:00:00 2001
From: Chris Croome <chris@webarchitects.co.uk>
Date: Sun, 26 Jan 2025 10:31:20 +0000
Subject: [PATCH] add apache_install tag to local facts tasks

---
 README.md             | 2 +-
 tasks/checks.yml      | 9 +++++++++
 tasks/local_facts.yml | 1 +
 tasks/main.yml        | 6 ++++++
 4 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 3db6cd0..ff405a6 100644
--- a/README.md
+++ b/README.md
@@ -10,7 +10,7 @@ By default this role uses [mod_md](https://github.com/icing/mod_md/) for HTTPS c
 
 ## Usage
 
-Run this role using the `apache_local_facts` tag for the intial install.
+Run this role using the `apache_install` tag for the intial install, once `apache2` has been installed the role can be run in check mode.
 
 ## Role variables
 
diff --git a/tasks/checks.yml b/tasks/checks.yml
index c3be017..9fa61ed 100644
--- a/tasks/checks.yml
+++ b/tasks/checks.yml
@@ -39,6 +39,15 @@
         manager: apt
       when: ansible_facts.packages is not defined
 
+    - name: The apache2 package is required to be installed for the tasks in this role to run
+      ansible.builtin.assert:
+        that:
+          - ( "apache2" in ansible_facts.packages.keys() )
+        quiet: "{% if ansible_check_mode or ansible_verbosity >= 1 %}false{% else %}true{% endif %}"
+        fail_msg: >-
+          Please run this role with the apache_install tag to just install apache2, once apache2 is installed
+          this role can be run in check mode.
+
     - name: Set a fact for the Apache apt package version
       ansible.builtin.set_fact:
         apache_version: >-
diff --git a/tasks/local_facts.yml b/tasks/local_facts.yml
index bda9f7e..4e07b7e 100644
--- a/tasks/local_facts.yml
+++ b/tasks/local_facts.yml
@@ -38,4 +38,5 @@
   tags:
     - apache
     - apache_local_facts
+    - apache_install
 ...
diff --git a/tasks/main.yml b/tasks/main.yml
index 5ec3e86..c54c455 100644
--- a/tasks/main.yml
+++ b/tasks/main.yml
@@ -14,6 +14,8 @@
   when: not apache | bool
   tags:
     - apache
+    - apache_install
+    - apache_local_facts
 
 - name: Install and configure Apache
   block:
@@ -24,6 +26,7 @@
         - apache_verify is defined
         - apache_verify | bool
       tags:
+        - apache_install
         - apache_local_facts
 
     - name: Include apt role local fact tasks
@@ -35,6 +38,7 @@
         ( ansible_facts.ansible_local.gpg.version is not defined ) or
         ( ansible_facts.ansible_local.bash.path is not defined )
       tags:
+        - apache_install
         - apache_local_facts
 
     - name: Packages present and absent
@@ -71,12 +75,14 @@
         ( ( apache_pkgs_absent is defined ) and ( apache_pkgs_absent != [] ) ) or
         ( ( apache_pkgs_present is defined ) and ( apache_pkgs_present != [] ) )
       tags:
+        - apache_install
         - apache_local_facts
 
     - name: Include local facts tasks
       ansible.builtin.include_tasks: local_facts.yml
       when: ansible_facts.ansible_local.a2query is not defined
       tags:
+        - apache_install
         - apache_local_facts
 
     - name: Include Apache checks
-- 
GitLab