A launch without A1 capacity just sits queued (ACCEPTED, 0%), and the old retry stacked more queued launches. Now every AD is checked first; with no capacity anywhere the run stops with a clear message and launches nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
267 lines
11 KiB
YAML
267 lines
11 KiB
YAML
# oci.yml: cogitator-04 on Oracle Cloud (Always Free), compartment, network,
|
|
# instance and data volume. Runs locally: PLAYBOOK=oci.yml bin/deploy
|
|
# Needs the oracle.oci collection (requirements.yml) and the `oci` Python SDK
|
|
# in .venv (uv pip install --python .venv/bin/python oci).
|
|
# Credentials: vault_oci (API signing key of the tenancy's user).
|
|
- import_playbook: deploy-guard.yml
|
|
|
|
- name: cogitator-04 on Oracle Cloud
|
|
hosts: localhost
|
|
gather_facts: false
|
|
|
|
vars:
|
|
oci_instance_name: cogitator-04
|
|
oci_compartment_name: cogitator-04
|
|
oci_shape: VM.Standard.A1.Flex
|
|
oci_ocpus: 2
|
|
oci_memory_gbs: 6 # > 20% used keeps Oracle from reclaiming it as idle
|
|
oci_boot_volume_gbs: 50
|
|
oci_data_volume_gbs: 50
|
|
oci_storage_budget_gbs: 100 # of the tenancy's 200 GB; the rest stays free
|
|
oci_ad_index: 1 # preferred AD; others are tried if it has no capacity
|
|
oci_vcn_cidr: 10.40.0.0/24
|
|
# The modules need an OCI config file; it's written to a private temp
|
|
# dir from vault_oci and removed at the end.
|
|
oci_auth: &oci_auth
|
|
config_file_location: "{{ oci_key_dir.path }}/config"
|
|
config_profile_name: DEFAULT
|
|
|
|
tasks:
|
|
- name: Stay inside the Always Free limits
|
|
ansible.builtin.assert:
|
|
that:
|
|
- oci_shape == 'VM.Standard.A1.Flex'
|
|
- oci_ocpus <= 2
|
|
- oci_memory_gbs <= 12
|
|
- oci_boot_volume_gbs + oci_data_volume_gbs <= oci_storage_budget_gbs
|
|
- oci_storage_budget_gbs <= 200
|
|
quiet: true
|
|
|
|
- name: Private key in a temporary directory
|
|
block:
|
|
- name: Make a private temp dir
|
|
ansible.builtin.tempfile:
|
|
state: directory
|
|
register: oci_key_dir
|
|
|
|
- name: Write the API key
|
|
ansible.builtin.copy:
|
|
content: "{{ vault_oci.private_key }}"
|
|
dest: "{{ oci_key_dir.path }}/key.pem"
|
|
mode: '0600'
|
|
no_log: true
|
|
|
|
- name: Write the OCI config
|
|
ansible.builtin.copy:
|
|
content: |
|
|
[DEFAULT]
|
|
user={{ vault_oci.user }}
|
|
fingerprint={{ vault_oci.fingerprint }}
|
|
tenancy={{ vault_oci.tenancy }}
|
|
region={{ vault_oci.region }}
|
|
key_file={{ oci_key_dir.path }}/key.pem
|
|
dest: "{{ oci_key_dir.path }}/config"
|
|
mode: '0600'
|
|
|
|
- name: Compartment
|
|
oracle.oci.oci_identity_compartment:
|
|
<<: *oci_auth
|
|
parent_compartment_id: "{{ vault_oci.tenancy }}"
|
|
name: "{{ oci_compartment_name }}"
|
|
description: "cogitator-04 (monitoring), managed by ansible oci.yml"
|
|
wait: true
|
|
register: oci_compartment
|
|
|
|
- name: Availability domains
|
|
oracle.oci.oci_identity_availability_domain_facts:
|
|
<<: *oci_auth
|
|
compartment_id: "{{ vault_oci.tenancy }}"
|
|
register: oci_ads
|
|
|
|
- name: Facts
|
|
ansible.builtin.set_fact:
|
|
oci_cid: "{{ oci_compartment.compartment.id }}"
|
|
oci_ad: "{{ (oci_ads.availability_domains | map(attribute='name') | sort)[oci_ad_index - 1] }}"
|
|
|
|
- name: VCN
|
|
oracle.oci.oci_network_vcn:
|
|
<<: *oci_auth
|
|
compartment_id: "{{ oci_cid }}"
|
|
display_name: "{{ oci_instance_name }}"
|
|
cidr_blocks: ["{{ oci_vcn_cidr }}"]
|
|
dns_label: cog04
|
|
key_by: [compartment_id, display_name]
|
|
register: oci_vcn
|
|
|
|
- name: Internet gateway (outbound only; nothing is allowed in)
|
|
oracle.oci.oci_network_internet_gateway:
|
|
<<: *oci_auth
|
|
compartment_id: "{{ oci_cid }}"
|
|
vcn_id: "{{ oci_vcn.vcn.id }}"
|
|
display_name: "{{ oci_instance_name }}"
|
|
is_enabled: true
|
|
key_by: [compartment_id, vcn_id, display_name]
|
|
register: oci_igw
|
|
|
|
- name: Route table
|
|
oracle.oci.oci_network_route_table:
|
|
<<: *oci_auth
|
|
compartment_id: "{{ oci_cid }}"
|
|
vcn_id: "{{ oci_vcn.vcn.id }}"
|
|
display_name: "{{ oci_instance_name }}"
|
|
route_rules:
|
|
- destination: 0.0.0.0/0
|
|
destination_type: CIDR_BLOCK
|
|
network_entity_id: "{{ oci_igw.internet_gateway.id }}"
|
|
key_by: [compartment_id, vcn_id, display_name]
|
|
register: oci_rt
|
|
|
|
# No ingress rules: Tailscale needs outbound only (direct or via DERP).
|
|
- name: Security list (egress only)
|
|
oracle.oci.oci_network_security_list:
|
|
<<: *oci_auth
|
|
compartment_id: "{{ oci_cid }}"
|
|
vcn_id: "{{ oci_vcn.vcn.id }}"
|
|
display_name: "{{ oci_instance_name }}"
|
|
egress_security_rules:
|
|
- destination: 0.0.0.0/0
|
|
protocol: all
|
|
ingress_security_rules: []
|
|
purge_security_rules: true
|
|
key_by: [compartment_id, vcn_id, display_name]
|
|
register: oci_sl
|
|
|
|
- name: Subnet
|
|
oracle.oci.oci_network_subnet:
|
|
<<: *oci_auth
|
|
compartment_id: "{{ oci_cid }}"
|
|
vcn_id: "{{ oci_vcn.vcn.id }}"
|
|
display_name: "{{ oci_instance_name }}"
|
|
cidr_block: "{{ oci_vcn_cidr }}"
|
|
dns_label: main
|
|
route_table_id: "{{ oci_rt.route_table.id }}"
|
|
security_list_ids: ["{{ oci_sl.security_list.id }}"]
|
|
prohibit_public_ip_on_vnic: false
|
|
key_by: [compartment_id, vcn_id, display_name]
|
|
register: oci_subnet
|
|
|
|
- name: Existing instance?
|
|
oracle.oci.oci_compute_instance_facts:
|
|
<<: *oci_auth
|
|
compartment_id: "{{ oci_cid }}"
|
|
display_name: "{{ oci_instance_name }}"
|
|
register: oci_existing
|
|
|
|
- name: Instances that aren't terminated
|
|
ansible.builtin.set_fact:
|
|
oci_live: "{{ oci_existing.instances | rejectattr('lifecycle_state', 'in', ['TERMINATED', 'TERMINATING']) | list }}"
|
|
|
|
- name: Latest Ubuntu 24.04 image for Arm
|
|
oracle.oci.oci_compute_image_facts:
|
|
<<: *oci_auth
|
|
compartment_id: "{{ vault_oci.tenancy }}"
|
|
operating_system: Canonical Ubuntu
|
|
operating_system_version: "24.04"
|
|
shape: "{{ oci_shape }}"
|
|
sort_by: TIMECREATED
|
|
sort_order: DESC
|
|
register: oci_images
|
|
when: oci_live | length == 0
|
|
|
|
# Free A1 capacity is often exhausted. Check every AD first and only
|
|
# launch where there is room: a launch without capacity just sits
|
|
# queued (ACCEPTED, 0%) and repeated tries pile up queued requests.
|
|
- name: A1 capacity per availability domain
|
|
oracle.oci.oci_compute_capacity_report:
|
|
<<: *oci_auth
|
|
compartment_id: "{{ vault_oci.tenancy }}"
|
|
availability_domain: "{{ item }}"
|
|
shape_availabilities:
|
|
- instance_shape: "{{ oci_shape }}"
|
|
instance_shape_config:
|
|
ocpus: "{{ oci_ocpus }}"
|
|
memory_in_gbs: "{{ oci_memory_gbs }}"
|
|
loop: "{{ ([oci_ad] + (oci_ads.availability_domains | map(attribute='name') | sort | list)) | unique }}"
|
|
register: oci_capacity
|
|
when: oci_live | length == 0
|
|
|
|
- name: Pick an availability domain with capacity
|
|
ansible.builtin.set_fact:
|
|
oci_launch_ad: "{{ oci_capacity.results
|
|
| selectattr('compute_capacity_report', 'defined')
|
|
| selectattr('compute_capacity_report.shape_availabilities.0.availability_status', 'equalto', 'AVAILABLE')
|
|
| map(attribute='item') | first | default('') }}"
|
|
when: oci_live | length == 0
|
|
|
|
- name: Stop if no availability domain has capacity
|
|
ansible.builtin.fail:
|
|
msg: >-
|
|
No {{ oci_shape }} {{ oci_ocpus }} OCPU / {{ oci_memory_gbs }} GB capacity in any
|
|
availability domain of {{ vault_oci.region }} right now
|
|
({{ oci_capacity.results | map(attribute='compute_capacity_report.shape_availabilities.0.availability_status', default='?') | join(', ') }}).
|
|
Nothing was launched; run oci.yml again later.
|
|
when: oci_live | length == 0 and oci_launch_ad == ''
|
|
|
|
# Created once; later runs never touch it (so a newer image can't
|
|
# trigger anything).
|
|
- name: Instance
|
|
oracle.oci.oci_compute_instance:
|
|
<<: *oci_auth
|
|
compartment_id: "{{ oci_cid }}"
|
|
availability_domain: "{{ oci_launch_ad }}"
|
|
display_name: "{{ oci_instance_name }}"
|
|
shape: "{{ oci_shape }}"
|
|
shape_config:
|
|
ocpus: "{{ oci_ocpus }}"
|
|
memory_in_gbs: "{{ oci_memory_gbs }}"
|
|
source_details:
|
|
source_type: image
|
|
image_id: "{{ (oci_images.images | rejectattr('display_name', 'search', 'Minimal') | first).id }}"
|
|
boot_volume_size_in_gbs: "{{ oci_boot_volume_gbs }}"
|
|
create_vnic_details:
|
|
subnet_id: "{{ oci_subnet.subnet.id }}"
|
|
assign_public_ip: true
|
|
hostname_label: cog04
|
|
metadata:
|
|
user_data: "{{ lookup('template', 'cogitator-04-cloud-init.yml.j2') | b64encode }}"
|
|
wait: true
|
|
wait_timeout: 900
|
|
register: oci_new
|
|
when: oci_live | length == 0
|
|
no_log: true # user_data contains the Tailscale auth key
|
|
|
|
- name: Instance id
|
|
ansible.builtin.set_fact:
|
|
oci_instance_id: "{{ (oci_live | first).id if oci_live | length > 0 else oci_new.instance.id }}"
|
|
oci_instance_ad: "{{ (oci_live | first).availability_domain if oci_live | length > 0 else oci_new.instance.availability_domain }}"
|
|
|
|
- name: Data volume (Prometheus and Loki)
|
|
oracle.oci.oci_blockstorage_volume:
|
|
<<: *oci_auth
|
|
compartment_id: "{{ oci_cid }}"
|
|
availability_domain: "{{ oci_instance_ad }}"
|
|
display_name: "{{ oci_instance_name }}-data"
|
|
size_in_gbs: "{{ oci_data_volume_gbs }}"
|
|
key_by: [compartment_id, display_name]
|
|
wait: true
|
|
register: oci_volume
|
|
|
|
- name: Attach the data volume (paravirtualized, /dev/oracleoci/oraclevdb)
|
|
oracle.oci.oci_compute_volume_attachment:
|
|
<<: *oci_auth
|
|
compartment_id: "{{ oci_cid }}"
|
|
instance_id: "{{ oci_instance_id }}"
|
|
volume_id: "{{ oci_volume.volume.id }}"
|
|
type: paravirtualized
|
|
device: /dev/oracleoci/oraclevdb
|
|
display_name: "{{ oci_instance_name }}-data"
|
|
key_by: [instance_id, volume_id]
|
|
wait: true
|
|
|
|
always:
|
|
- name: Remove the temporary key
|
|
ansible.builtin.file:
|
|
path: "{{ oci_key_dir.path }}"
|
|
state: absent
|
|
when: oci_key_dir.path is defined
|