How to select hosts based on the last connection time?

How to run a playbook on hosts, that have been connected today? Or those, which have nor been connected today?

Use the connected/not connected data as part of your inventory, groups
seem like the obvious thing.

O

It is not so obvious to me, what you suggest. The inventory is something written by hand. Should I manually write the time-stamp when I have run a playbook on a host into the inventory file? And how should I query the information?

I am looking for something like find . -mtime +1 transferred to Ansible. Run the playbook on hosts, on which it has not been run for n days. And the negation: run the playbook on host, on which it has been run during the last n days.

Background: I have a playbook and I use it on my hosts. Then I change it. The change affects about half of the hosts. But the change was erroneous. Next I correct the playbook. And now I would like to run the playbook on those hosts changed by the last run.

Read up on ansible's idempotency. This allows you to run your playbook
multiple times on whatever hosts, and (if done well) it will assure
the state of each host ist what it should be. Changes are only made,
if the state deviates from the desired state (e.g. a file is deleted,
if it exists and you want it absent, but if there is no file then
ansible has nothing to do).

Johannes

Ansible does not guarantee that playbooks are idempotent. They are only idempotent, if you limit yourself to those modules and arguments, which are actually idempotent. And this is obviously only a subset.

My initial answer was very generic, but so was the question. In any
case it still applies.

If you are asking if Ansible has a 'built in' way to do this, no, it
does not, but it does give you the facilities to build this easily
yourself in any way that works for you.

You can use a callback/fact cache/or tasks that write to file/external
db to keep track of hosts contacted, then use that either as inventory
data (dynamic/group/host_vars/etc) or directly in play as vars_file or
lookup to condition which hosts are contacted.

hope this helps