Skip to content

Instantly share code, notes, and snippets.

@schwartzmx
Last active February 10, 2023 00:51
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save schwartzmx/1d01cbf3cc07d486e1fa to your computer and use it in GitHub Desktop.
Save schwartzmx/1d01cbf3cc07d486e1fa to your computer and use it in GitHub Desktop.
Ansible Windows EC2 Provisioning Playbook
- name: Launch {{env}}-{{service}} Instance(s)
hosts: localhost
gather_facts: false
connection: local
vars_files:
- group_vars/env/{{ environment }}.yml
- group_vars/service/{{ service }}.yml
- group_vars/admin.yml
# Launch instances with the following parameters. Register the output.
tasks:
- name: Launch instance
local_action:
module: ec2
region: "{{region}}"
image: "{{image}}"
instance_type: "{{instance_type}}"
instance_profile_name: "{{iam_role}}"
group: "{{security_group}}"
key_name: "{{key_name}}"
vpc_subnet_id: "{{vpc_subnet_id}}"
assign_public_ip: "{{public_ip}}"
instance_tags:
Name: "{{service}}"
Environment: "{{env}}"
stackdriver_monitor: "{{stackdriver_monitor}}"
count: "{{count}}"
user_data: "<powershell>
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force\n
$storageDir = $pwd\n
$webclient = New-Object System.Net.WebClient\n
$url = \"https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1\"\n
$file = \"C:\\Program Files\\Amazon\\Ec2ConfigService\\Scripts\\ConfigureRemotingForAnsible.ps1\"\n
$webclient.DownloadFile($url,$file)\n
$VerbosePreference=\"Continue\"\n
& $file\n
$user=[adsi]\"WinNT://localhost/Administrator,user\"\n
$user.SetPassword(\"{{password}}\")\n
</powershell>"
wait: true
register: ec2
Copy link

ghost commented Feb 10, 2023

Hello Phil, I'll be testing your playbook, can the user data also call a .ps1 file that you create?

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