# Execute command only\_if some file does not exists yet

**URL:** <https://forum.ansible.com/t/execute-command-only-if-some-file-does-not-exists-yet/11612>\
**Category:** Ansible Project\
**Created:** [June 14, 2012, 10:35am UTC](https://forum.ansible.com/t/execute-command-only-if-some-file-does-not-exists-yet/11612 "2012-06-14T10:35:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harm\_Aarts](https://avatars.discourse-cdn.com/v4/letter/h/a5b964/32.png) [@Harm\_Aarts](https://forum.ansible.com/u/Harm_Aarts)\
**Post date:** [June 14, 2012, 10:35am UTC](https://forum.ansible.com/t/execute-command-only-if-some-file-does-not-exists-yet/11612/1 "2012-06-14T10:35:32Z")

</div>

Dear list,

I’m creating a playbook. In it I create a user. For that user I want to create an SSH key. But I want this to be idempotent. Currently I try to do this with:

- name: generate an SSH key for ops  
action: command su -c ‘ssh-keygen -N “” -t rsa -f /home/ops/.ssh/id\_rsa’ ops  
only\_if: ‘not os.path.isfile(“/home/ops/.ssh/id\_rsa”)’

The action clause seems to be evaluated before the only\_if clause causing Ansible to hang as the command requires user input (“are you sure you want to overwrite?”).  
I was thinking maybe I need to assign a variable in a vars block but I’m pretty sure I can’t do dynamic assignments there. Or maybe I’m going about this the wrong way entirely. I’d appreciate the help!

With kind regards,

---

<div class="post-metadata">

**Author:** ![Jeroen\_Hoekx](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@Jeroen\_Hoekx](https://forum.ansible.com/u/Jeroen_Hoekx)\
**Post date:** [June 14, 2012, 10:44am UTC](https://forum.ansible.com/t/execute-command-only-if-some-file-does-not-exists-yet/11612/2 "2012-06-14T10:44:29Z")

</div>

Hi,

> Dear list,
> 
> I'm creating a playbook. In it I create a user. For that user I want to  
> create an SSH key. But I want this to be idempotent. Currently I try to do  
> this with:  
> - name: generate an SSH key for ops  
> action: command su -c 'ssh-keygen -N "" -t rsa -f /home/ops/.ssh/id\_rsa'  
> ops  
> only\_if: 'not os.path.isfile("/home/ops/.ssh/id\_rsa")'

Look at [http://ansible.github.com/modules.html#command](http://ansible.github.com/modules.html#command)  
There's a 'creates' parameter to the command/shell modules that does  
what you want.

Greetings,

Jeroen

---

<div class="post-metadata">

**Author:** ![Harm\_Aarts](https://avatars.discourse-cdn.com/v4/letter/h/a5b964/32.png) [@Harm\_Aarts](https://forum.ansible.com/u/Harm_Aarts)\
**Post date:** [June 14, 2012, 12:11pm UTC](https://forum.ansible.com/t/execute-command-only-if-some-file-does-not-exists-yet/11612/3 "2012-06-14T12:11:52Z")

</div>

> \_\< Can’t believe I missed that. Thanks a lot.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [June 14, 2012, 12:47pm UTC](https://forum.ansible.com/t/execute-command-only-if-some-file-does-not-exists-yet/11612/4 "2012-06-14T12:47:50Z")

</div>

There were some patches to only\_if that I applied around 10 EST last night that correct the order, however only\_if works on facts only and that is evaluated "overlord" not "minion" side.

Since you are using command, the "creates" option to the module looks to be exactly what you want.

-- Michael

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [June 14, 2012, 12:48pm UTC](https://forum.ansible.com/t/execute-command-only-if-some-file-does-not-exists-yet/11612/5 "2012-06-14T12:48:14Z")

</div>

Beat me to it 🙂

-- Michael
