From 522da0a0a504d50f7f4397ac36a038bdac390c0d Mon Sep 17 00:00:00 2001 From: Noriko Hosoi Date: Thu, 6 Jan 2022 15:33:10 -0800 Subject: [PATCH] Add logging_restore_confs variable to restore backup. Following the suggestion from @richm (Thank you!), generates a back up only once in /var/tmp/rsyslog, which stores the rsyslog logging files prior to running logging role. Remove "changed_when: false" from the backup dir and file creation tasks. --- README.md | 2 ++ defaults/main.yml | 9 +++++++++ roles/rsyslog/tasks/main.yml | 35 ++++++++++++++++++++++++++--------- tasks/main.yml | 1 + tests/tests_basics_files.yml | 1 + 5 files changed, 39 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c4c384ec..9925eed2 100644 --- a/README.md +++ b/README.md @@ -404,6 +404,8 @@ These variables are set in the same level of the `logging_inputs`, `logging_outp - `logging_mark`: Mark message periodically by immark, if set to `true`. Default to `false`. - `logging_mark_interval`: Interval for `logging_mark` in seconds. Default to `3600`. - `logging_purge_confs`: `true` or `false`. If set to `true`, files in /etc/rsyslog.d are purged. +- `logging_backup_dir`: Path to specify the directory to store backup file which contains pre-logging system role run. The backup is used when `logging_restore_confs` is set to `true`. +- `logging_restore_confs`: `true` or `false`. If set to `true`, config files in the backup file are restored. This variable is to be used in the cleaning up. - `logging_system_log_dir`: Directory where the local log output files are placed. Default to `/var/log`. ### Update and Delete diff --git a/defaults/main.yml b/defaults/main.yml index 72a53065..0016c394 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -17,6 +17,15 @@ logging_flows: [] # If logging_purge_confs is set to true, existing config files will be purged. logging_purge_confs: false +# Variable to specify the directory to store backup file which contains pre- +# logging system role run. The backup is used when logging_restore_confs is +# set to true. +# Default to '/var/lib/rsyslog' for rsyslog. +logging_backup_dir: "" + +# If logging_restore_confs is set to true, backed up config files are restored. +logging_restore_confs: false + # Variable to specify the directory to put the local output files to store logs. logging_system_log_dir: /var/log diff --git a/roles/rsyslog/tasks/main.yml b/roles/rsyslog/tasks/main.yml index 233ba323..5f9b7cc2 100644 --- a/roles/rsyslog/tasks/main.yml +++ b/roles/rsyslog/tasks/main.yml @@ -74,25 +74,42 @@ state: directory path: '{{ __rsyslog_backup_dir }}' mode: '0700' - changed_when: false - - name: Archive the contents of {{ __rsyslog_config_dir }} to - the backup dir + - name: Get backup file stat + stat: + path: "{{ __rsyslog_backup_dir }}/backup.tgz" + register: __backup_stat + + - name: Archive the original contents of {{ __rsyslog_config_dir }} + to the backup dir command: > tar -cvzPf "{{ __rsyslog_backup_dir }}/backup.tgz" - /etc/rsyslog.conf "{{ __rsyslog_config_dir }}" - args: - warn: false # suppress 'unarchive' warning - changed_when: false + /etc/rsyslog.conf "{{ __rsyslog_config_dir }}" + when: not __backup_stat.stat.exists - name: Purge original conf file: state: absent path: "{{ __rsyslog_config_dir }}/*" when: __rsyslog_purge_original_conf | bool | d(false) + + - block: + - name: Get backup file stat + stat: + path: "{{ __rsyslog_backup_dir }}/backup.tgz" + register: __backup_stat + + - name: Restore original conf + command: > + tar -xvzPf "{{ __rsyslog_backup_dir }}/backup.tgz" + when: __backup_stat.stat.exists + when: + - __rsyslog_restore_original_conf | bool | d(false) + vars: - __rsyslog_backup_dir: '{{ rsyslog_backup_dir | - d("/tmp") }}/rsyslog.d-{{ ansible_date_time.iso8601_basic_short }}' + __rsyslog_backup_dir: '{{ logging_backup_dir + if logging_backup_dir|length > 0 + else "/var/lib/rsyslog" }}' - name: "Create logging directory if it does not exist or the ownership and/or modes are different." diff --git a/tasks/main.yml b/tasks/main.yml index 7384497e..37c9d1da 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -96,6 +96,7 @@ vars: __rsyslog_enabled: "{{ logging_enabled }}" __rsyslog_purge_original_conf: "{{ logging_purge_confs }}" + __rsyslog_restore_original_conf: "{{ logging_restore_confs }}" __rsyslog_system_log_dir: "{{ logging_system_log_dir }}" include_role: name: "{{ role_path }}/roles/rsyslog" diff --git a/tests/tests_basics_files.yml b/tests/tests_basics_files.yml index ac04a282..1bee8455 100644 --- a/tests/tests_basics_files.yml +++ b/tests/tests_basics_files.yml @@ -386,6 +386,7 @@ - name: END TEST CASE 3; Clean up the deployed config vars: + logging_restore_confs: true logging_enabled: false logging_outputs: - name: files_output0