Getting hostnames from a fact

33 views
Skip to first unread message

lift...@gmail.com

unread,
Apr 24, 2024, 3:26:45 PM4/24/24
to Ansible Project
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

dulh...@mailbox.org

unread,
Apr 24, 2024, 3:51:55 PM4/24/24
to ansible...@googlegroups.com
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
--
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.

lift...@gmail.com

unread,
Apr 24, 2024, 4:01:55 PM4/24/24
to Ansible Project
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,
Harry

Todd Lewis

unread,
Apr 24, 2024, 4:55:22 PM4/24/24
to ansible...@googlegroups.com, uto...@gmail.com
You're drifting from your goal, which was to restart a service based on a zero-length /var/log/messages file. Now you're getting mired down because of a set_fact (which you don't need) on hosts on which the find module (which is the wrong module) failed to find the file in question.

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

Todd Lewis

unread,
Apr 24, 2024, 5:55:25 PM4/24/24
to ansible...@googlegroups.com, uto...@gmail.com

On 4/24/24 4:01 PM, lift...@gmail.com 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.
The ansible.builtin.find module always returns a list.

You're doing an ansible.builtin.set_fact on all the hosts, the value of which is a subset of that list.

But that, too, is a list, even if it is an empty list.

So you'll get an empty list for any host where either (a) no /var/log/messages file was found (not likely), or (b) where /var/log/messages is not zero-length.

But there's nothing in your first few tasks to preclude ansible.builtin.debug from showing these very real and very much there empty lists.

-- 
Todd

Stuart Lowe

unread,
Apr 25, 2024, 4:09:28 AM4/25/24
to ansible...@googlegroups.com

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

 

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

 

--

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.

---------------

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

lift...@gmail.com

unread,
Apr 25, 2024, 10:10:19 AM4/25/24
to Ansible Project
I used the stat module as suggested by Todd and it worked perfectly.  I appreciate the suggestion and for setting me straight.

Thanks,
Harry

Reply all
Reply to author
Forward
0 new messages