# Task performs work despite conditional

**URL:** <https://forum.ansible.com/t/task-performs-work-despite-conditional/17302>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [January 17, 2015, 4:04pm UTC](https://forum.ansible.com/t/task-performs-work-despite-conditional/17302 "2015-01-17T16:04:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Morgan1](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@James\_Morgan1](https://forum.ansible.com/u/James_Morgan1)\
**Post date:** [January 17, 2015, 4:04pm UTC](https://forum.ansible.com/t/task-performs-work-despite-conditional/17302/1 "2015-01-17T16:04:47Z")

</div>

Hi,

I have the following task

> `
> 
> - name: Create ec2 key pair using local key  
> when: ec2\_key\_material is defined  
> ec2\_key:  
> name: “{{ ec2\_key\_name }}”  
> region: “{{ ec2\_region }}”  
> key\_material: “{{ item }}”  
> with\_file: /path/to/public\_key.id\_rsa.pub  
> `

Code was taking from [http://docs.ansible.com/ec2\_key\_module.html](http://docs.ansible.com/ec2_key_module.html)

ec2\_key\_material is not defined

Whilst it does skip it tries to find the file. If I wanted to pass the file into the role and do the following

`with_file: "{{ ec2_key_material }}"`

I get

> `TASK: [openbet/aws/ec2-create-instance | Create ec2 key pair using local key] *** fatal: [localhost] => could not locate file in lookup: {{ ec2_key_material }}`

If I add a real path then it logs out the key. I really don’t want that happening

> `TASK: [openbet/aws/ec2-create-instance | Create ec2 key pair using local key] *** skipping: [localhost] => (item=-----BEGIN RSA PRIVATE KEY----- MIIEogIBAAKCAQEAkYI6Fxq0Qlxmn0rwWlyVX4VERtahcjZDFMA8VnJiPCcHMcGSZWmqkVusrm39 ..... 9B5yU8q4D9vEzuLYmbaRJji9YpTnPCHEHJ7Dn9a85UMIk+D0AYsHDMW3rqkovcsXhwft5CdIVDJ0 gNgSLOyND24AHxTwxBhHnAf3oSsoYpx5PgcT75ydE123nGFWa4siMZiV/vP4+ycOUF4= -----END RSA PRIVATE KEY-----)`

Any suggestions. The latter issue seems worrying

Thanks

James

---

<div class="post-metadata">

**Author:** ![vaida.dan](https://avatars.discourse-cdn.com/v4/letter/v/5daacb/32.png) [@vaida.dan](https://forum.ansible.com/u/vaida.dan)\
**Post date:** [January 17, 2015, 4:54pm UTC](https://forum.ansible.com/t/task-performs-work-despite-conditional/17302/2 "2015-01-17T16:54:10Z")

</div>

Try with\_file: “{{ lookup(‘file’, ‘path/goes/here’ ) }}”

---

<div class="post-metadata">

**Author:** ![James\_Morgan1](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@James\_Morgan1](https://forum.ansible.com/u/James_Morgan1)\
**Post date:** [January 17, 2015, 5:13pm UTC](https://forum.ansible.com/t/task-performs-work-despite-conditional/17302/3 "2015-01-17T17:13:24Z")

</div>

Hi,

That gives me the following

TASK: [openbet/aws/ec2-create-instance | Create ec2 key pair using local key] \*\*\*  
fatal: [localhost] = **\> could not locate file in lookup:** -----BEGIN RSA PRIVATE KEY-----  
MIIEogIBAAKCAQEAkYI6Fxq0Qlxmn0rwWlyVX4VERtahcjZDFMA8VnJiPCcHMcGSZWmqkVusrm39  
wIHi16uUwyj+D0BpoNEDy8mvaRvE9apwn2s/Ampyb8nQnHCNdx43wu+AIN3PG8PAfRbrcGlPpKpQ

The location is correct.

I’m running ansible-playbook 1.8.2

It still logs the private key which is iffy and

when: ec2\_key\_material is defined

isn’t stopping it from looking for the file. I would have thought it would have short circuited itself before doing any lookups

James

---

<div class="post-metadata">

**Author:** ![vaida.dan](https://avatars.discourse-cdn.com/v4/letter/v/5daacb/32.png) [@vaida.dan](https://forum.ansible.com/u/vaida.dan)\
**Post date:** [January 17, 2015, 5:30pm UTC](https://forum.ansible.com/t/task-performs-work-despite-conditional/17302/4 "2015-01-17T17:30:20Z")

</div>

If you read the documentation page of this module careful enough, you will see that, in fact, the “{{ item }}” comes from the with\_file, not from your vars. That is the material 🙂

---

<div class="post-metadata">

**Author:** ![James\_Morgan1](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@James\_Morgan1](https://forum.ansible.com/u/James_Morgan1)\
**Post date:** [January 17, 2015, 6:03pm UTC](https://forum.ansible.com/t/task-performs-work-despite-conditional/17302/5 "2015-01-17T18:03:05Z")

</div>

Hi,

Not sure I follow, I understand that {{ item }} comes from with\_file. The same as other loops

Maybe i’m missing something and I apologise

All I want to achieve is to conditionally handle the key if the path to a key is specified.

I also don’t want it to log the key if its skipped and I want it to skip if the path isn’t specified.

The docs don’t mention using lookup

---

<div class="post-metadata">

**Author:** ![James\_Morgan1](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@James\_Morgan1](https://forum.ansible.com/u/James_Morgan1)\
**Post date:** [January 17, 2015, 6:54pm UTC](https://forum.ansible.com/t/task-performs-work-despite-conditional/17302/6 "2015-01-17T18:54:22Z")

</div>

Regarding the second point. This post helped correct the saving .pem logic

[http://grokbase.com/t/gg/ansible-project/147an12dj4/how-to-skip-local-action-if-file-exists](http://grokbase.com/t/gg/ansible-project/147an12dj4/how-to-skip-local-action-if-file-exists)

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [January 19, 2015, 2:22pm UTC](https://forum.ansible.com/t/task-performs-work-despite-conditional/17302/7 "2015-01-19T14:22:35Z")

</div>

try:

with\_file: "{{ ec2\_key\_material|default }}"

with\_ lookups always happen before the when clause (to allow for use  
of item in when), so if you don't specify a key this will be an error.

---

<div class="post-metadata">

**Author:** ![James\_Morgan1](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@James\_Morgan1](https://forum.ansible.com/u/James_Morgan1)\
**Post date:** [January 20, 2015, 9:56am UTC](https://forum.ansible.com/t/task-performs-work-despite-conditional/17302/8 "2015-01-20T09:56:50Z")

</div>

Perfect thanks
