# Handling unreachable hosts

**URL:** <https://forum.ansible.com/t/handling-unreachable-hosts/38277>\
**Category:** Ansible Project\
**Created:** [July 29, 2023, 12:00pm UTC](https://forum.ansible.com/t/handling-unreachable-hosts/38277 "2023-07-29T12:00:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kenady\_Inampudi](https://avatars.discourse-cdn.com/v4/letter/k/6a8cbe/32.png) [@Kenady\_Inampudi](https://forum.ansible.com/u/Kenady_Inampudi)\
**Post date:** [July 29, 2023, 12:00pm UTC](https://forum.ansible.com/t/handling-unreachable-hosts/38277/1 "2023-07-29T12:00:42Z")

</div>

I have a playbook to lock a user this works as expected but fails when any of the server in the inventory is unreachable  
lock\_user.yml

---

<div class="post-metadata">

**Author:** ![Pierre\_TOURON](https://avatars.discourse-cdn.com/v4/letter/p/b5a626/32.png) [@Pierre\_TOURON](https://forum.ansible.com/u/Pierre_TOURON)\
**Post date:** [August 6, 2023, 4:23pm UTC](https://forum.ansible.com/t/handling-unreachable-hosts/38277/2 "2023-08-06T16:23:51Z")

</div>

Hi,

Error is not about hosts being unreachable per say, but about a undefined variable (user\_exists.rc here) that is still being evaluated for each host, whether it is reachable or not.  
I’m not sure on how to change this behavior, but you should be able to circumvent the issue by checking whether var is defined before evaluating it :

when: “user\_exists.rc is defined and user\_exists.rc == 0 and inventory\_hostname in groups[‘linux’] and user not in myusers”

Or setting a default value, here with ‘default’ jinja filter, though you can also initialize somewhere else :

when: “user\_exists.rc|d() == 0 and inventory\_hostname in groups[‘linux’] and user not in myusers”

On a side note, you should probably use more specific modules than shell / command when you can help it, though I don’t know much of your context. Here you could use [‘ansible.builtin.user’ module](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/user_module.html#parameter-password_lock) :).
