Restart all your always free DBs

Based on Franck Pachots Blog about the unexpected downtime of the always free tier Databases I wrote an ansible playbook which restarts all your free tier databases. Improvements are welcome.

The Playbook is very simple.

---
- name: Restart all Always Free DBs
  hosts: localhost
  connection: local
  vars:
    compartment_ocid: "ocid1.tenancy.oc1..aaaaaaaafks433vraror3fo5h2pdld75q6az2uwxvuw67onypqq7uvn6gmea"
  tasks:

  - name: Get details of the compartment
    oci_compartment_facts:
      compartment_id: "{{ compartment_ocid }}"
    register: compartmentList

  - name: Set Fact compartment_id list
    set_fact:
      compartmentIdList: "{{ compartmentList | to_json | from_json | json_query(query) }}"
    vars:
      query: "compartments[*].id"

  - name: Get region details
    oci_region_subscription_facts:
      id: "{{ compartment_ocid }}"
    register: regionSub

  - name: Set Fact compartment_id list
    set_fact:
      regionSub: "{{ regionSub | to_json | from_json | json_query(query) }}"
    vars:
      query: "region_subscriptions[*].region_name"

  - name: "OCI Autonomous Facts"
    oci_autonomous_database_facts:
      is_free_tier: True
      compartment_id: "{{ item.0 }}"
      region: "{{ item.1 }}"
    loop: "{{ compartmentIdList | product(regionSub)|list}}"
    register: alwaysFreeDBs

  - name: Restart DBs
    include_tasks: "oci_restart_db.yml"
    vars:
      DBList: "{{ item }}"
    loop: "{{ alwaysFreeDBs.results }}"
    when: item.autonomous_databases | length > 0

In the “Get details of the compartment” Task I get the list of all sub compartments to search for autonomous databases. Afterwards I generate a list of ids in the “Set Fact compartment_id list” task. Then I generate a list of all regions this tenancy is subscribed to (“Get region details”) and build a list of ids (“Set Fact compartment_id list”). With this information I am able to loop all compartments and regions to create a list of always free DBs (“OCI Autonomous Facts”).
With this information I can loop thru these always free dbs. This loop includes the tasks in the file oci_restart_db.yml.

And in the included tasks file I restart the databases:

- name: "Set Region Fact"
  set_fact:
    region: "{{ DBList.item[1] }}"

- name: Stop DB
  oci_autonomous_database:
    autonomous_database_id: "{{ stopDB.id }}"
    compartment_id: "{{ stopDB.compartment_id }}"
    state: 'stop'
    region: "{{ region }}"
  loop: "{{ DBList.autonomous_databases }}"
  loop_control:
    loop_var: stopDB
  when:
    - stopDB.lifecycle_state  == 'AVAILABLE'
    - stopDB.is_free_tier == True

- name: Stop DB 
  oci_autonomous_database:
    autonomous_database_id: "{{ startDB.id }}"
    compartment_id: "{{ startDB.compartment_id }}"
    state: 'start'
    region: "{{ region }}"
  loop: "{{ DBList.autonomous_databases }}"
  loop_control:
    loop_var: startDB
  when:
    - startDB.is_free_tier == True
    - startDB.lifecycle_state == 'AVAILABLE'

Leave a Reply

Your email address will not be published. Required fields are marked *