File exists but stat is false

304 views
Skip to first unread message

Daniel Nanboe

unread,
Feb 4, 2020, 5:52:17 AM2/4/20
to Ansible Project
I have a problem checking if a file exists:

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

Dick Visser

unread,
Feb 4, 2020, 6:02:50 AM2/4/20
to ansible...@googlegroups.com
that exact task errors because of the space after "path=" - I think
that's a typo on your part?
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/72841c12-fda7-4df3-bc78-2997857a684f%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Daniel Nanboe

unread,
Feb 4, 2020, 12:26:51 PM2/4/20
to Ansible Project
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
        }
    }
}

On Tuesday, February 4, 2020 at 1:02:50 PM UTC+2, Dick Visser wrote:
that exact task errors because of the space after "path=" - I think
that's a typo on your part?



On Tue, 4 Feb 2020 at 11:52, Daniel Nanboe <nan...@gmail.com> wrote:
>
> I have a problem checking if a file exists:
>
> - 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
>         }
>     }
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Dick Visser

unread,
Feb 4, 2020, 3:50:03 PM2/4/20
to ansible...@googlegroups.com
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. 

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/876f3fed-4bdb-4982-8946-4d44c4ae2b28%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Dick Visser

unread,
Feb 4, 2020, 3:55:02 PM2/4/20
to ansible...@googlegroups.com
Which leads to the question: how did you establish that the file is actually there?

Daniel Nanboe

unread,
Feb 4, 2020, 3:58:46 PM2/4/20
to Ansible Project
I just realised that I am stupid :D

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"
Reply all
Reply to author
Forward
0 new messages