Astra + Ansible

Astra DevOps API with Ansible


Follow me on GitHub

Asta DevOps API

The Astra DevOps API allows Astra users to create databases, keyspaces, and take other DevOps actions at an organization level. You will need to login to Astra UI, select your organization, and create a Service Account. Once you create a Service Account click into menu on right side and choose Copy Credentials. You should end up with a json object of the values you need. You can now leave the Astra UI behind and work entirely from within ansible.

DevOps Auth Post

Add your Service Account details to astra_auth_request_body, run the playbook, and grab the JWT token from the response. You will use this token for [token from devops_auth_post] in all playbook demos below.

---
- hosts: localhost
  connection: local
  gather_facts: no
  vars:
    astra_api_url: "https://api.astra.datastax.com/v2/authenticateServiceAccount"
    astra_auth_request_body:
      clientName: "[asta_service_account_clientName]"
      clientId: "[astra_service_account_clientId]"
      clientSecret: "[astra_service_account_clientSecret]"
  tasks:
    - name: create auth
      uri:
        url: "{{ astra_api_url }}"
        method: POST
        headers:
          Accept: "application/json"
          Content-Type: "application/json"
        body: "{{ astra_auth_request_body | to_json }}"
        status_code: 200
      register: response_auth
    - name: debug
      debug:
        var: response_auth

DevOps Create Database

---
- hosts: localhost
  connection: local
  gather_facts: no
  vars:
    astra_api_url: "https://api.astra.datastax.com/v2/databases"
    astra_auth_token: "[token from devops_auth_post]"
    astra_create_table:
      name: "table_name"
      keyspace: "keyspace_name"
      cloudProvider: "gcp"
      tier: "developer"
      capacityUnits: 1
      region: "us-east1"
      user: "username"
      password: "password"
  tasks:
    - name: create database
      uri:
        url: "{{ astra_api_url }}"
        method: POST
        headers:
          Accept: "application/json"
          Content-Type: "application/json"
          Authorization: "Bearer {{ astra_auth_token }}"
        body: "{{ astra_create_table | to_json }}"
        status_code: 201
      register: response_database
    - name: debug
      debug:
        var: response_database

DevOps Get Databases

In order to leverage api actions against a database, you need the database’s Cluster ID. You can find this in the Astra UI or you can use this playbook to get the id. You will need this ID for all [astra database id] instances in further playbooks.

---
- hosts: localhost
  connection: local
  gather_facts: no
  vars:
    astra_api_url: "https://api.astra.datastax.com/v2/databases"
    astra_auth_token: "[token from devops_auth_post]"
  tasks:
    - name: get databases
      uri:
        url: "{{ astra_api_url }}"
        method: GET
        headers:
          Accept: "application/json"
          Content-Type: "application/json"
          Authorization: "Bearer {{ astra_auth_token }}"
        status_code: 200
      register: response_databases
    - name: debug
      debug:
        var: response_databases

DevOps Add Keyspace to Database

When you create a database above, the keyspace above will be automatically created. This playbook allows you to add additional keyspaces.

---
- hosts: localhost
  connection: local
  gather_facts: no
  vars:
    astra_api_url: "https://api.astra.datastax.com/v2/databases/[cluster_id]/keyspaces/newKeyspace"
    astra_auth_token: "[token from devops_auth_post]"
  tasks:
    - name: add keyspace
      uri:
        url: "{{ astra_api_url }}"
        method: POST
        headers:
          Accept: "application/json"
          Content-Type: "application/json"
          Authorization: "Bearer {{ astra_auth_token }}"
        status_code: 201
      register: response_keyspace
    - name: debug
      debug:
        var: response_keyspace

DevOps Reset Database Password

---
- hosts: localhost
  connection: local
  gather_facts: no
  vars:
    astra_api_url: "https://api.astra.datastax.com/v2/databases"
    astra_auth_token: "[token from devops_auth_post]"
    astra_database_id: "[astra database id]"
    astra_user_password:
      username: "username"
      password: "password"
  tasks:
    - name: reset password
      uri:
        url: "{{ astra_api_url }}/{{ astra_database_id }}/resetPassword"
        method: POST
        headers:
          Accept: "application/json"
          Content-Type: "application/json"
          Authorization: "Bearer {{ astra_auth_token }}"
        body: "{{ astra_user_password | to_json }}"
        status_code: 202
      register: response_password
    - name: debug
      debug:
        var: response_password

