I used the stat module as suggested by Todd and it worked perfectly.  I 
appreciate the suggestion and for setting me straight.

Thanks,
Harry

On Thursday, April 25, 2024 at 4:09:28 AM UTC-4 Stuart Lowe wrote:

> Something like this should work?
>
> ---
>
> - 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: Check if /var/log/messages is zero length
>
>     ansible.builtin.stat:
>
>       path: "{{ item.path }}"
>
>     register: file_details
>
>     with_items: "{{ messages_files.files }}"
>
>     when: messages_files.matched > 0
>
>  
>
>   - name: Restart rsyslog service if /var/log/messages is zero length
>
>     ansible.builtin.systemd:
>
>       name: rsyslog
>
>       state: restarted
>
>     when: item.stat.exists and item.stat.size == 0
>
>     with_items: "{{ file_details.results }}"
>
>  
>
> *From:* ansible...@googlegroups.com <ansible...@googlegroups.com> *On 
> Behalf Of *lift...@gmail.com
> *Sent:* Wednesday, April 24, 2024 8:27 PM
> *To:* Ansible Project <ansible...@googlegroups.com>
> *Subject:* [ansible-project] Getting hostnames from a fact
>
>  
>
> You don't often get email from lift...@gmail.com. Learn why this is 
> important <https://aka.ms/LearnAboutSenderIdentification>
>
>  
>
> *Caution:* This email originated from outside of the organisation. Do not 
> click links or open attachments unless you recognise the sender and know 
> the content is safe 
>
>  
>
> 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-proje...@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
>  
> <https://groups.google.com/d/msgid/ansible-project/5617fb1f-3aa7-45a3-ba84-656b7b786c86n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> ---------------
>
> *Stuart Lowe* He/Him
> Zen Cloud Engineering - Team Leader
> *Zen Internet*
> www.zen.co.uk 
>
> Proud to be a certified B Corporation
>
> This message is private and confidential. If you have received this 
> message in error, please notify us and remove it from your system.
>
> Zen Internet Limited may monitor email traffic data to manage billing, to 
> handle customer enquiries, and for the prevention and detection of fraud. 
> We may also monitor the content of emails sent to and/or from Zen Internet 
> Limited for the purposes of security, staff training and to monitor the 
> quality of service.
> Zen Internet Limited is registered in England and Wales, Sandbrook Park, 
> Sandbrook Way, Rochdale, OL11 1RY Company No. 03101568 VAT Reg No. 686 0495 
> 01 
>

-- 
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/51004e56-4d09-43e3-bea4-70db21cb4f4cn%40googlegroups.com.

Reply via email to