You probably want to use ansible.builtin.stat which will tell you both whether the file exists (find doesn't find it if it doesn't) and what size it is.
- name: Get stat for /var/log/messages
ansible.builtin.stat:
path: /var/log/messages
register: messages_files
- name: What to do about missing /var/log/messages?
ansible.builtin.debug:
msg: '{{ inventory_hostname }} has no /var/log/messages. Now what?'
when: not messages_files.stat.exists
- name: Restart syslog if /var/log/messages is zero-length
ansible.builtin.service:
name: syslog
state: restarted
when: messages_files.stat.exists and messages_files.stat.size == 0
There's no need to start another play to do the service restarts.
—
Todd
On 4/24/24 4:01 PM, [email protected] wrote:
Well, the set_fact is supposed to only register or select anything where the size parameter of the files attributes is 0. The ones that are printing just [] don't fit that criteria, and therefore, shouldn't be included in the debug output. Yet they are, so that's what I'm trying to fix/clean up first.Thanks, HarryOn Wednesday, April 24, 2024 at 3:51:55 PM UTC-4 [email protected] wrote:1) How can I NOT print what server2 is printing/showing?I'd say add a when clause to the task, like: - name: Print results ansible.builtin.debug: msg: "{{ zero }}" when: "some condition here" so the print only happens when the condition is met. The condition might be some value from the message_files variable Or maybe something like: when: meages_files.ssfiles | selectattr('size', '==', 0) not sure about that though because I don't understand really what meages_files.ssfiles | selectattr('size', '==', 0) does On 4/24/24 9:26 PM, [email protected] wrote:I have a playbook I'm developing where I'm trying to find any server that has a 0 length /var/log/messages files. When I find those, I want to restart the rsyslog service on those. So right now I'm setting this fact as follows: --- - hosts: my_hosts become: true become_method: sudo gather_facts: false tasks: - name: Determine if /var/log/messages is zero-length ansible.builtin.find: paths: /var/log patterns: messages register: messages_files - name: Set fact for all servers that have zero-length /var/log/messages ansible.builtin.set_fact: zero: "{{ messages_files.files | selectattr('size', '==', 0) }}" - name: Print results ansible.builtin.debug: msg: "{{ zero }}" When the debug print happens, I get all servers printing out either the file attributes, or an empty string: ok: [server1] => { "msg": [ { "atime": 1713683723.242925, "ctime": 1713683723.242925, "dev": 64777, "gid": 10, "gr_name": "wheel", "inode": 8212, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mode": "0640", "mtime": 1713683723.242925, "nlink": 1, "path": "/var/log/messages", "pw_name": "root", "rgrp": true, "roth": false, "rusr": true, "size": 0, "uid": 0, "wgrp": false, "woth": false, "wusr": true, "xgrp": false, "xoth": false, "xusr": false } ] } ok: [server2] => { "msg": [] } So, 2 questions: 1) How can I NOT print what server2 is printing/showing? 2) Once I fix #1, how can I get just the hostnames of those servers where the size of the file is 0, then start another play to restart rsyslog on only those? Thanks, Harry-- You received this message because you are subscribed to theGoogle Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/5617fb1f-3aa7-45a3-ba84-656b7b786c86n%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/5617fb1f-3aa7-45a3-ba84-656b7b786c86n%40googlegroups.com?utm_medium=email&utm_source=footer>.--You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/92d08bef-67eb-46d5-a2c1-6bcb75d2e262n%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/92d08bef-67eb-46d5-a2c1-6bcb75d2e262n%40googlegroups.com?utm_medium=email&utm_source=footer>.
-- Todd -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/777cf615-97a2-4363-aea4-3a006dc5bb36%40gmail.com.
