oci.yml: cogitator-04 on Oracle Cloud Always Free (A1.Flex 2 OCPU/6 GB, 50+50 GB)
Compartment, VCN, internet gateway, route table, egress-only security list, subnet, instance (created once, cloud-init creates phil and joins the tailnet; no inbound rules at all), 50 GB data volume (paravirtualized). Asserts Always Free limits and a 100 GB storage budget; retries 'out of host capacity'. Adds oracle.oci to requirements.yml. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4e58c3d320
commit
629abc0a96
3 files changed
+244
-1
No files matched your search
@@ -0,0 +1,223 @@
|
|||||||
|
# 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 # retry on another AD with -e oci_ad_index=2
|
||||||
|
oci_vcn_cidr: 10.40.0.0/24
|
||||||
|
oci_auth: &oci_auth
|
||||||
|
api_user: "{{ vault_oci.user }}"
|
||||||
|
api_user_fingerprint: "{{ vault_oci.fingerprint }}"
|
||||||
|
api_user_key_file: "{{ oci_key_dir.path }}/key.pem"
|
||||||
|
tenancy: "{{ vault_oci.tenancy }}"
|
||||||
|
region: "{{ vault_oci.region }}"
|
||||||
|
|
||||||
|
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: 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
|
||||||
|
|
||||||
|
# Created once; later runs never touch it (so a newer image can't
|
||||||
|
# trigger anything). "Out of host capacity" is retried.
|
||||||
|
- name: Instance
|
||||||
|
oracle.oci.oci_compute_instance:
|
||||||
|
<<: *oci_auth
|
||||||
|
compartment_id: "{{ oci_cid }}"
|
||||||
|
availability_domain: "{{ oci_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
|
||||||
|
register: oci_new
|
||||||
|
retries: 30
|
||||||
|
delay: 60
|
||||||
|
until: oci_new is succeeded or 'capacity' not in (oci_new.msg | default('') | lower)
|
||||||
|
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
|
||||||
+3
-1
@@ -1,3 +1,5 @@
|
|||||||
---
|
---
|
||||||
roles:
|
roles:
|
||||||
- name: artis3n.tailscale
|
- name: artis3n.tailscale
|
||||||
|
collections:
|
||||||
|
- name: oracle.oci # oci.yml (cogitator-04); also needs the `oci` Python SDK in .venv
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#cloud-config
|
||||||
|
# First boot of cogitator-04 (passed by oci.yml). Replaces bootstrap.yml for
|
||||||
|
# this host: creates phil, joins the tailnet. The instance has no inbound
|
||||||
|
# rules at all; Ansible reaches it over Tailscale afterwards.
|
||||||
|
hostname: {{ oci_instance_name }}
|
||||||
|
users:
|
||||||
|
- default
|
||||||
|
- name: phil
|
||||||
|
groups: [sudo]
|
||||||
|
shell: /bin/bash
|
||||||
|
sudo: "ALL=(ALL) NOPASSWD:ALL"
|
||||||
|
lock_passwd: true
|
||||||
|
ssh_authorized_keys:
|
||||||
|
- {{ lookup('file', lookup('env', 'HOME') + '/.ssh/id_ed25519.pub') }}
|
||||||
|
package_update: true
|
||||||
|
runcmd:
|
||||||
|
- curl -fsSL https://tailscale.com/install.sh | sh
|
||||||
|
- tailscale up --authkey={{ vault_infrastructure.tailscale_key }} --hostname={{ oci_instance_name }}
|
||||||
Reference in new issue
Block a user