Advertisement
Guest User

Untitled

a guest
Feb 13th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. - name: Search for the latest Windows Server 2016 AMI
  2. ec2_ami_find:
  3. region: "{{ aws_region }}"
  4. platform: windows
  5. owner: amazon
  6. architecture: x86_64
  7. name: "Windows_Server-2016-English-Full-Base*"
  8. sort: creationDate
  9. sort_order: descending
  10. no_result_action: fail
  11. changed_when: False
  12. register: win_server_ami_id
  13.  
  14. - name: Create temporary CloudFormation temaplte
  15. template:
  16. src: templates/aws_cf_stack.yml.j2
  17. dest: /tmp/aws_cf_stack.yml
  18. changed_when: False
  19.  
  20. - name: create/update stack
  21. cloudformation:
  22. region: "{{ aws_region }}"
  23. stack_name: "{{ ansible_user_id }}-{{ aws_cf_stack_name }}"
  24. state: present
  25. disable_rollback: true
  26. template: /tmp/aws_cf_stack.yml
  27. tags: "{{ aws_cf_tags }}"
  28. register: aws_cf_stack
  29.  
  30. - name: Remove temporary CloudFormation temaplte
  31. file: path=/tmp/aws_cf_stack.yml state=absent
  32. changed_when: False
  33.  
  34. - name: Get facts about the newly created instances
  35. ec2_remote_facts:
  36. region: "{{ aws_region }}"
  37. filters:
  38. instance-state-name: running
  39. "tag:aws:cloudformation:stack-name": "{{ ansible_user_id }}-{{ aws_cf_stack_name }}"
  40. register: ec2_facts
  41.  
  42. - name: Get volumes ids
  43. ec2_vol:
  44. region: "{{ aws_region }}"
  45. instance: "{{ item.id }}"
  46. state: list
  47. with_items: "{{ ec2_facts.instances }}"
  48. register: ec2_instances_volumes
  49. loop_control:
  50. label: "{{ item.id }} - {{ item.private_ip_address }} - {{ item.tags.Name }}"
  51.  
  52. - name: Tag volumes
  53. ec2_tag:
  54. region: "{{ aws_region }}"
  55. resource: "{{ item.1.id }}"
  56. 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 }) }}"
  57. with_subelements:
  58. - "{{ ec2_instances_volumes.results }}"
  59. - volumes
  60. loop_control:
  61. label: "{{ item.1.id }} - {{ item.1.attachment_set.device }}"
  62.  
  63. - name: Wait for RDP to come up
  64. wait_for: host={{ item.private_ip_address }} port=3389
  65. with_items: "{{ ec2_facts.instances }}"
  66. when: item.tags.Hostname | match ("^win\d{2}")
  67. loop_control:
  68. label: "{{ item.private_ip_address }} - {{ item.id }} - {{ item.tags.Name }}"
  69.  
  70. - name: Get AWS Windows Administrator password
  71. ec2_win_password:
  72. instance_id: "{{ item.id }}"
  73. region: "{{ aws_region }}"
  74. key_file: ~/.ssh/id_rsa
  75. wait: yes
  76. wait_timeout: 300
  77. with_items: "{{ ec2_facts.instances }}"
  78. changed_when: false
  79. when: item.tags.Hostname | match ("^win\d{2}")
  80. register: win_ec2_passwords
  81. loop_control:
  82. label: "{{ item.id }} - {{ item.private_ip_address }} - {{ item.tags.Name }}"
  83.  
  84. - name: Add AWS Windows AD hosts to group winservers
  85. add_host:
  86. name: "{{ item.1.tags.Name }}"
  87. ansible_ssh_host: "{{ item.1.private_ip_address }}"
  88. ansible_port: 5986
  89. ansible_user: "{{ windows_machines_ansible_user }}"
  90. ansible_password: "{{ windows_machines_ansible_pass }}"
  91. ansible_winrm_server_cert_validation: ignore
  92. ansible_connection: 'winrm'
  93. groups: winservers
  94. site_name: "{{ ansible_user_id }}-{{ aws_cf_stack_name }}"
  95. changed_when: false
  96. when: item.0.win_password is defined and item.1.tags.Hostname | match ("^win\d{2}")
  97. with_together:
  98. - "{{ win_ec2_passwords.results }}"
  99. - "{{ ec2_facts.instances }}"
  100. loop_control:
  101. label: "{{ item.1.id }} - {{ item.1.private_ip_address }} - {{ item.1.tags.Name }}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement