How can I check if file has been downloaded in ansible

Note: this answer covers general question of “How can i check the file existence in ansible”, not a specific case of downloading file.

The problems with the previous answers using “command” or “shell” actions is that they won’t work in –check mode. Actually, first action will be skipped, and next will error out on “when: solr_exists.rc != 0” condition (due to variable not being defined).

Since Ansible 1.3, there’s more direct way to check for file existance – using “stat” module. It of course also works well as “local_action” to check a local file existence:

- local_action: stat path={{secrets_dir}}/secrets.yml
  register: secrets_exist

- fail: msg="Production credentials not found"
  when: secrets_exist.stat.exists == False

Leave a Comment