Ansible stat - check file exists not working

1,473 views
Skip to first unread message

Anfield

unread,
Jun 1, 2017, 9:33:27 PM6/1/17
to Ansible Project
---
- hosts: localhost
  become: yes
  tasks:
    - name: Check if a file exists
      stat:
        path: /home/ansible/test.txt
      register: file
    - debug: var="file exists"
      when: file.stat.exists == true

The above doesnt work, can anyone tell me why?

The output is

PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [Check if a file exists] **************************************************
ok: [localhost]

TASK [debug] *******************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "template error while templating string: expected token 'end of print statement', got 'exists'. String: {{file exists}}"}
        to retry, use: --limit @/etc/ansible/playbooks/statfile.retry


Kai Stian Olstad

unread,
Jun 2, 2017, 12:55:37 AM6/2/17
to ansible...@googlegroups.com
On 02. juni 2017 03:33, Anfield wrote:
> ---
> - hosts: localhost
> become: yes
> tasks:
> - name: Check if a file exists
> stat:
> path: /home/ansible/test.txt
> register: file
> - debug: var="file exists"
> when: file.stat.exists == true
>
> The above doesnt work, can anyone tell me why?

The var= in debug is for variables, for text you need to use msg=

So to print out the content of your file.
- debug: var=file

To print text:
- debug: msg="file exist and content of exists {{ file.stat.exists }}"

Since file is a module name, you shouldn't use that as you variable name.

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages