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 the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/5617fb1f-3aa7-45a3-ba84-656b7b786c86n%40googlegroups.com.

Reply via email to