Score:0

ansible logrotate issues with postrorate script

sj flag

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'"}

br flag
Post the template `logrotate.d.j2`
Score:1
br flag

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
+}
Vicky Ingle - Mint avatar
sj flag
Thanks a lot, the solution worked for me!!!
br flag
You're welcome. You should [edit] the question and post the template. It should be clear where does the error come from.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.