Skip to content

Commit 522da0a

Browse files
committed
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.
1 parent 531e876 commit 522da0a

File tree

5 files changed

+39
-9
lines changed

5 files changed

+39
-9
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,8 @@ These variables are set in the same level of the `logging_inputs`, `logging_outp
404404
- `logging_mark`: Mark message periodically by immark, if set to `true`. Default to `false`.
405405
- `logging_mark_interval`: Interval for `logging_mark` in seconds. Default to `3600`.
406406
- `logging_purge_confs`: `true` or `false`. If set to `true`, files in /etc/rsyslog.d are purged.
407+
- `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`.
408+
- `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.
407409
- `logging_system_log_dir`: Directory where the local log output files are placed. Default to `/var/log`.
408410
409411
### Update and Delete

defaults/main.yml

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ logging_flows: []
1717
# If logging_purge_confs is set to true, existing config files will be purged.
1818
logging_purge_confs: false
1919

20+
# Variable to specify the directory to store backup file which contains pre-
21+
# logging system role run. The backup is used when logging_restore_confs is
22+
# set to true.
23+
# Default to '/var/lib/rsyslog' for rsyslog.
24+
logging_backup_dir: ""
25+
26+
# If logging_restore_confs is set to true, backed up config files are restored.
27+
logging_restore_confs: false
28+
2029
# Variable to specify the directory to put the local output files to store logs.
2130
logging_system_log_dir: /var/log
2231

roles/rsyslog/tasks/main.yml

+26-9
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,42 @@
7474
state: directory
7575
path: '{{ __rsyslog_backup_dir }}'
7676
mode: '0700'
77-
changed_when: false
7877

79-
- name: Archive the contents of {{ __rsyslog_config_dir }} to
80-
the backup dir
78+
- name: Get backup file stat
79+
stat:
80+
path: "{{ __rsyslog_backup_dir }}/backup.tgz"
81+
register: __backup_stat
82+
83+
- name: Archive the original contents of {{ __rsyslog_config_dir }}
84+
to the backup dir
8185
command: >
8286
tar -cvzPf "{{ __rsyslog_backup_dir }}/backup.tgz"
83-
/etc/rsyslog.conf "{{ __rsyslog_config_dir }}"
84-
args:
85-
warn: false # suppress 'unarchive' warning
86-
changed_when: false
87+
/etc/rsyslog.conf "{{ __rsyslog_config_dir }}"
88+
when: not __backup_stat.stat.exists
8789

8890
- name: Purge original conf
8991
file:
9092
state: absent
9193
path: "{{ __rsyslog_config_dir }}/*"
9294
when: __rsyslog_purge_original_conf | bool | d(false)
95+
96+
- block:
97+
- name: Get backup file stat
98+
stat:
99+
path: "{{ __rsyslog_backup_dir }}/backup.tgz"
100+
register: __backup_stat
101+
102+
- name: Restore original conf
103+
command: >
104+
tar -xvzPf "{{ __rsyslog_backup_dir }}/backup.tgz"
105+
when: __backup_stat.stat.exists
106+
when:
107+
- __rsyslog_restore_original_conf | bool | d(false)
108+
93109
vars:
94-
__rsyslog_backup_dir: '{{ rsyslog_backup_dir |
95-
d("/tmp") }}/rsyslog.d-{{ ansible_date_time.iso8601_basic_short }}'
110+
__rsyslog_backup_dir: '{{ logging_backup_dir
111+
if logging_backup_dir|length > 0
112+
else "/var/lib/rsyslog" }}'
96113

97114
- name: "Create logging directory if it does not exist or
98115
the ownership and/or modes are different."

tasks/main.yml

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
vars:
9797
__rsyslog_enabled: "{{ logging_enabled }}"
9898
__rsyslog_purge_original_conf: "{{ logging_purge_confs }}"
99+
__rsyslog_restore_original_conf: "{{ logging_restore_confs }}"
99100
__rsyslog_system_log_dir: "{{ logging_system_log_dir }}"
100101
include_role:
101102
name: "{{ role_path }}/roles/rsyslog"

tests/tests_basics_files.yml

+1
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@
386386

387387
- name: END TEST CASE 3; Clean up the deployed config
388388
vars:
389+
logging_restore_confs: true
389390
logging_enabled: false
390391
logging_outputs:
391392
- name: files_output0

0 commit comments

Comments
 (0)