# File exists but stat is false

**URL:** <https://forum.ansible.com/t/file-exists-but-stat-is-false/32132>\
**Category:** Ansible Project\
**Created:** [February 4, 2020, 10:52am UTC](https://forum.ansible.com/t/file-exists-but-stat-is-false/32132 "2020-02-04T10:52:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Nanboe](https://avatars.discourse-cdn.com/v4/letter/d/9d8465/32.png) [@Daniel\_Nanboe](https://forum.ansible.com/u/Daniel_Nanboe)\
**Post date:** [February 4, 2020, 10:52am UTC](https://forum.ansible.com/t/file-exists-but-stat-is-false/32132/1 "2020-02-04T10:52:17Z")

</div>

I have a problem checking if a file exists:

```auto
- name: Check if ssh key exists
  stat: path= "/config/clients/ssh_keys/daniel_nanboe.pub"
  register: exista
- debug: var=exista
- fail:
    msg: Stop

```

The file exists but the stat returns false:

“exista”: {  
“changed”: false,  
“failed”: false,  
“stat”: {  
“exists”: false  
}  
}

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 4, 2020, 11:02am UTC](https://forum.ansible.com/t/file-exists-but-stat-is-false/32132/2 "2020-02-04T11:02:17Z")

</div>

that exact task errors because of the space after "path=" - I think  
that's a typo on your part?

---

<div class="post-metadata">

**Author:** ![Daniel\_Nanboe](https://avatars.discourse-cdn.com/v4/letter/d/9d8465/32.png) [@Daniel\_Nanboe](https://forum.ansible.com/u/Daniel_Nanboe)\
**Post date:** [February 4, 2020, 5:26pm UTC](https://forum.ansible.com/t/file-exists-but-stat-is-false/32132/3 "2020-02-04T17:26:50Z")

</div>

Thanks I have removed that but the result is the same when I debug de var stat is false:

ok: =\> {  
“exista”: {  
“changed”: false,  
“failed”: false,  
“stat”: {  
“exists”: false  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 4, 2020, 8:49pm UTC](https://forum.ansible.com/t/file-exists-but-stat-is-false/32132/4 "2020-02-04T20:49:32Z")

</div>

It could still be a permissions issue, for instance if the ansible user has different rights than whatever you did manually that caused you to believe that the file is there.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 4, 2020, 8:54pm UTC](https://forum.ansible.com/t/file-exists-but-stat-is-false/32132/5 "2020-02-04T20:54:31Z")

</div>

Which leads to the question: how did you establish that the file is actually there?

---

<div class="post-metadata">

**Author:** ![Daniel\_Nanboe](https://avatars.discourse-cdn.com/v4/letter/d/9d8465/32.png) [@Daniel\_Nanboe](https://forum.ansible.com/u/Daniel_Nanboe)\
**Post date:** [February 4, 2020, 8:58pm UTC](https://forum.ansible.com/t/file-exists-but-stat-is-false/32132/6 "2020-02-04T20:58:46Z")

</div>

I just realised that I am stupid 😃

The file was on the computer where I was running the ansible script not on the target server. What I needed to do was to run the check locally

`

- name: Check if ssh key exists  
local\_action: stat path=“/config/clients/ssh\_keys/daniel\_nanboe.pub”  
register: exista
- debug: var=exista
- fail:  
msg: Stop  
`
