{"id":3717,"date":"2025-03-08T09:58:49","date_gmt":"2025-03-08T08:58:49","guid":{"rendered":"https:\/\/kudzia.eu\/b\/?p=3717"},"modified":"2025-04-07T09:35:47","modified_gmt":"2025-04-07T08:35:47","slug":"ansible-playbook-increasing-log-log-retention-period","status":"publish","type":"post","link":"https:\/\/kudzia.eu\/b\/2025\/03\/ansible-playbook-increasing-log-log-retention-period\/","title":{"rendered":"ansible playbook increasing log retention period"},"content":{"rendered":"\n<p>i needed to adjust local log retention period on bunch of servers. rules:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>touch only logs which have daily or weekly rotation period,<\/li>\n\n\n\n<li>if it&#8217;s shorter than ~1 month &#8211; change it to 30 days<\/li>\n\n\n\n<li>otherwise &#8211; don&#8217;t change anything<\/li>\n<\/ul>\n\n\n\n<p>with some help from chatgpt and grog3 i&#8217;ve cobbled this one:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>- name: Ensure all logrotate configs with 'daily' have 'rotate 30'\n  hosts: all\n  tasks:\n    - name: Find files containing 'daily' in \/etc\/logrotate.d\n      ansible.builtin.find:\n        paths: \/etc\/logrotate.d\n        patterns: '*'\n        contains: '.*(daily).*'\n      register: find_daily\n    - name: Find files with low value for rate in \/etc\/logrotate.d for daily\n      ansible.builtin.find:\n        paths: \/etc\/logrotate.d\n        patterns: '*'\n        contains: '^\\s*rotate (&#91;0-9]|&#91;1-2]&#91;0-9])\\s*$'\n      register: find_low_retention_daily\n    - name: Set rotate to 30 in matched files\n      ansible.builtin.lineinfile:\n        path: \"{{ item }}\"\n        regexp: '^(\\s*)rotate\\s+\\d+'\n        line: '    rotate 30'\n        backrefs: yes\n      # if i used 'find_low_retention_daily | intersect(find_daily)' - it would work unreliabely, probably due to difference in atime\n      loop: \"{{ (find_low_retention_daily.files|map(attribute='path') |list ) | intersect( find_daily.files|map(attribute='path') |list ) }}\"\n\n    - name: Find files containing 'weekly' in \/etc\/logrotate.d\n      ansible.builtin.find:\n        paths: \/etc\/logrotate.d\n        patterns: '*'\n        contains: '.*(weekly).*'\n      register: find_weekly\n    - name: Find files with low value for rate in \/etc\/logrotate.d for weekly\n      ansible.builtin.find:\n        paths: \/etc\/logrotate.d\n        patterns: '*'\n        contains: '^\\s*rotate (&#91;1-4])\\s*$'\n      register: find_low_retention_weekly\n    - name: Set rotate to 30 in matched files\n      ansible.builtin.lineinfile:\n        path: \"{{ item }}\"\n        regexp: '^(\\s*)rotate\\s+\\d+'\n        line: '    rotate 5'\n        backrefs: yes\n      loop: \"{{ (find_low_retention_weekly.files|map(attribute='path') |list ) | intersect( find_weekly.files|map(attribute='path') |list ) }}\"\n\n#     some debug info\n#    - name: files in find_daily\n#      ansible.builtin.debug:\n#        msg: \"File found: {{ item }}\"\n#      loop: \"{{ find_daily.files }}\"\n\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>i needed to adjust local log retention period on bunch of servers. rules: with some help from chatgpt and grog3 i&#8217;ve cobbled this one:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[74],"class_list":["post-3717","post","type-post","status-publish","format-standard","hentry","category-unimportant","tag-ansible"],"_links":{"self":[{"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/posts\/3717","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/comments?post=3717"}],"version-history":[{"count":3,"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/posts\/3717\/revisions"}],"predecessor-version":[{"id":3730,"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/posts\/3717\/revisions\/3730"}],"wp:attachment":[{"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/media?parent=3717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/categories?post=3717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/tags?post=3717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}