Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install the packages by passing EC2 user data at the launch time using Ansible

I want to install 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 }}"
    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

like image 927
Arbab Nazar Avatar asked Jul 27 '15 07:07

Arbab Nazar


People also ask

Where can we add scripts to run when an instance is being launched?

When a user data script is processed, it is copied to and run from /var/lib/cloud/instances/ instance-id / . The script is not deleted after it is run. Be sure to delete the user data scripts from /var/lib/cloud/instances/ instance-id / before you create an AMI from the instance.


1 Answers

You are missing the shebang, try the following:

- 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: |
               #!/bin/sh
               sudo apt-get install nginx -y
    image: "{{ imgae_id.ami }}"
    vpc_subnet_id: "{{ public_subnet }}"
    assign_public_ip: True
    wait: True
    wait_timeout: 600
like image 183
Conrado Avatar answered Oct 04 '22 00:10

Conrado