Merge "logrotate: don't use filename to generate config file"

This commit is contained in:
Zuul 2023-04-28 00:57:48 +00:00 committed by Gerrit Code Review
commit 9b842f48f0
3 changed files with 20 additions and 6 deletions

View File

@ -14,12 +14,15 @@ not an exhaustive list of directives (contributions are welcome).
.. zuul:rolevar:: logrotate_file_name
The log file on disk to rotate
The full path to log file on disk to rotate. May be a wild-card;
e.g. ``/var/log/progname/*.log``.
.. zuul:rolevar:: logrotate_config_file_name
:default: Unique name based on :zuul:rolevar::`logrotate.logrotate_file_name`
:default: Unique name based on the hash of :zuul:rolevar::`logrotate.logrotate_file_name`
The name of the configuration file in ``/etc/logrotate.d``
The name of the configuration file in ``/etc/logrotate.d``. If
this is specified, it is up to the caller to ensure it is unique
across all calls of this role.
.. zuul:rolevar:: logrotate_compress
:default: yes

View File

@ -15,10 +15,19 @@
when: logrotate_frequency == 'size'
# Hash the full path to avoid any conflicts but remain idempotent.
# "/var/log/ansible/ansible.log" becomes "ansible.log.37237.conf" for example
- name: Create a unique config name
set_fact:
logrotate_generated_config_file_name: "{{ logrotate_file_name | basename }}.{{ (logrotate_file_name|hash('sha1'))[0:5] }}.conf"
# NOTE(ianw) 2023-02-13 : we missed that this makes files with
# names like "*.1234.conf" when using wild-cards. Below we have
# dropped using the file-name component. After we've removed them
# we can drop this.
_old_logrotate_generated_config_file_name: "{{ logrotate_file_name | basename }}.{{ (logrotate_file_name|hash('sha1'))[0:5] }}.conf"
logrotate_generated_config_file_name: "{{ (logrotate_file_name | hash('sha1'))[0:6] }}.conf"
- name: Clear out potentially confusing config files
file:
state: absent
path: '{{ _old_logrotate_generated_config_file_name }}'
- name: 'Install {{ logrotate_file_name }} rotatation config file'
template:

View File

@ -130,7 +130,9 @@ def test_logrotate(host):
'''
ansible_vars = host.ansible.get_variables()
if ansible_vars['inventory_hostname'].startswith('bridge'):
cfg_file = host.file("/etc/logrotate.d/ansible.log.37237.conf")
# Generated for idempotence by logrotate role; hash of
# "/var/log/ansible/ansible.log"
cfg_file = host.file("/etc/logrotate.d/372374.conf")
assert cfg_file.exists
assert cfg_file.contains('/var/log/ansible/ansible.log')