0

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

1
  • Post the template logrotate.d.j2 Commented Feb 6, 2023 at 11:42

1 Answer 1

1

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 +} 
2
  • Thanks a lot, the solution worked for me!!! Commented Feb 6, 2023 at 22:47
  • You're welcome. You should edit the question and post the template. It should be clear where does the error come from. Commented Feb 7, 2023 at 0:07

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.