ctucx.git: ansible-configs

My personal ansible roles and playbooks [deprecated in favor of nixos]

commit 07b6a9ac020e6a0334fcfcfd3ab61f61edf78e07
parent ee571466183b6acad887036ea5a8c3e0cf094fbb
Author: Leah (ctucx) <leah@ctu.cx>
Date: Sun, 21 Feb 2021 22:06:05 +0100

roles/dnsmasq: split tasks into multiple files
6 files changed, 118 insertions(+), 107 deletions(-)
A
roles/dnsmasq/handlers/main.yml
|
17
+++++++++++++++++
A
roles/dnsmasq/tasks/configure.yml
|
9
+++++++++
A
roles/dnsmasq/tasks/install.yml
|
20
++++++++++++++++++++
M
roles/dnsmasq/tasks/main.yml
|
121
++++++++++---------------------------------------------------------------------
A
roles/dnsmasq/tasks/remove.yml
|
40
++++++++++++++++++++++++++++++++++++++++
A
roles/dnsmasq/tasks/start.yml
|
18
++++++++++++++++++
diff --git a/roles/dnsmasq/handlers/main.yml b/roles/dnsmasq/handlers/main.yml
@@ -0,0 +1,17 @@
+---
+
+- name: "[OpenRC] Restart service: dnsmasq (to deploy config changes)"
+  service:
+    name: dnsmasq
+    state: restarted
+  when:
+    - ansible_service_mgr == "openrc"
+  listen: "Restart dnsmasq"
+
+- name: "[systemd] Restart service: dnsmasq (to deploy config changes)"
+  systemd:
+    name: dnsmasq
+    state: restarted
+  when:
+    - ansible_service_mgr == "systemd"
+  listen: "Restart dnsmasq"
diff --git a/roles/dnsmasq/tasks/configure.yml b/roles/dnsmasq/tasks/configure.yml
@@ -0,0 +1,8 @@
+---
+
+- name: "Create file: /etc/dnsmasq.conf"
+  template:
+    src: dnsmasq.conf.j2
+    dest: /etc/dnsmasq.conf
+    validate: /usr/bin/dnsmasq --test -C %s
+  notify: Restart dnsmasq+
\ No newline at end of file
diff --git a/roles/dnsmasq/tasks/install.yml b/roles/dnsmasq/tasks/install.yml
@@ -0,0 +1,19 @@
+---
+
+- name: "[Alpine] Install package: dnsmasq" 
+  apk:
+    name: dnsmasq 
+    state: present
+    update_cache: yes
+  when:
+    - ansible_distribution == "Alpine"
+  notify: Restart dnsmasq
+
+- name: "[Archlinux] Install package: dnsmasq" 
+  pacman:
+    name: dnsmasq 
+    state: present
+    update_cache: yes
+  when:
+    - ansible_distribution == "Archlinux"
+  notify: Restart dnsmasq+
\ No newline at end of file
diff --git a/roles/dnsmasq/tasks/main.yml b/roles/dnsmasq/tasks/main.yml
@@ -1,117 +1,24 @@
 ---
 
-# install it 
-- name: "[Alpine] Install package: dnsmasq" 
-  apk:
-    name: dnsmasq 
-    state: present
-    update_cache: yes
-  when: 
-    - ansible_distribution == "Alpine" 
+- include: install.yml
+  when:
+    - services.dnsmasq.enable is defined
     - services.dnsmasq.enable is true
 
-- name: "[Archlinux] Install package: dnsmasq" 
-  pacman:
-    name: dnsmasq 
-    state: present
-    update_cache: yes
-  when: 
-    - ansible_distribution == "Archlinux" 
+- include: configure.yml
+  when:
+    - services.dnsmasq.enable is defined
     - services.dnsmasq.enable is true
 
