Find number of servers where something is true

9 views
Skip to first unread message

Tommy Ash

unread,
Jun 19, 2017, 11:35:38 AM6/19/17
to Ansible Project
Hi,  I'm trying to get a count of how many servers have a specific file on them.  For example, if /tmp/testFile is on 8 of 25 hosts that I'm running a playbook against, I'd like to register a variable and give it the value of 8.  Is this possible?

Dick Davies

unread,
Jun 19, 2017, 12:13:08 PM6/19/17
to ansible list
http://docs.ansible.com/ansible/group_by_module.html

will let you make a group by fact, you could then just have a task
on localhost output '{{ groups["has_that_file"] _ length }}".

There's a set_fact task you can use to set a (boolean) fact on hosts,
or just bite the bullet and write a custom fact.

It seems a bit of an odd requirement to be honest, that's the best I can
suggest from what you've posted.
> --
> 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 post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/8bd181dc-20ce-4ce1-97b4-e85921799c69%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Dick Davies

unread,
Jun 19, 2017, 12:14:04 PM6/19/17
to ansible list
On 19 June 2017 at 17:13, Dick Davies <di...@hellooperator.net> wrote:

> will let you make a group by fact, you could then just have a task
> on localhost output '{{ groups["has_that_file"] _ length }}".

sorry that should be:

{{ groups["has_that_file"] | length }}

Matt Martz

unread,
Jun 19, 2017, 12:29:00 PM6/19/17
to ansible...@googlegroups.com
This can also be accomplished using the `stat` module, and then a little jinja2 pipeline:

- hosts: all
  tasks:
    - stat:
        path: /tmp
      register: stat_result

    - debug:
        msg: "{{ hostvars|dictsort|selectattr('1.stat_result.stat.exists')|list|length }}"
      run_once: true

This would output something like the following for me:

TASK [debug] *********************************************************************************************************************************************************************
ok: [example.org] => {
    "msg": "8"
}

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAK5eLPQV8OcnQcV%2BTRNhp9pQPrEQzayrP7gQ4GbH%2BOg14mq6bA%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net
Reply all
Reply to author
Forward
0 new messages