From da89e608758f160a346ff8e5ccceb6bc5ae61007 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Th=C3=A9o=20-=20Le=20Filament?= <theo@le-filament.com>
Date: Wed, 5 Jun 2024 16:42:50 +0200
Subject: [PATCH] feat: allow to use tags to trigger only some tasks

---
 tasks/instance.yml | 21 +++++++++++++++++++--
 tasks/main.yml     | 12 ++++++++----
 2 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/tasks/instance.yml b/tasks/instance.yml
index 1d3d95d..e84704e 100644
--- a/tasks/instance.yml
+++ b/tasks/instance.yml
@@ -8,6 +8,8 @@
       mode: '0755'
 
 - name: Container building requirements
+  tags:
+    - "odoo"
   when: item.value.image_instance | default(false) == item.key
   block:
       - name: Create Odoo private docker structure on server in /home/docker/{{ item.key }}
@@ -108,6 +110,8 @@
             - Build odoo image
 
 - name: Get image from another instance
+  tags:
+    - "odoo"
   when: item.value.image_instance | default(false) != item.key
   block:
       - name: Check if destination instance image exists
@@ -133,6 +137,9 @@
             source: local
 
 - name: Copy docker compose service
+  tags:
+    - "metabase"
+    - "odoo"
   ansible.builtin.template:
       src: docker-compose.yaml.j2
       dest: "/home/docker/{{ item.key }}/docker-compose.yml"
@@ -146,7 +153,8 @@
 # --------------------------------------------------
 - name: Restore backups from another instance
   when: (item.value.backup_instance | default(item.key) != item.key or item.value.backup_host | default(inventory_hostname) != inventory_hostname) and inventory_hostname in groups.maintenance_contract
-  tags: 'backup_odoo'
+  tags:
+    - "backup_odoo"
   block:
       - name: Copy sql script to be run before restoring db from backup_instance
         ansible.builtin.template:
@@ -181,8 +189,9 @@
 # prod backup section
 # --------------------------------------------------
 - name: Backup
+  tags:
+    - "backup_odoo"
   when: item.value.backup_instance | default(false) == item.key and item.value.backup_host | default(inventory_hostname) == inventory_hostname and inventory_hostname in groups.maintenance_contract
-  tags: 'backup_odoo'
   block:
       - name: "Copy docker compose for backup account {{ account_index + 1 }}"
         ansible.builtin.template:
@@ -212,18 +221,26 @@
 
 # Flush handlers.
 - name: Set facts
+  tags:
+    - "metabase"
+    - "odoo"
   ansible.builtin.set_fact:
       instance: "{{ {'key': item.key, 'value': item.value} }}"
       instance_odoo_version: "{{ instance_odoo_setup.odoo_version }}"
       cacheable: false
 
 - name: Flush handlers
+  tags:
+    - "metabase"
+    - "odoo"
   ansible.builtin.meta: flush_handlers
 
 # --------------------------------------------------
 # Postgres Readonly user
 # --------------------------------------------------
 - name: Postgres Read-only user
+  tags:
+    - "db_remote_ro_user"
   when: item.value.odoo_remote_db_access | default(false)
   block:
       - name: Allow readonly user connection to prod db (with userns_remap)
diff --git a/tasks/main.yml b/tasks/main.yml
index 3e32ade..ffb2478 100644
--- a/tasks/main.yml
+++ b/tasks/main.yml
@@ -4,6 +4,7 @@
 # --------------------------------------------------
 
 - name: Copy docker compose for whitelists
+  tags: "whitelists"
   ansible.builtin.template:
       src: whitelists.yaml.j2
       dest: "/home/docker/whitelists.yaml"
@@ -14,10 +15,11 @@
   when: restrict_internet_access and whitelisted_urls | default([])
 
 - name: NON-PROD instance setup
+  tags:
+    - "always"
+    - "odoo_nonprod"
   ansible.builtin.include_tasks:
       file: instance.yml
-      apply:
-          tags: backup_odoo
   with_dict: "{{ odoo_instances }}"
   loop_control:
       label: "{{ item.key }}"
@@ -26,10 +28,11 @@
   when: not (item.value.prod_instance | default(false) == item.key) and (odoo_instance is undefined or item.key == odoo_instance)
 
 - name: PROD instance setup
+  tags:
+    - "always"
+    - "odoo_prod"
   ansible.builtin.include_tasks:
       file: instance.yml
-      apply:
-          tags: backup_odoo
   with_dict: "{{ odoo_instances }}"
   loop_control:
       label: "{{ item.key }}"
@@ -41,6 +44,7 @@
 # Remote imports section
 # --------------------------------------------------
 - name: Remote Imports
+  tags: "remote_imports"
   block:
       - name: Push private keys for any external tool connection
         when: private_keys is defined
-- 
GitLab