# How to select hosts based on the last connection time?

**URL:** <https://forum.ansible.com/t/how-to-select-hosts-based-on-the-last-connection-time/23683>\
**Category:** Ansible Project\
**Created:** [February 8, 2017, 3:45pm UTC](https://forum.ansible.com/t/how-to-select-hosts-based-on-the-last-connection-time/23683 "2017-02-08T15:45:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cev\_Ing](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@Cev\_Ing](https://forum.ansible.com/u/Cev_Ing)\
**Post date:** [February 8, 2017, 3:45pm UTC](https://forum.ansible.com/t/how-to-select-hosts-based-on-the-last-connection-time/23683/1 "2017-02-08T15:45:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 8, 2017, 4:02pm UTC](https://forum.ansible.com/t/how-to-select-hosts-based-on-the-last-connection-time/23683/2 "2017-02-08T16:02:02Z")

</div>

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

O

---

<div class="post-metadata">

**Author:** ![Cev\_Ing](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@Cev\_Ing](https://forum.ansible.com/u/Cev_Ing)\
**Post date:** [February 9, 2017, 1:00pm UTC](https://forum.ansible.com/t/how-to-select-hosts-based-on-the-last-connection-time/23683/3 "2017-02-09T13:00:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [February 9, 2017, 1:09pm UTC](https://forum.ansible.com/t/how-to-select-hosts-based-on-the-last-connection-time/23683/4 "2017-02-09T13:09:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Cev\_Ing](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@Cev\_Ing](https://forum.ansible.com/u/Cev_Ing)\
**Post date:** [February 9, 2017, 1:31pm UTC](https://forum.ansible.com/t/how-to-select-hosts-based-on-the-last-connection-time/23683/5 "2017-02-09T13:31:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 9, 2017, 1:40pm UTC](https://forum.ansible.com/t/how-to-select-hosts-based-on-the-last-connection-time/23683/6 "2017-02-09T13:40:41Z")

</div>

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