-
-# configure it 
-
-- name: "Create file: /etc/dnsmasq.conf"
-  template:
-    src: dnsmasq.conf.j2
-    dest: /etc/dnsmasq.conf
-  register: createConfig
-  when: 
-    - services.dnsmasq.enable is true
-
-
-# (re)start it
-
-- name: "[OpenRC] Enable and start service: dnsmasq"
-  service:
-    name: dnsmasq
-    enabled: yes
-    state: started
-  when: 
-    - ansible_service_mgr == "openrc"
-    - services.dnsmasq.enable is true
-
-- name: "[systemd] Enable and start service: dnsmasq"
-  systemd:
-    name: dnsmasq
-    enabled: yes
-    state: started
-  when: 
-    - ansible_service_mgr == "systemd"
-    - services.dnsmasq.enable is true
-
-- name: "[OpenRC] Restart service: dnsmasq"
-  service:
-    name: dnsmasq
-    state: restarted
-  when: 
-    - ansible_service_mgr == "openrc"
-    - services.dnsmasq.enable is true
-    - createConfig.changed
-
-- name: "[systemd] Restart service: dnsmasq"
-  systemd:
-    name: dnsmasq
-    state: restarted
-  when: 
-    - ansible_service_mgr == "systemd"
+- include: start.yml
+  when:
+    - services.dnsmasq.enable is defined
     - services.dnsmasq.enable is true
-    - createConfig.changed
 
+- name: Run handlers
+  meta: flush_handlers
 
-# stop it 
-
-- name: "[OpenRC] Disable and stop service: dnsmasq"
-  service:
-    name: dnsmasq
-    enabled: no
-    state: stopped
-  when: 
-    - ansible_service_mgr == "openrc"
-    - services.dnsmasq.enable is false
-
-- name: "[systemd] Disable and stop service: dnsmasq"
-  systemd:
-    name: dnsmasq
-    enabled: no
-    state: stopped
-  when: 
-    - ansible_service_mgr == "systemd"
-    - services.dnsmasq.enable is false
-
-
-# remove it
-
-- name: "[Alpine] Remove package: dnsmasq" 
-  apk:
-    name: dnsmasq 
-    state: absent
-  when: 
-    - ansible_distribution == "Alpine" 
-    - services.dnsmasq.enable is false
-
-- name: "[Archlinux] Remove package: dnsmasq" 
-  pacman:
-    name: dnsmasq 
-    state: absent
-  when: 
-    - ansible_distribution == "Archlinux" 
-    - services.dnsmasq.enable is false
-
-- name: "Remove file: /etc/dnsmasq.conf"
-  file:
-    path: /etc/dnsmasq.conf
-    state: absent
-  when: 
+- include: remove.yml
+  when:
+    - services.dnsmasq.enable is defined
     - services.dnsmasq.enable is false
diff --git a/roles/dnsmasq/tasks/remove.yml b/roles/dnsmasq/tasks/remove.yml
@@ -0,0 +1,40 @@
+---
+
+- name: "[OpenRC] Disable and stop service: dnsmasq"
+  service:
+    name: dnsmasq
+    enabled: no
+    state: stopped
+  when:
+    - ansible_service_mgr == "openrc"
+
+- name: "[systemd] Disable and stop service: dnsmasq"
+  systemd:
+    name: dnsmasq
+    enabled: no
+    state: stopped
+  when:
+    - ansible_service_mgr == "systemd"
+
+
+- name: "[Alpine] Remove package: dnsmasq" 
+  apk:
+    name: dnsmasq 
+    state: absent
+  when: 
+    - ansible_distribution == "Alpine" 
+
+- name: "[Archlinux] Remove package: dnsmasq" 
+  pacman:
+    name: dnsmasq 
+    state: absent
+  when:
+    - ansible_distribution == "Archlinux" 
+
+- name: "Delete leftovers"
+  file:
+    path: "{{item}}"
+    state: absent
+  with_items:
+    - /etc/dnsmasq.conf
+    - /var/lib/misc/dnsmasq.leases
diff --git a/roles/dnsmasq/tasks/start.yml b/roles/dnsmasq/tasks/start.yml
@@ -0,0 +1,17 @@
+---
+
+- name: "[OpenRC] Enable and start service: dnsmasq"
+  service:
+    name: dnsmasq
+    enabled: yes
+    state: started
+  when:
+    - ansible_service_mgr == "openrc"
+
+- name: "[systemd] Enable and start service: dnsmasq"
+  systemd:
+    name: dnsmasq
+    enabled: yes
+    state: started
+  when:
+    - ansible_service_mgr == "systemd"+
\ No newline at end of file