# Is it possible to copy a file to a newly created ec2 instance in the same playbook?

**URL:** <https://forum.ansible.com/t/is-it-possible-to-copy-a-file-to-a-newly-created-ec2-instance-in-the-same-playbook/26010>\
**Category:** Ansible Project\
**Created:** [November 13, 2017, 6:17pm UTC](https://forum.ansible.com/t/is-it-possible-to-copy-a-file-to-a-newly-created-ec2-instance-in-the-same-playbook/26010 "2017-11-13T18:17:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff\_Parrish](https://avatars.discourse-cdn.com/v4/letter/j/e9a140/32.png) [@Jeff\_Parrish](https://forum.ansible.com/u/Jeff_Parrish)\
**Post date:** [November 13, 2017, 6:17pm UTC](https://forum.ansible.com/t/is-it-possible-to-copy-a-file-to-a-newly-created-ec2-instance-in-the-same-playbook/26010/1 "2017-11-13T18:17:47Z")

</div>

I’m trying to start a marketplace instance and secure it all in the same playbook. That requires copying some files and running a provisioning script. I’d prefer to keep all that in a single playbook, to reduce chances of error and keep it semi-atomic. It doesn’t seem to be possible even with the dynamic inventory capability, since the state of the system is determined at the beginning of the playbook, and there is no way I can see to otherwise indicate the target host for the copy and script modules.

---

<div class="post-metadata">

**Author:** ![ddrake2012](https://avatars.discourse-cdn.com/v4/letter/d/9fc348/32.png) [@ddrake2012](https://forum.ansible.com/u/ddrake2012)\
**Post date:** [November 13, 2017, 6:23pm UTC](https://forum.ansible.com/t/is-it-possible-to-copy-a-file-to-a-newly-created-ec2-instance-in-the-same-playbook/26010/2 "2017-11-13T18:23:48Z")

</div>

Yes you can use the add\_host module to create an in-memory group. I am having weird issues with EC2 instances that I just posted about ([https://groups.google.com/forum/#!topic/ansible-project/jqRDPGqUof8](https://groups.google.com/forum/#!topic/ansible-project/jqRDPGqUof8)) but I am able to run 2 commands on the newly provisioned hosts.

You need something like this using a registered ec2 variable from provisioning:

- name: Add all instance public IPs to in-memory group  
add\_host:  
name: “{{ item.1.public\_ip }}”  
groups: ec2hosts  
with\_subelements:
- “{{ ec2.results }}”
- instances

and then you’d be able to use that group in another hosts section of your play:

- hosts: ec2hosts  
user: ubuntu  
gather\_facts: false
