Skip to content

Instantly share code, notes, and snippets.

@sidneiweber
Last active December 27, 2019 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sidneiweber/d229e9fc41249683f505dc4566566e18 to your computer and use it in GitHub Desktop.
Save sidneiweber/d229e9fc41249683f505dc4566566e18 to your computer and use it in GitHub Desktop.
# cat roles/launch/tasks/main.yml
- name: Find Windows AMI base in this region
ec2_ami_facts:
owners: 801119661308
filters:
name: Windows_Server-2019-English-Full-Base*
register: found_amis
- name: Get AMI Windows
set_fact:
win_ami_id: "{{ (found_amis.images | first).image_id }}"
- name: Ensure security group is present
ec2_group:
name: WinRM RDP
description: Inbound WinRM and RDP
region: "{{ target_aws_region }}"
vpc_id: "{{ vpc_id }}"
rules:
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 5986
to_port: 5986
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 3389
to_port: 3389
cidr_ip: 0.0.0.0/0
rules_egress:
- proto: -1
cidr_ip: 0.0.0.0/0
register: sg_out
- name: Ensure instances are running
ec2:
region: "{{ target_aws_region }}"
image: "{{ win_ami_id }}"
instance_type: "{{ instance_type }}"
group_id: "{{ sg_out.group_id }}"
key_name: "{{ keypair }}"
wait: yes
wait_timeout: 500
exact_count: 1
assign_public_ip: yes
vpc_subnet_id: "{{ subnet }}"
count_tag:
Name: stock-win-ami-test
instance_tags:
Name: stock-win-ami-test
user_data: "{{ lookup('template', 'userdata.txt.j2') }}"
register: ec2_result
- name: wait for WinRM to answer on all hosts
wait_for:
port: 5986
host: "{{ item.public_ip }}"
delay: 30
timeout: 300
state: started
with_items: "{{ ec2_result.tagged_instances }}"
- name: add hosts to groups
add_host:
name: "win-temp-{{ item.id }}"
ansible_ssh_host: "{{ item.public_ip }}"
ec2_id: "{{ item.id }}"
groups: win
changed_when: false
with_items: "{{ ec2_result.tagged_instances }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment