-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an Ansible playbook for creating a droplet
- Loading branch information
Showing
3 changed files
with
148 additions
and
48 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
collections: | ||
- name: ansible.posix | ||
- name: community.general | ||
version: ">=2.0.0" | ||
- name: community.digitalocean |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
--- | ||
- hosts: localhost | ||
tasks: | ||
- name: Gather information about DigitalOcean droplets | ||
community.digitalocean.digital_ocean_droplet_info: | ||
register: do_droplets | ||
- name: Gather information about DigitalOcean SSH keys | ||
community.digitalocean.digital_ocean_sshkey_info: | ||
register: do_ssh_keys | ||
|
||
- name: Print info on existing droplets | ||
ansible.builtin.debug: | ||
msg: >- | ||
{{ item.name }}: | ||
{{ item.networks.v4 | map(attribute='ip_address') | join(',') }} | ||
loop: "{{ do_droplets.data }}" | ||
loop_control: | ||
label: "{{ item.id }}" | ||
|
||
- name: "Enter name for new droplet (subdomain only)" | ||
ansible.builtin.pause: | ||
register: input_name | ||
|
||
- name: "Enter functional name for new droplet (webNN)" | ||
ansible.builtin.pause: | ||
register: input_functional | ||
|
||
- name: Print available SSH public keys | ||
ansible.builtin.debug: | ||
msg: "{{ item.name}} {{ item.fingerprint }}" | ||
loop: "{{ do_ssh_keys.data }}" | ||
loop_control: | ||
label: "{{ item.id }}" | ||
|
||
- name: "Enter SSH key names for new droplet (space separated)" | ||
ansible.builtin.pause: | ||
register: input_ssh_keys | ||
|
||
- name: Set droplet facts | ||
ansible.builtin.set_fact: | ||
host: "{{ input_name.user_input | trim }}" | ||
functional: "{{ input_functional.user_input | trim }}" | ||
ssh_keys: >- | ||
{{ | ||
do_ssh_keys.data | | ||
selectattr('name', 'in', | ||
input_ssh_keys.user_input | split | map('trim')) | | ||
map(attribute='fingerprint') | ||
}} | ||
- name: Verify droplet configuration | ||
ansible.builtin.assert: | ||
that: | ||
- host in valid_planets | ||
# Must not be an existing name. | ||
- >- | ||
do_droplets.data | | ||
selectattr('name', 'equalto', '{{ host }}.matplotlib.org') | | ||
count == 0 | ||
# TODO: Also check that functional name doesn't already exist. | ||
- functional is regex('^web[0-9][0-9]$') | ||
# At least 1 key, and same number as requested. | ||
- ssh_keys | length >= 1 | ||
- ssh_keys | length == input_ssh_keys.user_input | split | length | ||
|
||
- name: Print configuration | ||
ansible.builtin.debug: | ||
msg: "Creating droplet '{{ host }}' with SSH keys {{ ssh_keys }}" | ||
|
||
- name: Please verify the above configuration | ||
ansible.builtin.pause: | ||
|
||
- name: Create droplet on DigitalOcean | ||
community.digitalocean.digital_ocean_droplet: | ||
state: present | ||
name: "{{ host }}.matplotlib.org" | ||
firewall: | ||
- Web | ||
image: fedora-39-x64 | ||
monitoring: true | ||
project: matplotlib.org | ||
region: tor1 | ||
size: s-1vcpu-2gb | ||
ssh_keys: "{{ ssh_keys }}" | ||
tags: | ||
- website | ||
unique_name: true | ||
register: new_droplet | ||
|
||
- name: Setup DNS for droplet on CloudFlare | ||
community.general.cloudflare_dns: | ||
state: present | ||
proxied: true | ||
record: "{{ host }}" | ||
type: A | ||
value: >- | ||
{{ | ||
new_droplet.data.droplet.networks.v4 | | ||
selectattr('type', 'equalto', 'public') | | ||
map(attribute='ip_address') | | ||
first | ||
}} | ||
zone: matplotlib.org | ||
|
||
- name: Setup functional DNS for droplet on CloudFlare | ||
community.general.cloudflare_dns: | ||
state: present | ||
proxied: true | ||
record: "{{ functional }}" | ||
type: CNAME | ||
value: "{{ host }}.matplotlib.org" | ||
zone: matplotlib.org | ||
|
||
vars: | ||
# We currently name servers based on planets in the Solar System. | ||
valid_planets: | ||
- mercury | ||
- venus | ||
- earth | ||
- mars | ||
- jupiter | ||
- saturn | ||
- uranus | ||
- neptune | ||
- pluto |