DevOps Resize Database

---
- hosts: localhost
  connection: local
  gather_facts: no
  vars:
    astra_api_url: "https://api.astra.datastax.com/v2/databases"
    astra_auth_token: "[token from devops_auth_post]"
    astra_database_id: "[astra database id]"
    astra_resize_database:
      capacityUnits: 3
  tasks:
    - name: resize database
      uri:
        url: "{{ astra_api_url }}/{{ astra_database_id }}/resize"
        method: POST
        headers:
          Accept: "application/json"
          Content-Type: "application/json"
          Authorization: "Bearer {{ astra_auth_token }}"
        body: "{{ astra_resize_database | to_json }}"
        status_code: 202
      register: response_resize
    - name: debug
      debug:
        var: response_resize

DevOps Park Database

---
- hosts: localhost
  connection: local
  gather_facts: no
  vars:
    astra_api_url: "https://api.astra.datastax.com/v2/databases"
    astra_auth_token: "[token from devops_auth_post]"
    astra_database_id: "[astra database id]"
  tasks:
    - name: park database
      uri:
        url: "{{ astra_api_url }}/{{ astra_database_id }}/park"
        method: POST
        headers:
          Accept: "application/json"
          Content-Type: "application/json"
          Authorization: "Bearer {{ astra_auth_token }}"
        body: ""
        status_code: 202
      register: response_park
    - name: debug
      debug:
        var: response_park

DevOps Unpark Database

---
- hosts: localhost
  connection: local
  gather_facts: no
  vars:
    astra_api_url: "https://api.astra.datastax.com/v2/databases"
    astra_auth_token: "[token from devops_auth_post]"
    astra_database_id: "[astra database id]"
  tasks:
    - name: unpark database
      uri:
        url: "{{ astra_api_url }}/{{ astra_database_id }}/unpark"
        method: POST
        headers:
          Accept: "application/json"
          Content-Type: "application/json"
          Authorization: "Bearer {{ astra_auth_token }}"
        body: ""
        status_code: 202
      register: response_unpark
    - name: debug
      debug:
        var: response_unpark

DevOps Terminate Database

---
- hosts: localhost
  connection: local
  gather_facts: no
  vars:
    astra_api_url: "https://api.astra.datastax.com/v2/databases"
    astra_auth_token: "[token from devops_auth_post]"
    astra_database_id: "[astra database id]"
  tasks:
    - name: terminate database
      uri:
        url: "{{ astra_api_url }}/{{ astra_database_id }}/terminate"
        method: POST
        headers:
          Accept: "application/json"
          Content-Type: "application/json"
          Authorization: "Bearer {{ astra_auth_token }}"
        body: ""
        status_code: 202
      register: response_terminate
    - name: debug
      debug:
        var: response_terminate

DevOps Get Secure Bundle URL

---
- hosts: localhost
  connection: local
  gather_facts: no
  vars:
    astra_api_url: "https://api.astra.datastax.com/v2/databases"
    astra_auth_token: "[token from devops_auth_post]"
    astra_database_id: "[astra database id]"
  tasks:
    - name: get bundle
      uri:
        url: "{{ astra_api_url }}/{{ astra_database_id }}/secureBundleURL"
        method: POST
        headers:
          Accept: "application/json"
          Content-Type: "application/json"
          Authorization: "Bearer {{ astra_auth_token }}"
        body: ""
        status_code: 200
      register: response_bundle
    - name: debug
      debug:
        var: response_bundle

DevOps Get Supported Regions

---
- hosts: localhost
  connection: local
  gather_facts: no
  vars:
    astra_api_url: "https://api.astra.datastax.com/v2/availableRegions"
    astra_auth_token: "[token from devops_auth_post]"
  tasks:
    - name: return regions
      uri:
        url: "{{ astra_api_url }}"
        method: GET
        headers:
          Accept: "application/json"
          Content-Type: "application/json"
          Authorization: "Bearer {{ astra_auth_token }}"
        body: ""
        status_code: 200
      register: response_regions
    - name: debug
      debug:
        var: response_regions

How can I help you with Astra?

SRM

Find me over on the Astra Slack to ask me any questions about Astra. Also let’s chat if you have something kewl you did with Astra and you want me to feature it in my blog. Look below or to the right for more ways to find me.