Skip to content

Instantly share code, notes, and snippets.

@Greyeye
Last active April 29, 2021 22:53
Show Gist options
  • Save Greyeye/04bfaaf3eb34c2d2d2d0016bb27d9dc7 to your computer and use it in GitHub Desktop.
Save Greyeye/04bfaaf3eb34c2d2d2d0016bb27d9dc7 to your computer and use it in GitHub Desktop.
ansible ec2 bootstrap for windows serverit boots up AMI with MSSQL 2014 Express and install some basic tools like 7zip, notepad++tested with Ansible 2.0.2 on ubuntu 14.04 LTS~/.aws/credentials file is required for boto File Contents ==== ./ansible/ec2_windows_bootstrap.yml ./ansible/hosts ./ansible/group_vars/windows.yml to run ===== ./ansible/>…
---
- name: Provision an EC2 Instance
hosts: local
connection: local
gather_facts: False
tags: provisioning
# Necessary Variables for creating/provisioning the EC2 Instance
vars:
instance_type: t2.small
group_id: ['sg-xxx','sg-xxx']
region: ap-southeast-2 # Change the Region
keypair: aws-keypair # Change the keypair name
vpc_subnet_id: subnet-xxx
instance_profile_name: test-servers #IAM role name - make sure it exists
count: 1
#content of https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1
#saved as init_ansible.ps1
user_data: "{{lookup('file','init_ansible.ps1')}}"
# Task that will be used to Launch/Create an EC2 Instance
tasks:
- name: find current Windows AMI in this region
ec2_ami_find:
region: "{{ region }}"
platform: windows
virtualization_type: hvm
owner: amazon
name: Windows_Server-2012-R2_RTM-English-64Bit-SQL_2014_SP1_Express*
no_result_action: fail
sort: name
sort_order: descending
register: found_amis
- set_fact:
win_ami_id: "{{ (found_amis.results | first).ami_id }}"
- name: Launch the new EC2 Instance
ec2:
group_id: "{{ group_id }}"
instance_type: "{{ instance_type }}"
image: "{{ win_ami_id }}"
wait: true
region: "{{ region }}"
key_name: "{{ keypair }}"
vpc_subnet_id: "{{ vpc_subnet_id }}"
instance_profile_name: "{{ instance_profile_name }}"
count: "{{ count }}"
user_data: "{{ user_data }}"
register: ec2
- name: Add the newly created EC2 instance(s) to the local host group (located inside the directory)
lineinfile: dest="./hosts"
regexp={{ item.private_ip }}
insertafter="[windows]" line={{ item.private_ip }}
with_items: "{{ec2.instances}}"
- name: get the Administrator password
ec2_win_password:
instance_id: "{{ item.id }}"
region: "{{ region }}"
key_file: "~/.aws/aws-keypair.pem"
wait: yes
wait_timeout: 600
with_items: "{{ec2.instances}}"
register: instance_password
- name: Add the new instance password to the variables.
replace: dest="./group_vars/windows.yml"
regexp="ec2_dynamic_password"
replace={{ (instance_password.results | first).win_password }}
- name: Wait for winrm to come up
local_action: wait_for
host={{ item.private_ip }}
port=5986
state=started
with_items: "{{ ec2.instances }}"
- name: Add tag to Instance(s)
local_action: ec2_tag resource={{ item.id }} region={{ region }} state=present
with_items: "{{ ec2.instances }}"
args:
tags:
Name: ansible-ec2-auto
- meta: refresh_inventory
- name: install support applications
hosts: windows
gather_facts: False
tasks:
- set_fact:
ansible_password: "{{ (hostvars['localhost']['instance_password'].results | first).win_password }}"
- pause: minutes=2
- name: install 7zip
win_chocolatey:
name: 7zip
- name: install notepad++
win_chocolatey:
name: notepadplusplus.install
register: results
- debug: var=results
[local]
localhost
[windows]
#./group_vars/windows.yml
ansible_user: Administrator
ansible_password: "ec2_dynamic_password"
ansible_port: 5986
ansible_connection: winrm
# The following is necessary for Python 2.7.9+ when using default WinRM self-signed certificates:
ansible_winrm_server_cert_validation: ignore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment