# Stat not working as expected for file exists or not

**URL:** <https://forum.ansible.com/t/stat-not-working-as-expected-for-file-exists-or-not/27890>\
**Category:** Ansible Project\
**Created:** [June 27, 2018, 9:26pm UTC](https://forum.ansible.com/t/stat-not-working-as-expected-for-file-exists-or-not/27890 "2018-06-27T21:26:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raj5](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@Raj5](https://forum.ansible.com/u/Raj5)\
**Post date:** [June 27, 2018, 9:26pm UTC](https://forum.ansible.com/t/stat-not-working-as-expected-for-file-exists-or-not/27890/1 "2018-06-27T21:26:26Z")

</div>

Hello,

I am trying to use “STAT” to verify if file exists or not but it’s not working as expected, looks like it’s not supporting standard variables like inventory\_hostname & Playbook\_dir

this is my task/main.yml

> - name: Ansible check file exists example.  
> stat:  
> path: {{playbook\_dir}}/tmp/{{inventory\_hostname}}/schema\_names  
> register: schema\_names
> - debug:  
> msg: “schema\_names file exists proceeding to shared schema upgrade”  
> when: fschema\_names.stat.exists

I am getting below error message

```auto
**15:26:22** [node] [WARNING]: Syntax Error while loading YAML. The error appears to have been
**15:26:22** [node] in '/workspace/Test_Deploy/ansible/roles/test-
**15:26:22** [node] deploy/tasks/main.yml': line 62, column 29, but may be elsewhere in the file
**15:26:22** [node] depending on the exact syntax problem. The offending line appears to be:
**15:26:22** [node] stat: path:
**15:26:22** [node] {{playbook_dir}}/tmp/{{inventory_hostname}}/schema_names
**15:26:22** [node] ^ here We could be wrong, but this one looks like it might be an issue with
**15:26:22** [node] missing quotes. Always quote template expression brackets when they start a
**15:26:22** [node] value. For instance: with_items: - {{ foo }} Should be written as:
**15:26:22** [node] with_items: - "{{ foo }}" exception type: <class
**15:26:22** [node] 'yaml.parser.ParserError'> exception: while parsing a block mapping in
**15:26:22** [node] "<unicode string>", line 62, column 7 did not find expected key in "<unicode
**15:26:22** [node] string>", line 62, column 29

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [June 27, 2018, 9:29pm UTC](https://forum.ansible.com/t/stat-not-working-as-expected-for-file-exists-or-not/27890/2 "2018-06-27T21:29:01Z")

</div>

That seems unrelated to 'stat', you have an incorrectly formatted YAML file.

looking at `path:`, you are missing quotes as the value starts with {

see:  
[https://docs.ansible.com/ansible/latest/reference\_appendices/YAMLSyntax.html#gotchas](https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html#gotchas)

---

<div class="post-metadata">

**Author:** ![Raj5](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@Raj5](https://forum.ansible.com/u/Raj5)\
**Post date:** [June 27, 2018, 10:13pm UTC](https://forum.ansible.com/t/stat-not-working-as-expected-for-file-exists-or-not/27890/3 "2018-06-27T22:13:22Z")

</div>

Thanks Brian for pointing out the error, now it’s working great.
