02a__create_postgres_cluster.yml.md

The source files for this tutorial can be downloaded from its GitHub repository, or cloned into your current working directory using the command git clone https://github.com/ionos-cloud/module-ansible.git before changing into the module-ansible/docs/tutorials/09__a_quick_introduction_to_dbaas sub-directory.

01__create_jumpbox_and_nlb.yml
---
- hosts: localhost
  connection: local
  gather_facts: false

  vars_files:
    - ../vars.yml
    - vars.yml

  


  tasks:
    # =======================================================================
    - name: Get information about the LANs in '{{ datacenter_name }}'
      ionoscloudsdk.ionoscloud.lan_info:
        datacenter: "{{ datacenter_name }}"
      register: lan_info_response


    - name: Set the fact 'internal_lan' based on the above
      ansible.builtin.set_fact:
        internal_lan: "{{ (lan_info_response | json_query(query))[0] }}"
      vars:
        query: "lans[?properties.name=='{{ lan.name }}']"


    - name: Get information about our reserved IP Blocks
      ionoscloudsdk.ionoscloud.ipblock_info:
        filters: "{ 'properties.name': 'IP Block for {{ datacenter_name }}' }"
      register: ipblock_info_response
      when: ENABLE_EXPLICITLY_UNSUPPORTED_CONFIGURATIONS


    - name: Set 'ip_block' based on the above
      ansible.builtin.set_fact:
        ip_block: "{{ ipblock_info_response.ipblocks[0].properties.ips }}"
      when: ENABLE_EXPLICITLY_UNSUPPORTED_CONFIGURATIONS


    - name: Get information about the servers in our data center
      ionoscloudsdk.ionoscloud.server_info:
        datacenter: "{{ datacenter_name }}"
        depth: 5
      register: get_server_response


    - name: Retrieve the object describing the jumpbox
      ansible.builtin.set_fact:
        desired_server_info__query: "{{ get_server_response | json_query(query) }}"
      vars:
        query: "servers[?properties.name=='{{ server_config['jumpbox'].name }}']"


    - name: Retrieve the jumpbox's public IP
      ansible.builtin.set_fact:
        jumpbox_ip: "{{ (desired_server_info__query[0] | json_query(query))[0] }}"
      vars:
        query: "entities.nics.items[?properties.name!='{{ server_config['jumpbox'].name }}.eth1'].properties.ips[]"




    # =======================================================================
    # See https://docs.ionos.com/ansible/api/dbaas-postgres/postgres_cluster
    - name: Create Postgres Cluster
      ionoscloudsdk.ionoscloud.postgres_cluster:
        postgres_version: 12
        instances: 1
        cores: "{{ dbaas_config.postgres_cluster.cores }}"
        ram: "{{ dbaas_config.postgres_cluster.ram }}"
        storage_size: "{{ dbaas_config.postgres_cluster.volume_size }}"
        storage_type: "{{ dbaas_config.postgres_cluster.storage_type }}"
        location: "{{ location }}"
        connections:
          - cidr: "{{ dbaas_config.postgres_cluster.ip }}/24"
            datacenter: "{{ datacenter_name }}"
            lan: "{{ internal_lan.id }}"
        display_name: "Postgres Cluster for {{ datacenter_name }}"
        synchronization_mode: ASYNCHRONOUS
        db_username: dbadmin
        db_password: "{{ default_password }}"

        wait: true
        wait_timeout: "{{ wait_timeout }}"
      register: postgres_cluster_response    




    # =======================================================================
    - name: Print usage information
      ansible.builtin.debug:
        msg:
          - "To access your Postgres Cluster via, e.g., psql, use one of the following commands:"
          - "    From the jumpbox:"
          - "        psql -h {{ dbaas_config.postgres_cluster.ip }} -U dbadmin postgres"
          - "    Externally via the jumpbox:"
          - "        In one shell:     ssh -L 5432:{{ postgres_cluster_response.postgres_cluster.properties.dns_name }}:5432 root@{{ jumpbox_ip }}"
          - "        In another shell: psql -h localhost -U dbadmin postgres"
          - "

<div data-gb-custom-block data-tag="if">    Externally via the NLB:</div>"
          - "<div data-gb-custom-block data-tag="if">        psql -h {{ ip_block[0] }} -U dbadmin postgres</div>

"

Last updated