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?
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.
sorry that should be:
{{ groups["has_that_file"] | length }}
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”
}