Skip to content

Instantly share code, notes, and snippets.

@initcron
Created December 25, 2017 16:04
Show Gist options
  • Save initcron/562cf9af7bf61b83885f40bce20376ed to your computer and use it in GitHub Desktop.
Save initcron/562cf9af7bf61b83885f40bce20376ed to your computer and use it in GitHub Desktop.
provision ec2 instance with ansible with user-data
# link: http://allandenot.com/devops/2015/01/31/provisioning-ec2-hosts-with-ansible.html
---
- name: Provision EC2 Box
local_action:
module: ec2
key_name: "{{ ec2_keypair }}"
group_id: "{{ ec2_security_group }}"
instance_type: "{{ ec2_instance_type }}"
image: "{{ ec2_image }}"
vpc_subnet_id: "{{ ec2_subnet_ids|random }}"
region: "{{ ec2_region }}"
user_data: "{{ user_data }}"
instance_tags: '{"Name":"{{ec2_tag_Name}}","Type":"{{ec2_tag_Type}}","Environment":"{{ec2_tag_Environment}}"}'
assign_public_ip: yes
wait: true
count: 1
volumes:
- device_name: /dev/sda1
device_type: gp2
volume_size: "{{ ec2_volume_size }}"
delete_on_termination: true
register: ec2
# in vars file (e.g. webserver.yml)
user_data: "{{ lookup('file', 'user-data.sh') }}"
# create user-data.sh and add anything that you would want to run during provisioning
# example is given below
#!/bin/bash
apt-get update
apt-get install -y git wget
# Install Docker
wget -qO- get.docker.com | sh &
wait
service docker start
# Start Codespaces
cd /root
git clone https://github.com/codespaces-io/codespaces.git
curl -L "https://github.com/docker/compose/releases/download/1.11.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
cd /root/codespaces/cs-chef-ci
docker-compose up -d
Copy link

ghost commented Feb 10, 2023

Hi Initcron,
How would you use power shell as a user data in this playbook...?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment