From 32266b707ada32788d608fa6fbe7f1de623f2755 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sat, 27 Jul 2019 16:18:56 +0300 Subject: [PATCH] Feat: Use module `stat` instead of command `test` --- .../roles/add-apache-vhost/tasks/main.yml | 11 +++++------ playbooks/debian/roles/apache/tasks/main.yml | 17 ++++++++--------- .../roles/install-dehydrated/tasks/main.yml | 14 +++++++------- playbooks/debian/roles/phd/tasks/main.yml | 12 ++++++------ playbooks/debian/roles/root/tasks/mc.yml | 17 ++++++++--------- 5 files changed, 34 insertions(+), 37 deletions(-) diff --git a/playbooks/debian/roles/add-apache-vhost/tasks/main.yml b/playbooks/debian/roles/add-apache-vhost/tasks/main.yml index 0133626..666cd77 100644 --- a/playbooks/debian/roles/add-apache-vhost/tasks/main.yml +++ b/playbooks/debian/roles/add-apache-vhost/tasks/main.yml @@ -1,13 +1,12 @@ - name: Check vhost - command: "test -L /etc/apache2/sites-enabled/{{ virtual_host }}.conf" - failed_when: false - register: vhost - changed_when: vhost.rc != 0 + stat: + path: "/etc/apache2/sites-enabled/{{ virtual_host }}.conf" + register: vhost_conf - name: Add vhost import_tasks: add-vhost.yml - when: vhost.rc != 0 + when: not vhost_conf.stats.exists - name: Run dehydrated for the vhost import_tasks: dehydrated.yml - when: vhost.rc != 0 + when: not vhost_conf.stats.exists diff --git a/playbooks/debian/roles/apache/tasks/main.yml b/playbooks/debian/roles/apache/tasks/main.yml index f0481d9..b289d94 100644 --- a/playbooks/debian/roles/apache/tasks/main.yml +++ b/playbooks/debian/roles/apache/tasks/main.yml @@ -1,12 +1,11 @@ - name: Check apache - command: test -L /etc/apache2/conf-enabled/001-phd.conf - failed_when: false - register: apache - changed_when: apache.rc != 0 + stat: + path: /etc/apache2/conf-enabled/001-phd.conf + register: phd_conf - debug: msg: "apache has already been configured" - when: apache.rc == 0 + when: not phd_conf.stats.exists - name: Install apache become: true @@ -18,13 +17,13 @@ purge: yes state: latest update_cache: yes - when: apache.rc != 0 + when: not phd_conf.stats.exists - name: Enable SSL module become: true command: a2enmod ssl notify: Reload apache - when: apache.rc != 0 + when: not phd_conf.stats.exists - name: Configure apache become: true @@ -35,13 +34,13 @@ group: root mode: '0640' force: no - when: apache.rc != 0 + when: not phd_conf.stats.exists - name: Enable config become: true command: a2enconf 001-phd notify: Reload apache - when: apache.rc != 0 + when: not phd_conf.stats.exists - name: Configure logrotate become: true diff --git a/playbooks/debian/roles/install-dehydrated/tasks/main.yml b/playbooks/debian/roles/install-dehydrated/tasks/main.yml index dbd994c..7672bc8 100644 --- a/playbooks/debian/roles/install-dehydrated/tasks/main.yml +++ b/playbooks/debian/roles/install-dehydrated/tasks/main.yml @@ -1,8 +1,8 @@ - name: Test if dehydrated is already cloned - local_action: command test -f /usr/local/src/LetsEncrypt/dehydrated/.git/config - failed_when: false - register: dehydrated_exist - changed_when: dehydrated_exist.rc != 0 + local_action: + module: stat + path: /usr/local/src/LetsEncrypt/dehydrated/.git/config + register: dehydrated - name: Install git local_action: @@ -14,21 +14,21 @@ purge: yes state: latest update_cache: yes - when: dehydrated_exist.rc != 0 + when: dehydrated.stat.exists - name: Prepare to clone dehydrated local_action: module: file path: /usr/local/src/LetsEncrypt state: directory - when: dehydrated_exist.rc != 0 + when: dehydrated.stat.exists - name: Clone dehydrated local_action: module: git repo: https://github.com/lukas2511/dehydrated.git dest: /usr/local/src/LetsEncrypt - when: dehydrated_exist.rc != 0 + when: dehydrated.stat.exists - name: Prepare the server to syncronize dehydrated become: true diff --git a/playbooks/debian/roles/phd/tasks/main.yml b/playbooks/debian/roles/phd/tasks/main.yml index 7ab7ba1..4c00ec5 100644 --- a/playbooks/debian/roles/phd/tasks/main.yml +++ b/playbooks/debian/roles/phd/tasks/main.yml @@ -1,12 +1,12 @@ - name: Test if user phd already exists - command: test -f ~/.profile -a -f ~/.shellrc - failed_when: false - register: phd_exist - changed_when: phd_exist.rc != 0 + stat: + path: "{{ item }}" + register: phd_exists + loop: ['~/.profile', '~/.shellrc'] - debug: msg: "User phd has already been created" - when: phd_exist.rc == 0 + when: phd_exists.results|selectattr('stat.exists')|list|length == 2 - name: Create and setup user phd block: @@ -16,4 +16,4 @@ unarchive: src: ~/archive/STORE/phd/Home/phd.tar.bz2 dest: /home - when: phd_exist.rc != 0 + when: phd_exists.results|selectattr('stat.exists')|list|length == 2 diff --git a/playbooks/debian/roles/root/tasks/mc.yml b/playbooks/debian/roles/root/tasks/mc.yml index 655514a..dbb78ba 100644 --- a/playbooks/debian/roles/root/tasks/mc.yml +++ b/playbooks/debian/roles/root/tasks/mc.yml @@ -1,13 +1,13 @@ - name: "Setup root mc - check mc links" become: true - command: test -L ~root/.cache/mc -a -L ~root/.config/mc -a -L ~root/.local/share/mc - failed_when: false + stat: + path: "{{ item }}" register: links_exist - changed_when: links_exist.rc != 0 + loop: ['~root/.cache/mc', '~root/.config/mc', '~root/.local/share/mc'] - debug: msg: "mc links have already been created" - when: links_exist.rc == 0 + when: links_exist.results|selectattr('stat.islnk')|list|length == 3 - name: "Setup root mc - create directories for mc links" become: true @@ -16,7 +16,7 @@ state: directory mode: "0700" loop: ['.cache', '.config', '.local/share'] - when: links_exist.rc != 0 + when: links_exist.results|selectattr('stat.islnk')|list|length == 3 - name: "Setup root mc - remove mc directories" become: true @@ -24,7 +24,7 @@ path: "~root/{{ item }}" state: absent loop: ['.cache/mc', '.config/mc', '.local/share/mc'] - when: links_exist.rc != 0 + when: links_exist.results|selectattr('stat.islnk')|list|length == 3 - name: "Setup root mc - link mc directories (1)" become: true @@ -34,7 +34,7 @@ src: "../.mc" force: yes loop: ['.cache/mc', '.config/mc'] - when: links_exist.rc != 0 + when: links_exist.results|selectattr('stat.islnk')|list|length == 3 - name: "Setup root mc - link mc directories (2)" become: true @@ -44,7 +44,7 @@ src: "../../.mc" force: yes loop: ['.local/share/mc'] - when: links_exist.rc != 0 + when: links_exist.results|selectattr('stat.islnk')|list|length == 3 - name: "Setup root mc - overwrite files from ~phd/admin" become: true @@ -57,4 +57,3 @@ mode: "0600" force: yes loop: ['hotlist', 'ini', 'panels.ini'] - -- 2.39.2