Ansible – Troubleshooting Logrotate Issues with Postrotate Script

ansible

Below is my ansible role > defaults/main.yaml

    $cat roles/logrotate/defaults/main.yml
    
      logrotate_conf_dir: "/etc/logrotate.d/"
      logrotate_scripts:
        - name: test
          log_dir: '/var/log/test'
          log_extension: 'log'
          options:
            - rotate 7
            - weekly
            - size 10M
            - missingok
            - compress
            - create 0644 test test
          scripts:
            postrotate: "echo test >> /var/log/test.log"

and my task/main.yaml has

    $ cat roles/logrotate/tasks/main.yaml
    # tasks file for nginx
    
    - name: Setup logrotate scripts
      template:
        src: logrotate.d.j2
        dest: "{{ logrotate_conf_dir }}{{ item.name }}"
      with_items: "{{ logrotate_scripts }}"
      when: logrotate_scripts is defined

Getting the below error while running this simple playbook:

TASK [logrotate : Setup logrotate scripts] *******************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ansible.errors.AnsibleUndefinedVariable: 'dict object' has no attribute 'iteritems'
failed: [10.20.5.72] (item={'name': 'test', 'log_dir': '/var/log/test', 'log_extension': 'log', 'options': ['rotate 7', 'weekly', 'size 10M', 'missingok', 'compress', 'create 0644 test test'], 'scripts': {'postrotate': 'echo test >> /var/log/test.log'}}) => {"ansible_loop_var": "item", "changed": false, "item": {"log_dir": "/var/log/test", "log_extension": "log", "name": "test", "options": ["rotate 7", "weekly", "size 10M", "missingok", "compress", "create 0644 test test"], "scripts": {"postrotate": "echo test >> /var/log/test.log"}}, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'iteritems'"}

Best Answer

In the template, fix the iteration. For example,

{% for k,v in item.scripts.items() %}

Example of a complete playbook for testing

shell> cat pb.yml
- hosts: localhost

  vars:

    logrotate_conf_dir: "/etc/logrotate.d/"
    logrotate_scripts:
      - name: test
        log_dir: '/var/log/test'
        log_extension: 'log'
        options:
          - rotate 7
          - weekly
          - size 10M
          - missingok
          - compress
          - create 0644 test test
        scripts:
          postrotate: "echo test >> /var/log/test.log"

  tasks:

    - name: Setup logrotate scripts
      template:
        src: logrotate.d.j2
        dest: "{{ logrotate_conf_dir }}{{ item.name }}"
      loop: "{{ logrotate_scripts|d([]) }}"
shell> cat logrotate.d.j2
{{ item.log_dir }}/{{ item.name }}.{{ item.log_extension }}
{
{% for option in item.options %}
        {{ option }}
{% endfor %}
{% for k,v in item.scripts.items() %}
        {{ k }}
                {{ v }}
        endscript
{% endfor %}
}

gives (--check --diff)

+/var/log/test/test.log
+{
+        rotate 7
+        weekly
+        size 10M
+        missingok
+        compress
+        create 0644 test test
+        postrotate
+                echo test >> /var/log/test.log
+        endscript
+}