# creating ec2 key and save the result to a file

**URL:** <https://forum.ansible.com/t/creating-ec2-key-and-save-the-result-to-a-file/21116>\
**Category:** Ansible Project\
**Created:** [April 9, 2016, 7:23pm UTC](https://forum.ansible.com/t/creating-ec2-key-and-save-the-result-to-a-file/21116 "2016-04-09T19:23:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tzach\_Livyatan1](https://avatars.discourse-cdn.com/v4/letter/t/9fc29f/32.png) [@Tzach\_Livyatan1](https://forum.ansible.com/u/Tzach_Livyatan1)\
**Post date:** [April 9, 2016, 7:23pm UTC](https://forum.ansible.com/t/creating-ec2-key-and-save-the-result-to-a-file/21116/1 "2016-04-09T19:23:23Z")

</div>

I’m trying to use ec2\_key module, creating a new key and save it to a file for future use.  
The first succeed, but I have problem saving to a .pem file:  
Look like _ec2\_key.private\_key_ is not returned, but _ec2\_key.fingerprint_ is.

Any idea why?

Thanks  
Tzach

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 10, 2016, 5:20pm UTC](https://forum.ansible.com/t/creating-ec2-key-and-save-the-result-to-a-file/21116/2 "2016-04-10T17:20:24Z")

</div>

Tzach,

change ec2\_key.private\_key into ec2\_key.key.private\_key, here is a working example of this one for your reference. By the way, I am using Ansible 2.0.1.0

`

- name: Create an EC2 key  
ec2\_key:  
name: “mykey”  
region: “eu-west-1”  
register: ec2\_key

- name: save private key  
copy:  
content: “{{ ec2\_key.key.private\_key }}”  
dest: “./aws-private.pem”  
mode: 0600  
when: ec2\_key.changed  
`

Hope this will help you.

---

<div class="post-metadata">

**Author:** ![Tzach\_Livyatan2](https://avatars.discourse-cdn.com/v4/letter/t/58956e/32.png) [@Tzach\_Livyatan2](https://forum.ansible.com/u/Tzach_Livyatan2)\
**Post date:** [April 10, 2016, 6:44pm UTC](https://forum.ansible.com/t/creating-ec2-key-and-save-the-result-to-a-file/21116/3 "2016-04-10T18:44:32Z")

</div>

Thanks!
