Created
January 18, 2018 06:15
-
-
Save MTN-RowinAndruscavage/ba0272801451fe0a417f371d29b71a82 to your computer and use it in GitHub Desktop.
Ansible cloudformation creator
This file contains hidden or 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
| --- | |
| - set_fact: | |
| cf_stack_name: "{{ item }}" | |
| - name: Delete cloudformation for {{ cf_stack_name }} | |
| include: cf_delete.yml | |
| # heh, careful with this | |
| when: ( cf_stack_name in cf_stack_names_to_delete ) | |
| tags: | |
| - cf_delete | |
| - name: Create cloudformation for {{ cf_stack_name }} | |
| include: cf_create.yml | |
| tags: | |
| - cf_create |
This file contains hidden or 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: Launch docker cloudformation for {{ cf_stack_name }} | |
| cloudformation: | |
| stack_name: "{{ cf_stack_name }}" | |
| state: present | |
| region: "{{ cf_aws_region }}" | |
| disable_rollback: true | |
| template_url: https://editions-{{ cf_aws_region }}.s3.amazonaws.com/aws/stable/Docker.tmpl | |
| template_parameters: | |
| ManagerSize: 3 | |
| ClusterSize: 0 | |
| KeyName: "{{ aws_key_name }}" | |
| EnableSystemPrune: "yes" | |
| EnableCloudWatchLogs: "no" | |
| EnableCloudStorEfs: "yes" | |
| ManagerInstanceType: t2.small | |
| InstanceType: t2.small | |
| tags: | |
| Dept: "{{ aws_dept_name }}" | |
| Stack: "{{ cf_stack_name }}" | |
| Owner: "{{ aws_owner_name }}" | |
| Purpose: "CICD Pipeline" | |
| # WORKAROUND: Ideally this would be included in ec2.py dynamic inventory output, | |
| # but this is the way cloudformation_fact gathering module works at the moment. | |
| - name: Collect cloudformation vars for {{ cf_stack_name }} | |
| cloudformation_facts: | |
| stack_name: "{{ cf_stack_name }}" | |
| register: cf_facts | |
| - name: Current {{ cf_stack_name }}-Manager instance IP from ec2.py | |
| debug: | |
| var: hostvars[ cf_stack_name + '-Manager'].ansible_host | |
| - name: Collect {{ cf_stack_name }}-Manager instance IPs | |
| shell: >- | |
| aws ec2 describe-instances | jq -r ".Reservations[] | |
| .Instances[] | select(.SecurityGroups[].GroupName | | |
| contains(\"{{ cf_stack_name }}-ManagerVpcSG\")).PublicIpAddress" | |
| register: manager_ips | |
| changed_when: not (hostvars[ cf_stack_name + '-Manager' ].ansible_host in manager_ips.stdout_lines ) | |
| - name: Write cloudformations to ansible-inventory for {{ cf_stack_name }} | |
| template: | |
| src: docker-cloudformation.yml.j2 | |
| dest: ansible-inventory/settings/static/aws/docker-cf-{{ cf_stack_name }}.yml | |
| - name: Create cloudformation Route53 CNAME alias for {{ cf_stack_name }} | |
| route53: | |
| command: create | |
| zone: "{{ domainname }}" | |
| record: "{{ cf_stack_name }}.{{ domainname }}" | |
| type: CNAME | |
| ttl: 300 | |
| value: "{{ cf_facts['ansible_facts']['cloudformation'][ cf_stack_name ]['stack_outputs']['DefaultDNSTarget'] }}" | |
| overwrite: yes | |
| register: route53_result | |
| until: route53_result|success | |
| retries: 2 | |
| delay: 5 | |
| tags: | |
| - route53 | |
| - route53_cloudformation | |
This file contains hidden or 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
| --- | |
| # Remove cloudformation and route53 entries listed in cf_stack_names_to_delete | |
| - name: Delete docker cloudformation for {{ cf_stack_name }} | |
| cloudformation: | |
| stack_name: "{{ cf_stack_name }}" | |
| state: absent | |
| region: "{{ cf_aws_region }}" | |
| - name: Get cloudformation Route53 record to delete for {{ cf_stack_name }} | |
| route53: | |
| command: get | |
| zone: "{{ domainname }}" | |
| record: "{{ cf_stack_name }}.{{ domainname }}" | |
| type: CNAME | |
| register: rec | |
| - name: Delete cloudformation Route53 CNAME alias for {{ cf_stack_name }} | |
| route53: | |
| command: delete | |
| zone: "{{ domainname }}" | |
| record: "{{ rec.set.record }}" | |
| type: "{{ rec.set.type }}" | |
| ttl: "{{ rec.set.ttl }}" | |
| value: "{{ rec.set.value }}" | |
| when: rec.set.record is defined |
This file contains hidden or 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
| --- | |
| {{ ansible_managed | comment }} | |
| groups: | |
| cloudformation: | |
| # TODO: Define all hosts in the cluster | |
| hosts: | |
| {{ cf_stack_name }}-Manager: | |
| vars: | |
| # FIXME: First match in ec2.py takes precedence over ansible_host defined here | |
| ansible_host: {{ hostvars[ cf_stack_name + '-Manager'].ansible_host }} | |
| ansible_user: docker | |
| ansible_ssh_private_key_file: '~/.ssh/keys/ | |
| {{- cf_facts['ansible_facts']['cloudformation'][ cf_stack_name ]['stack_parameters']['KeyName'] }}.pem' | |
| DefaultDNSTarget: {{ cf_facts['ansible_facts']['cloudformation'][ cf_stack_name ]['stack_outputs']['DefaultDNSTarget'] }} | |
| # Dump of cloudformation facts | |
| {{ cf_facts['ansible_facts']['cloudformation'][ cf_stack_name ] | to_yaml | comment }} |
This file contains hidden or 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: Create docker cloudformation stacks {{ cf_stack_names }} | |
| include: cf.yml | |
| with_items: "{{ cf_stack_names }}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment