Stat not working as expected for file exists or not

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

**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

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

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