From 9d43bd9e52701c477ba57d6a0275a6b259a183f8 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Tue, 30 Jul 2019 21:27:32 +0300 Subject: [PATCH] Refactor: Join tasks into blocks to factor out conditions --- .../roles/add-apache-vhost/tasks/main.yml | 11 ++-- .../roles/add-dns-domain/tasks/main.yml | 50 +++++++-------- playbooks/debian/roles/apache/tasks/main.yml | 58 +++++++++-------- .../debian/roles/dehydrated/tasks/main.yml | 44 ++++++------- .../roles/init-system/tasks/locales.yml | 31 +++++----- playbooks/debian/roles/named/tasks/main.yml | 49 +++++++-------- .../roles/remove-systemd/tasks/main.yml | 13 ++-- playbooks/debian/roles/root/tasks/mc.yml | 62 +++++++++---------- 8 files changed, 154 insertions(+), 164 deletions(-) diff --git a/playbooks/debian/roles/add-apache-vhost/tasks/main.yml b/playbooks/debian/roles/add-apache-vhost/tasks/main.yml index 2f0e03a..511a676 100644 --- a/playbooks/debian/roles/add-apache-vhost/tasks/main.yml +++ b/playbooks/debian/roles/add-apache-vhost/tasks/main.yml @@ -4,10 +4,9 @@ register: vhost_conf changed_when: not vhost_conf.stat.exists -- name: Add vhost - import_tasks: add-vhost.yml - when: not vhost_conf.stat.exists - -- name: Run dehydrated for the vhost - import_tasks: dehydrated.yml +- block: + - name: Add vhost + import_tasks: add-vhost.yml + - name: Run dehydrated for the vhost + import_tasks: dehydrated.yml when: not vhost_conf.stat.exists diff --git a/playbooks/debian/roles/add-dns-domain/tasks/main.yml b/playbooks/debian/roles/add-dns-domain/tasks/main.yml index 88a49c2..a8c93fb 100644 --- a/playbooks/debian/roles/add-dns-domain/tasks/main.yml +++ b/playbooks/debian/roles/add-dns-domain/tasks/main.yml @@ -4,33 +4,31 @@ register: domain_exists changed_when: not domain_exists.stat.exists -- name: Copy domain template - become: true - template: - src: domain - dest: "/etc/bind/{{ domain }}" - owner: bind - group: bind - mode: '0600' - force: no - when: not domain_exists.stat.exists - -- name: Update config - become: true - shell: | - echo 'zone "{{ domain }}" { - type master; - file "/etc/bind/{{ domain }}"; - allow-query { any; }; - notify yes; - };' >> /etc/bind/named.conf.local +- block: + - name: Copy domain template + become: true + template: + src: domain + dest: "/etc/bind/{{ domain }}" + owner: bind + group: bind + mode: '0600' + force: no + - name: Update config + become: true + shell: | + echo 'zone "{{ domain }}" { + type master; + file "/etc/bind/{{ domain }}"; + allow-query { any; }; + notify yes; + };' >> /etc/bind/named.conf.local - when: not domain_exists.stat.exists -- name: Reload BIND - become: true - service: - name: bind9 - state: reloaded + - name: Reload BIND + become: true + service: + name: bind9 + state: reloaded when: not domain_exists.stat.exists diff --git a/playbooks/debian/roles/apache/tasks/main.yml b/playbooks/debian/roles/apache/tasks/main.yml index 5cd209a..c05eb6f 100644 --- a/playbooks/debian/roles/apache/tasks/main.yml +++ b/playbooks/debian/roles/apache/tasks/main.yml @@ -8,39 +8,37 @@ msg: "apache has already been configured" when: phd_conf.stat.exists -- name: Install apache - become: true - apt: - autoclean: yes - autoremove: yes - install_recommends: no - name: apache2 - purge: yes - state: latest - update_cache: yes - when: not phd_conf.stat.exists +- block: + - name: Install apache + become: true + apt: + autoclean: yes + autoremove: yes + install_recommends: no + name: apache2 + purge: yes + state: latest + update_cache: yes -- name: Enable SSL module - become: true - command: a2enmod ssl - notify: Reload apache - when: not phd_conf.stat.exists + - name: Enable SSL module + become: true + command: a2enmod ssl + notify: Reload apache -- name: Configure apache - become: true - template: - src: 001-phd.conf - dest: /etc/apache2/conf-available - owner: root - group: root - mode: '0640' - force: no - when: not phd_conf.stat.exists + - name: Configure apache + become: true + template: + src: 001-phd.conf + dest: /etc/apache2/conf-available + owner: root + group: root + mode: '0640' + force: no -- name: Enable config - become: true - command: a2enconf 001-phd - notify: Reload apache + - name: Enable config + become: true + command: a2enconf 001-phd + notify: Reload apache when: not phd_conf.stat.exists - name: Configure logrotate diff --git a/playbooks/debian/roles/dehydrated/tasks/main.yml b/playbooks/debian/roles/dehydrated/tasks/main.yml index 87ebe8f..3405ff3 100644 --- a/playbooks/debian/roles/dehydrated/tasks/main.yml +++ b/playbooks/debian/roles/dehydrated/tasks/main.yml @@ -5,30 +5,30 @@ register: dehydrated changed_when: not dehydrated.stat.exists -- name: Install git - local_action: - module: apt - autoclean: yes - autoremove: yes - install_recommends: no - name: git - purge: yes - state: latest - update_cache: yes - when: not dehydrated.stat.exists +- block: + - name: Install git + become: true + local_action: + module: apt + autoclean: yes + autoremove: yes + install_recommends: no + name: git + purge: yes + state: latest + update_cache: yes -- name: Prepare to clone dehydrated - local_action: - module: file - path: /usr/local/src/LetsEncrypt - state: directory - when: not dehydrated.stat.exists + - name: Prepare to clone dehydrated + local_action: + module: file + path: /usr/local/src/LetsEncrypt + state: directory -- name: Clone dehydrated - local_action: - module: git - repo: https://github.com/lukas2511/dehydrated.git - dest: /usr/local/src/LetsEncrypt/dehydrated + - name: Clone dehydrated + local_action: + module: git + repo: https://github.com/lukas2511/dehydrated.git + dest: /usr/local/src/LetsEncrypt/dehydrated when: not dehydrated.stat.exists - name: Prepare the server to syncronize dehydrated diff --git a/playbooks/debian/roles/init-system/tasks/locales.yml b/playbooks/debian/roles/init-system/tasks/locales.yml index 8143407..8351b60 100644 --- a/playbooks/debian/roles/init-system/tasks/locales.yml +++ b/playbooks/debian/roles/init-system/tasks/locales.yml @@ -7,20 +7,19 @@ msg: "Locales have already been configured" when: has_locales.stdout != "0" -- name: Configure locales - become: true - lineinfile: - path: /etc/locale.gen - regexp: "^{{ item }}" - line: "{{ item }}" - loop: [ - 'en_US ISO-8859-1', - 'ru_RU.KOI8-R KOI8-R', - 'ru_RU.UTF-8 UTF-8', - ] - when: has_locales.stdout == "0" - -- name: Regenerate locales - become: true - command: /usr/sbin/locale-gen +- block: + - name: Configure locales + become: true + lineinfile: + path: /etc/locale.gen + regexp: "^{{ item }}" + line: "{{ item }}" + loop: [ + 'en_US ISO-8859-1', + 'ru_RU.KOI8-R KOI8-R', + 'ru_RU.UTF-8 UTF-8', + ] + - name: Regenerate locales + become: true + command: /usr/sbin/locale-gen when: has_locales.stdout == "0" diff --git a/playbooks/debian/roles/named/tasks/main.yml b/playbooks/debian/roles/named/tasks/main.yml index b39eda3..8c6b7d0 100644 --- a/playbooks/debian/roles/named/tasks/main.yml +++ b/playbooks/debian/roles/named/tasks/main.yml @@ -8,31 +8,30 @@ msg: "BIND has already been configured" when: named_conf.stdout not in ('', "0") -- name: Install BIND - become: true - apt: - autoclean: yes - autoremove: yes - install_recommends: no - name: bind9 - purge: yes - state: latest - update_cache: yes - when: named_conf.stdout in ('', "0") +- block: + - name: Install BIND + become: true + apt: + autoclean: yes + autoremove: yes + install_recommends: no + name: bind9 + purge: yes + state: latest + update_cache: yes -- name: Configure BIND - become: true - copy: - src: named.conf.options - dest: /etc/bind/named.conf.options - owner: bind - group: bind - mode: '0600' - when: named_conf.stdout in ('', "0") + - name: Configure BIND + become: true + copy: + src: named.conf.options + dest: /etc/bind/named.conf.options + owner: bind + group: bind + mode: '0600' -- name: Reload BIND - become: true - service: - name: bind9 - state: reloaded + - name: Reload BIND + become: true + service: + name: bind9 + state: reloaded when: named_conf.stdout in ('', "0") diff --git a/playbooks/debian/roles/remove-systemd/tasks/main.yml b/playbooks/debian/roles/remove-systemd/tasks/main.yml index 76ff125..1739c6c 100644 --- a/playbooks/debian/roles/remove-systemd/tasks/main.yml +++ b/playbooks/debian/roles/remove-systemd/tasks/main.yml @@ -33,11 +33,10 @@ - name: Purge SystemD import_tasks: remove-systemd.yml -- name: Reboot if SystemD is still active - become: true - reboot: - when: "'rc' in purged and purged.rc != 0" - -- name: Purge SystemD after reboot - import_tasks: remove-systemd.yml +- block: + - name: Reboot if SystemD is still active + become: true + reboot: + - name: Purge SystemD after reboot + import_tasks: remove-systemd.yml when: "'rc' in purged and purged.rc != 0" diff --git a/playbooks/debian/roles/root/tasks/mc.yml b/playbooks/debian/roles/root/tasks/mc.yml index 332015e..c27e80e 100644 --- a/playbooks/debian/roles/root/tasks/mc.yml +++ b/playbooks/debian/roles/root/tasks/mc.yml @@ -10,41 +10,39 @@ msg: "mc links have already been created" when: links_exist.results|selectattr('stat.exists')|selectattr('stat.islnk')|list|length == 3 -- name: "Setup root mc - create directories for mc links" - become: true - file: - path: "~root/{{ item }}" - state: directory - mode: "0700" - loop: ['.cache', '.config', '.local/share'] - when: links_exist.results|selectattr('stat.exists')|selectattr('stat.islnk')|list|length != 3 +- block: + - name: "Setup root mc - create directories for mc links" + become: true + file: + path: "~root/{{ item }}" + state: directory + mode: "0700" + loop: ['.cache', '.config', '.local/share'] -- name: "Setup root mc - remove mc directories" - become: true - file: - path: "~root/{{ item }}" - state: absent - loop: ['.cache/mc', '.config/mc', '.local/share/mc'] - when: links_exist.results|selectattr('stat.exists')|selectattr('stat.islnk')|list|length != 3 + - name: "Setup root mc - remove mc directories" + become: true + file: + path: "~root/{{ item }}" + state: absent + loop: ['.cache/mc', '.config/mc', '.local/share/mc'] -- name: "Setup root mc - link mc directories (1)" - become: true - file: - path: "~root/{{ item }}" - state: link - src: "../.mc" - force: yes - loop: ['.cache/mc', '.config/mc'] - when: links_exist.results|selectattr('stat.exists')|selectattr('stat.islnk')|list|length != 3 + - name: "Setup root mc - link mc directories (1)" + become: true + file: + path: "~root/{{ item }}" + state: link + src: "../.mc" + force: yes + loop: ['.cache/mc', '.config/mc'] -- name: "Setup root mc - link mc directories (2)" - become: true - file: - path: "~root/{{ item }}" - state: link - src: "../../.mc" - force: yes - loop: ['.local/share/mc'] + - name: "Setup root mc - link mc directories (2)" + become: true + file: + path: "~root/{{ item }}" + state: link + src: "../../.mc" + force: yes + loop: ['.local/share/mc'] when: links_exist.results|selectattr('stat.exists')|selectattr('stat.islnk')|list|length != 3 - name: "Setup root mc - overwrite files from ~phd/admin" -- 2.39.2