# How to install the packages using EC2 user data at the launch time

**URL:** <https://forum.ansible.com/t/how-to-install-the-packages-using-ec2-user-data-at-the-launch-time/18860>\
**Category:** Ansible Project\
**Created:** [July 27, 2015, 7:26am UTC](https://forum.ansible.com/t/how-to-install-the-packages-using-ec2-user-data-at-the-launch-time/18860 "2015-07-27T07:26:01Z")\
**Posts on this page:** 3\
**Page:** 1

<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:** [July 27, 2015, 7:26am UTC](https://forum.ansible.com/t/how-to-install-the-packages-using-ec2-user-data-at-the-launch-time/18860/1 "2015-07-27T07:26:01Z")

</div>

I want to install the nginx by passing it as the user data to the ec2 instance at the launch time. I am passing it like this

- name: WebServer | Create the WebServer Instance(s)  
local\_action:  
module: ec2  
region: “{{ vpc\_region }}”  
group: “{{ ec2\_security\_groups[1].sg\_name }}”  
keypair: “{{ key\_name }}”  
instance\_type: “{{ web\_instance\_type }}”  
**user\_data: “sudo apt-get install nginx -y”**

image: “{{ imgae\_id.ami }}”  
vpc\_subnet\_id: “{{ public\_subnet }}”  
#private\_ip: “{{ nat\_private\_ip }}”  
assign\_public\_ip: True  
wait: True  
wait\_timeout: 600

But the above method didn’t work for me although it has created the EC2 instance successfully but didn’t install the nginx.

Can you please point me in the right direction. Thanks

---

<div class="post-metadata">

**Author:** ![jon2](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@jon2](https://forum.ansible.com/u/jon2)\
**Post date:** [July 27, 2015, 5:57pm UTC](https://forum.ansible.com/t/how-to-install-the-packages-using-ec2-user-data-at-the-launch-time/18860/2 "2015-07-27T17:57:43Z")

</div>

I had the same issue on Centos, i.e. I added a script and nothing happened. Not tried on Debian based distros, but I found building a base AMI with cloud-init installed and, in /etc/cloud/cloud.cfg set ‘disable\_root: false’, solved it for me. Then launch an instance of that base AMI, setting the user-data.  
In my case it wasn’t an ansible issue, but a limitation of the AMI I chose.

---

<div class="post-metadata">

**Author:** ![Timothy\_Gerla](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@Timothy\_Gerla](https://forum.ansible.com/u/Timothy_Gerla)\
**Post date:** [July 27, 2015, 6:00pm UTC](https://forum.ansible.com/t/how-to-install-the-packages-using-ec2-user-data-at-the-launch-time/18860/3 "2015-07-27T18:00:49Z")

</div>

It’s been a long time since I’ve used cloud-init, but I believe that your script needs to start with a bangline (#!/bin/sh) for cloud-init to interpret it as an on-boot script.

Regardless, I think a better approach would be to use Ansible and the apt module to ensure that the package is installed post-boot, not just running a script on boot.

-Tim
