Skip to content

Commit

Permalink
fixes: digital ocean provisioning
Browse files Browse the repository at this point in the history
  • Loading branch information
mtulio committed Jun 12, 2023
1 parent 7c521f0 commit 89bbcb1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 72 deletions.
22 changes: 11 additions & 11 deletions tasks/do-lb-register-dns.yaml
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
---
- name: LB | DO | Register DNS | Set IP
- name: DO | CB DNS | Register DNS | Set IP
ansible.builtin.set_fact:
res_value: "{{ lb_ip }}"
when: res.value is not defined
when: cb.value is not defined

# Module does not provide a good lookup nor indepotency, so
# let's load current records filter it and create only new ones.
# It provides indepotency by name, and not value. Additional
# checks should be done to achieve that.

- name: LB | DO | Register DNS | Get RR
- name: DO | CB DNS | Register DNS | Get RR
community.digitalocean.digital_ocean_domain_record_info:
state: present
domain: "{{ res.domain }}"
domain: "{{ cb.domain }}"
register: reg_dns_zone_records

- name: LB | DO | Register DNS | Show RRs
- name: DO | CB DNS | Register DNS | Show RRs
ansible.builtin.debug:
var: reg_dns_zone_records

- name: LB | DO | Register DNS | Set list to add
- name: DO | CB DNS | Register DNS | Set list to add
ansible.builtin.set_fact:
rr_to_add: []

- name: LB | DO | Register DNS | Discover value
- name: DO | CB DNS | Register DNS | Discover value
ansible.builtin.set_fact:
rr_to_add: "{{ rr_to_add + [record] }}"
when: reg_dns_zone_records.data.records | community.general.json_query(q_rr) | length <= 0
with_items: "{{ res.records }}"
with_items: "{{ cb.records }}"
loop_control:
loop_var: record
vars:
q_rr: "[?name=='{{ record.name }}']"

- name: LB | DO | Register DNS | Show RRs to add
- name: DO | CB DNS | Register DNS | Show RRs to add
ansible.builtin.debug:
var: rr_to_add

- name: LB | DO | Register DNS | Create RR # noqa: args[module]
- name: DO | CB DNS | Register DNS | Create RR # noqa: args[module]
community.digitalocean.digital_ocean_domain_record:
state: present
domain: "{{ res.domain }}"
domain: "{{ cb.domain }}"
type: "{{ record.type }}"
name: "{{ record.name }}"
data: "{{ record.value | d(res_value) }}"
Expand Down
90 changes: 29 additions & 61 deletions tasks/do-lb.yaml
Original file line number Diff line number Diff line change
@@ -1,81 +1,49 @@
---
- name: LB | DO | Set defaults
- name: DO | Set defaults
ansible.builtin.set_fact:
vpc_uuid: "{{ lb.vpc_uuid | d('') }}"
lb_name: ''
lb_ip: ''
lb_spec: "{{ lb.spec | d({}) }}"

# API does not return when member=true name=vpc_name
- name: LB | DO | Lookup for vpc_uuid
community.digitalocean.digital_ocean_vpc_info:
register: do_vpc
when: vpc_uuid | length > 0

- name: LB | DO | Set vpc_uuid
ansible.builtin.set_fact:
vpc_uuid: "{{ do_vpc.data | json_query(filter_vpc) | join(' ') }}"
- name: DO | Lookup for vpc_uuid
when:
- vpc_uuid | length > 0
- not(do_vpc.failed)
vars:
filter_vpc: "[?name=='{{ lb.vpc_name }}'].id"

- name: LB | DO | Gather load balancers
- lb_spec.vpc_uuid is not defined
- lb_spec.vpc_uuid | d('') | length == 0
block:
- name: DO | Lookup for vpc_uuid
community.digitalocean.digital_ocean_vpc_info:
register: do_vpc

- name: DO | Set vpc_uuid
ansible.builtin.set_fact:
lb_spec: "{{ lb_spec | combine({
'vpc_uuid': do_vpc.data | json_query(filter_vpc) | join(' ')
}) }}"
when: not(do_vpc.failed)
vars:
filter_vpc: "[?name=='{{ lb.vpc_name }}'].id"

- name: DO | Gather load balancers
community.digitalocean.digital_ocean_load_balancer_info:
register: do_lbs

- name: LB | DO | Set LB Name when eixsts
ansible.builtin.set_fact:
lb_name: "{{ do_lbs.data | community.general.json_query(filter_lb) | join(' ') }}"
vars:
filter_lb: "[?name=='{{ lb.name }}'].name"

- name: LB | DO | Set LB ID when eixsts
ansible.builtin.set_fact:
lb_id: "{{ do_lbs.data | community.general.json_query(filter_lb) | join(' ') }}"
vars:
filter_lb: "[?name=='{{ lb.name }}'].id"

- name: LB | DO | Set LB IP when eixsts
ansible.builtin.set_fact:
lb_ip: "{{ do_lbs.data | community.general.json_query(filter_lb) | join(' ') }}"
vars:
filter_lb: "[?name=='{{ lb.name }}'].ip"

- name: LB | DO | Create
- name: DO | Create
community.digitalocean.digital_ocean_load_balancer:
state: present
name: "{{ lb.name | d(omit) }}"
project: "{{ lb.project_name | d(omit) }}"
region: "{{ lb.region | d(omit) }}"
vpc_uuid: "{{ vpc_uuid | d(omit) }}"
size: "{{ lb.size | d(omit) }}"
droplet_ids: "{{ lb.droplet_ids | d([]) }}"
forwarding_rules: "{{ lb.forwarding_rules | d(omit) }}"
health_check: "{{ lb.health_check | d(omit) }}"
redirect_http_to_https: "{{ lb.redirect_http_to_https | d(omit) }}"
enable_backend_keepalive: "{{ lb.enable_backend_keepalive | d(omit) }}"
enable_proxy_protocol: "{{ lb.enable_proxy_protocol | d(omit) }}"
algorithm: "{{ lb.algorithm | d(omit) }}"
wait: true
when: lb_name | length > 0
args: "{{ lb_spec }}"
register: lb_out

# Gather info again (maybe sleep a bit?)
- name: LB | DO | Gather load balancers to register
- name: DO | Gather load balancers to register
community.digitalocean.digital_ocean_load_balancer_info:
register: do_lbs
when: lb_ip | length > 0

- name: LB | DO | Set LB IP when eixsts
- name: DO | Set LB IP when eixsts
ansible.builtin.set_fact:
lb_ip: "{{ do_lbs.data | community.general.json_query(filter_lb) | join(' ') }}"
vars:
filter_lb: "[?name=='{{ lb.name }}'].ip"
when: lb_ip | length > 0
filter_lb: "[?name=='{{ lb_spec.name }}'].ip"

- name: LB | DO | Callback register resources
ansible.builtin.include_tasks: "do-lb-register-{{ res.service }}.yaml"
with_items: "{{ lb.register_resources | d([]) }}"
- name: DO | Callback register resources
ansible.builtin.include_tasks: "do-lb-register-{{ cb.service }}.yaml"
with_items: "{{ lb.callbacks | d([]) }}"
loop_control:
loop_var: res
loop_var: cb
2 changes: 2 additions & 0 deletions tasks/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
loop_control:
loop_var: tg

- debug: var=cloud_loadbalancers

- name: Setup Load Balancers by provider
ansible.builtin.include_tasks: "{{ lb.provider }}-lb.yaml"
with_items: "{{ cloud_loadbalancers | d([]) }}"
Expand Down

0 comments on commit 89bbcb1

Please sign in to comment.