Created
February 13, 2017 20:25
-
-
Save ruzickap/3f795259af505ff06023b15a29ac817a to your computer and use it in GitHub Desktop.
Ansible playbook used for creating CloudFormation template + upload it to AWS + tag volumes + prepare winserver group
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- name: Search for the latest Windows Server 2016 AMI | |
ec2_ami_find: | |
region: "{{ aws_region }}" | |
platform: windows | |
owner: amazon | |
architecture: x86_64 | |
name: "Windows_Server-2016-English-Full-Base*" | |
sort: creationDate | |
sort_order: descending | |
no_result_action: fail | |
changed_when: False | |
register: win_server_ami_id | |
- name: Create temporary CloudFormation temaplte | |
template: | |
src: templates/aws_cf_stack.yml.j2 | |
dest: /tmp/aws_cf_stack.yml | |
changed_when: False | |
- name: create/update stack | |
cloudformation: | |
region: "{{ aws_region }}" | |
stack_name: "{{ ansible_user_id }}-{{ aws_cf_stack_name }}" | |
state: present | |
disable_rollback: true | |
template: /tmp/aws_cf_stack.yml | |
tags: "{{ aws_cf_tags }}" | |
register: aws_cf_stack | |
- name: Remove temporary CloudFormation temaplte | |
file: path=/tmp/aws_cf_stack.yml state=absent | |
changed_when: False | |
- name: Get facts about the newly created instances | |
ec2_remote_facts: | |
region: "{{ aws_region }}" | |
filters: | |
instance-state-name: running | |
"tag:aws:cloudformation:stack-name": "{{ ansible_user_id }}-{{ aws_cf_stack_name }}" | |
register: ec2_facts | |
- name: Get volumes ids | |
ec2_vol: | |
region: "{{ aws_region }}" | |
instance: "{{ item.id }}" | |
state: list | |
with_items: "{{ ec2_facts.instances }}" | |
register: ec2_instances_volumes | |
loop_control: | |
label: "{{ item.id }} - {{ item.private_ip_address }} - {{ item.tags.Name }}" | |
- name: Tag volumes | |
ec2_tag: | |
region: "{{ aws_region }}" | |
resource: "{{ item.1.id }}" | |
tags: "{{ aws_cf_instance_tags | combine({ 'Instance': item.1.attachment_set.instance_id }, { 'Device': item.1.attachment_set.device }, { 'Name': item.0.item.tags.Name + ' ' + item.1.attachment_set.device }) }}" | |
with_subelements: | |
- "{{ ec2_instances_volumes.results }}" | |
- volumes | |
loop_control: | |
label: "{{ item.1.id }} - {{ item.1.attachment_set.device }}" | |
- name: Wait for RDP to come up | |
wait_for: host={{ item.private_ip_address }} port=3389 | |
with_items: "{{ ec2_facts.instances }}" | |
when: item.tags.Hostname | match ("^win\d{2}") | |
loop_control: | |
label: "{{ item.private_ip_address }} - {{ item.id }} - {{ item.tags.Name }}" | |
- name: Get AWS Windows Administrator password | |
ec2_win_password: | |
instance_id: "{{ item.id }}" | |
region: "{{ aws_region }}" | |
key_file: ~/.ssh/id_rsa | |
wait: yes | |
wait_timeout: 300 | |
with_items: "{{ ec2_facts.instances }}" | |
changed_when: false | |
when: item.tags.Hostname | match ("^win\d{2}") | |
register: win_ec2_passwords | |
loop_control: | |
label: "{{ item.id }} - {{ item.private_ip_address }} - {{ item.tags.Name }}" | |
- name: Add AWS Windows AD hosts to group winservers | |
add_host: | |
name: "{{ item.1.tags.Name }}" | |
ansible_ssh_host: "{{ item.1.private_ip_address }}" | |
ansible_port: 5986 | |
ansible_user: "{{ windows_machines_ansible_user }}" | |
ansible_password: "{{ windows_machines_ansible_pass }}" | |
ansible_winrm_server_cert_validation: ignore | |
ansible_connection: 'winrm' | |
groups: winservers | |
site_name: "{{ ansible_user_id }}-{{ aws_cf_stack_name }}" | |
changed_when: false | |
when: item.0.win_password is defined and item.1.tags.Hostname | match ("^win\d{2}") | |
with_together: | |
- "{{ win_ec2_passwords.results }}" | |
- "{{ ec2_facts.instances }}" | |
loop_control: | |
label: "{{ item.1.id }} - {{ item.1.private_ip_address }} - {{ item.1.tags.Name }}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment