Skip to content

Commit

Permalink
[patch] Adding cluster's ingress in DNS record list for Route53 hoste…
Browse files Browse the repository at this point in the history
…d zone (#1118)
  • Loading branch information
andrercm authored Nov 30, 2023
1 parent 7592542 commit 803bcc3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ibm/mas_devops/common_tasks/pod_templates/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
block:
- name: Get and combine podTemplates
ansible.builtin.set_fact:
merged_pod_templates_list: "{{ merged_pod_templates_list | default([]) + item_list }}"
merged_pod_templates_list: "{{ merged_pod_templates_list | default([]) + item_list }}"
vars:
item_name: "{{ item | splitext | first | replace('-', '_') }}_pod_templates"
item_list: "{{ lookup('ansible.builtin.vars', item_name, default='') | default([], true) }}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,31 @@
route53_lb_dnsname_output: "{{ aws_hosted_zone_loadbalancer_output.stdout }}"

- set_fact:
route53_lb_dnsname: "{{ route53_lb_dnsname_output | first }}"
route53_lb_dnsname: "{{ route53_lb_dnsname_output | first | regex_search('^.*(?=.$)') }}" # removes a dot at the end that aws cli command adds

- name: "aws-route53 : Lookup Load Balancer's zone id" # this finds the load balancer host id based on the load balancer dns name
shell: |
aws elb describe-load-balancers |
jq --arg name {{ route53_lb_dnsname }} \
-r '.LoadBalancerDescriptions | .[] | select(.CanonicalHostedZoneName=="\($name)") | .CanonicalHostedZoneNameID'
register: aws_hosted_zone_id_loadbalancer_output

- set_fact:
route53_lb_zone_id: "{{ aws_hosted_zone_id_loadbalancer_output.stdout }}"

- name: "Assert Load Balancer DNS Name for cluster {{ cluster_ingress }} exists"
assert:
that: route53_lb_dnsname is defined and route53_lb_dnsname != ""
fail_msg: "There is no Load Balancer DNS Name found for {{ cluster_ingress }}. Verify your AWS Route53 hosted zone '{{ route53_hosted_zone_name }}' and ensure there's an 'A type' entry for your cluster and a corresponding load balancer associated to it."

- name: "aws-route53 : Generate CNAME json file for {{ route53_lb_dnsname }}"
- name: "aws-route53 : Generate CNAME json file in: {{ route53_cname_json_file_path_local }}/{{ mas_instance_id }}-{{ route53_hosted_zone_name }}-cnames.json"
ansible.builtin.template:
src: "{{ route53_cname_json_file_path_local }}/create-cnames.json.j2"
dest: "{{ route53_cname_json_file_path_local }}/{{ mas_instance_id }}-{{ route53_hosted_zone_name }}-cnames.json"
mode: '664'
mode: "664"

- name: "aws-route53 : Create CNAME records pointing to {{ route53_lb_dnsname }}"
shell: |
aws route53 change-resource-record-sets --hosted-zone-id {{ route53_hosted_zone_id }} --change-batch file://{{ route53_cname_json_file_path_local }}/{{ mas_instance_id }}-{{ route53_hosted_zone_name }}-cnames.json
register: aws_hosted_zone_loadbalancer_output
failed_when: aws_hosted_zone_loadbalancer_output.rc > 0 and ('it already exists' not in aws_hosted_zone_loadbalancer_output.stderr )
failed_when: aws_hosted_zone_loadbalancer_output.rc > 0 and ('it already exists' in aws_hosted_zone_loadbalancer_output.stderr )
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@
"Value": "{{ route53_lb_dnsname }}"
}]
}
},
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "{{ cluster_ingress }}",
"Type": "A",
"AliasTarget": {
"HostedZoneId": "{{ route53_lb_zone_id }}",
"DNSName": "{{ route53_lb_dnsname }}",
"EvaluateTargetHealth": false
}
}
}
]
}

0 comments on commit 803bcc3

Please sign in to comment.