Skip to content

Instantly share code, notes, and snippets.

@mechastorm
Last active December 17, 2015 23:58
Show Gist options
  • Save mechastorm/5692657 to your computer and use it in GitHub Desktop.
Save mechastorm/5692657 to your computer and use it in GitHub Desktop.
---
# Attempt to check if the folder exist.
# If it exist echo out a string. You have to echo something
# if the directory does exist or else the task is considered failed
# Output is set to $is_default_created. Note that this variable is
# object and not an atomic value.
- name: Attempt to check if a directory exist
action: shell test -d /my/folder && echo "exist" || echo ""
register: is_folder_created
# Do an operation of somekind if the folder does not exist
- name: Do an operation if folder does not exist
action: command echo "Operation Yashima"
when_string: ${is_folder_created.stdout} != "exist"
@mechastorm
Copy link
Author

I recently needed to copy a folder to a server only if it does not exist.

Ansible makes this partly easier with the "when" property but I had not found a magic module that does a check if the directory exist.

This was the solution I came up with was

@pbuyle
Copy link

pbuyle commented Jun 19, 2014

You can now uses the stat module to check if a file exists and is a directory.
See http://docs.ansible.com/stat_module.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment