From 850a2d88fb898d2518c48bb29563581e3f422698 Mon Sep 17 00:00:00 2001
From: Chris Croome <chris@webarchitects.co.uk>
Date: Wed, 6 Jul 2022 17:29:02 +0100
Subject: [PATCH] Ansible local_fact tasks updated

---
 meta/main.yml               |  4 ++--
 tasks/local_facts.yml       | 39 ++++++++++++++++++++++++++-----------
 tasks/main.yml              |  5 ++++-
 templates/bash.fact.j2      | 11 +++++++++++
 templates/docker.sources.j2 |  2 +-
 templates/dpkg.fact.j2      | 12 ++++++++++++
 templates/gpg.fact.j2       | 12 ++++++++++++
 7 files changed, 70 insertions(+), 15 deletions(-)
 create mode 100644 templates/bash.fact.j2
 create mode 100644 templates/dpkg.fact.j2
 create mode 100644 templates/gpg.fact.j2

diff --git a/meta/main.yml b/meta/main.yml
index 6e07013..6a7f008 100644
--- a/meta/main.yml
+++ b/meta/main.yml
@@ -5,8 +5,8 @@ galaxy_info:
   namespace: chriscroome
   description: Ansible role for installing Docker CE on Debian and Ubuntu
   company: Webarchitects Co-operative
-  license: license (GPLv3)
-  min_ansible_version: 2.9
+  license: GNU General Public License v3.0 (GPLv3)
+  min_ansible_version: 2.10
   platforms:
     - name: debian
       versions:
diff --git a/tasks/local_facts.yml b/tasks/local_facts.yml
index 721d930..6ea58ed 100644
--- a/tasks/local_facts.yml
+++ b/tasks/local_facts.yml
@@ -13,34 +13,51 @@
         path: /etc/ansible/facts.d
         recurse: true
         state: directory
-        mode: 0700
+        mode: 0755
         owner: root
         group: root
 
-    - name: Ansible dpkg architecture local fact script present
+    - name: Ansible local facts scripts present
       ansible.builtin.template:
-        src: dpkg_arch.fact.j2
-        dest: /etc/ansible/facts.d/dpkg_arch.fact
-        mode: 0700
+        src: "{{ script }}.j2"
+        dest: "/etc/ansible/facts.d/{{ script }}"
+        mode: 0755
         owner: root
         group: root
-      register: apt_arch_facts
+      loop:
+        - dpkg.fact
+        - gpg.fact
+        - bash.fact
+      loop_control:
+        loop_var: script
 
     - name: Re-read Ansible local facts
       ansible.builtin.setup:
         filter: ansible_local
 
-    - name: Check that ansible_local.dpkg_arch is defined
+    - name: Check that ansible_local facts are defined
       ansible.builtin.assert:
         that:
-          - ansible_local.dpkg_arch is defined
+          - ansible_local.dpkg is defined
+          - ansible_local.gpg is defined
+          - ansible_local.bash is defined
 
-    - name: Print Ansible dpkg --architecture variables
+    - name: Print Ansible local dpkg facts
       ansible.builtin.debug:
-        var: ansible_local.dpkg_arch
+        var: ansible_local.dpkg
+        verbosity: 2
+
+    - name: Print Ansible local gpg facts
+      ansible.builtin.debug:
+        var: ansible_local.gpg
+        verbosity: 2
+
+    - name: Print Ansible local bash facts
+      ansible.builtin.debug:
+        var: ansible_local.bash
         verbosity: 2
 
   tags:
-    - docker
+    - docker 
     - docker_local_facts
 ...
diff --git a/tasks/main.yml b/tasks/main.yml
index fcea900..4c12a56 100644
--- a/tasks/main.yml
+++ b/tasks/main.yml
@@ -4,7 +4,10 @@
 
     - name: Include Docker local facts tasks
       ansible.builtin.include_tasks: local_facts.yml
-      when: ansible_local.dpkg_arch is not defined
+      when: >
+        ( ansible_local.dpkg.arch is not defined ) or
+        ( ansible_local.gpg.version is not defined ) or
+        ( ansible_local.bash.path is not defined )
       tags:
         - docker_apt
         - docker_compose_v1
diff --git a/templates/bash.fact.j2 b/templates/bash.fact.j2
new file mode 100644
index 0000000..27caa93
--- /dev/null
+++ b/templates/bash.fact.j2
@@ -0,0 +1,11 @@
+#!/usr/bin/env bash
+# {{ ansible_managed }}
+
+set -euo pipefail
+BASH_PATH=$(which bash)
+
+if [[ -f "${BASH_PATH}" ]]; then
+  jo state=present path="${BASH_PATH}"
+else
+  jo state=absent
+fi
diff --git a/templates/docker.sources.j2 b/templates/docker.sources.j2
index 126fcc2..48ef3de 100644
--- a/templates/docker.sources.j2
+++ b/templates/docker.sources.j2
@@ -2,7 +2,7 @@
 
 Types: deb 
 URIs: https://download.docker.com/linux/{{ ansible_distribution | lower }}
-Architectures: {{ ansible_facts.ansible_local.dpkg_arch.arch }}
+Architectures: {{ ansible_facts.ansible_local.dpkg.arch }}
 Components: stable
 Suites: {{ ansible_distribution_release }}
 Signed-By: /etc/apt/keyrings/docker.gpg
diff --git a/templates/dpkg.fact.j2 b/templates/dpkg.fact.j2
new file mode 100644
index 0000000..e6aefb6
--- /dev/null
+++ b/templates/dpkg.fact.j2
@@ -0,0 +1,12 @@
+#!/usr/bin/env bash
+# {{ ansible_managed }}
+
+set -euo pipefail
+DPKG="{{ apt_dpkg }}"
+
+if [[ -f "${DPKG}" ]]; then
+  ARCH=$("${DPKG}" --print-architecture)
+  jo state=present arch="${ARCH}"
+else
+  jo state=absent
+fi
diff --git a/templates/gpg.fact.j2 b/templates/gpg.fact.j2
new file mode 100644
index 0000000..a6e1fc6
--- /dev/null
+++ b/templates/gpg.fact.j2
@@ -0,0 +1,12 @@
+#!/usr/bin/env bash
+# {{ ansible_managed }}
+
+set -euo pipefail
+GPG="{{ apt_gpg }}"
+
+if [[ -f "${GPG}" ]]; then
+  VERSION=$("${GPG}" --version | head -n1 | awk '{ print $3 }')
+  jo state=present version="${VERSION}"
+else
+  jo state=absent
+fi
-- 
GitLab