How to install packages by passing EC2 user data at startup using Ansible

I want to install nginx by passing it as user data to the ec2 instance during startup. I pass 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 successfully instantiated EC2 but didn't install nginx.

Could you point me in the right direction? Thanks to

+3


source to share


1 answer


You are missing the shebang, try 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: |
               #!/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

      

+6


source







All Articles