# How to skip local\_action if file exists?

**URL:** <https://forum.ansible.com/t/how-to-skip-local-action-if-file-exists/15640>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [July 10, 2014, 2:46am UTC](https://forum.ansible.com/t/how-to-skip-local-action-if-file-exists/15640 "2014-07-10T02:46:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stuart\_Reynolds](https://avatars.discourse-cdn.com/v4/letter/s/90ced4/32.png) [@Stuart\_Reynolds](https://forum.ansible.com/u/Stuart_Reynolds)\
**Post date:** [July 10, 2014, 2:46am UTC](https://forum.ansible.com/t/how-to-skip-local-action-if-file-exists/15640/1 "2014-07-10T02:46:10Z")

</div>

I’m trying to write a set of tasks to create an ec2 key\_pair (if one doesn’t exist) and save it to svn.

Sadly, ec2\_key is quite slow for me, even if the key already exists (maybe my network?). How can I prevent this task step from being run if I already have a key file locally?

… other modules include things like “creates:”. It would be nice if this logic was available the task level.

- Stu  
PS: is there a complete documentation of a the keys in a task object? (name:, local\_action:, … what else?)  
PPS. does anyone how to use variable in the text for a name?

`

- name: “Create a key for {{ cluster\_name }}” # Output includes the braces ☹  
hosts: localhost  
tags:
- provision  
gather\_facts: false # Prevents immediately logging into hosts  
vars:

# Allow override of key name

key\_name: “{{instance\_key\_name | default(cluster\_name)}}”  
tasks:

- name: Ensure key file exists on AWS  
local\_action:  
module: ec2\_key  
name: “{{key\_name}}”  
region: “{{instance\_region}}”  
register: result

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 10, 2014, 12:24pm UTC](https://forum.ansible.com/t/how-to-skip-local-action-if-file-exists/15640/2 "2014-07-10T12:24:28Z")

</div>

Start by trying something like this:

- local\_action: stat path=foo  
register: stat\_result

- debug: var=stat\_result

Then use the stat\_result in the conditional

---

<div class="post-metadata">

**Author:** ![Stuart\_Reynolds](https://avatars.discourse-cdn.com/v4/letter/s/90ced4/32.png) [@Stuart\_Reynolds](https://forum.ansible.com/u/Stuart_Reynolds)\
**Post date:** [July 10, 2014, 6:23pm UTC](https://forum.ansible.com/t/how-to-skip-local-action-if-file-exists/15640/3 "2014-07-10T18:23:15Z")

</div>

Nifty. Thx.
