Skip to content

Instantly share code, notes, and snippets.

@halberom
Created June 5, 2014 14:58
Show Gist options
  • Save halberom/7ba0b251a45291621be4 to your computer and use it in GitHub Desktop.
Save halberom/7ba0b251a45291621be4 to your computer and use it in GitHub Desktop.
ansible - example of doing something if a file exists
# the file doesn't exist
TASK: [debug var=foo] *********************************************************
ok: [localhost] => {
"foo": {
"changed": false,
"invocation": {
"module_args": "path=/tmp/file",
"module_name": "stat"
},
"stat": {
"exists": false
}
}
}
TASK: [do something with file if exists] **************************************
skipping: [localhost]
---
- hosts: all
vars:
mypath: /tmp/file
tasks:
- stat: path={{mypath}}
register: foo
- debug: var=foo
- name: do something with file if exists
command: cat {{ mypath }}
when: foo.stat.exists
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment