AnsiblePilot — Ansible Automation Platform and Enterprise Ansible

AnsiblePilot covers Red Hat Ansible Automation Platform (AAP) and enterprise Ansible: install and upgrade paths, Event-Driven Ansible, security and day-2 operations, plus Luca Berton's books, videos and training.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 9 technical books published by Apress and Leanpub, 4 of them on Ansible, including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Rename a PostgreSQL Database - Ansible module postgresql_db — Video Tutorial

\"How to rename a PostgreSQL database using Ansible postgresql_db module. Step-by-step playbook for database rename operations with error handling.\"testdb\".

Watch on YouTube · Read the written article

Tutorial summary

What you'll learn

  • How to Rename a PostgreSQL Database with Ansible?
  • Ansible Rename a PostgreSQL Database
  • Parameters
  • Links
  • code
  • execution
  • idempotency
  • before execution
  • after execution
  • Conclusion
How to Rename a PostgreSQL Database with Ansible? Ansible Rename a PostgreSQL Database - `community.postgresql.postgresql_db` - Add or remove PostgreSQL databases from a remote host Let's talk about the Ansible module `postgresql_db`. The full name is `community.postgresql.postgresql_db`, which means that is part of the collection of modules "community.postgresql" maintained by the Ansible Community to interact with PostgreSQL. The collection is tested with `ansible-core` version 2.11+, prior versions such as 2.9 or 2.10 are not supported. The purpose of the module is to add or remove PostgreSQL databases from a remote host. Parameters - name _string_ - Name of database - state _string_ - present/absent/dump/restore/rename - The operation Let me summarize the main parameters of the module `postgresql_db`. Ansible supposes that PostgreSQL is in the target node. The only required parameter is "name", the name of the database to interact with. The parameter "state" specify the desired state or the operation for the selected database. The option "present" means that the database should be Renamed and the option "absent" means that the database should be deleted. Other useful operations are "dump" and "restore" that uses `pg_dump`, the embedded PostgreSQL utility to backup and restore to the `target` file. Another useful operation is `rename`, from `name` to `target`. This module uses `psycopg2`, a Python PostgreSQL database library. You must ensure that `python3-psycopg2` is installed on the host before using this module. Links - [`community.postgresql.postgresql_db`](https://docs.ansible.com/ansible/latest/collections/community/postgresql/postgresql_db_module.html) ## Playbook Let's jump into a real-life Ansible Playbook to Rename a PostgreSQL Database. code ```yaml --- - name: postgresql Playbook hosts: all become: true vars: db_name: testdb db_newname: exampledb tasks: - name: Utility present ansible.builtin.package: name: python3-psycopg2 state: present - name: Rename db community.postgresql.postgresql_db: state: rename name: "{{ db_name }}" target: "{{ db_newname }}" become: true become_user: postgres ``` execution ```bash $ ansible-playbook -i virtualmachines/demo/inventory postgresql/db_rename.yml PLAY [postgresql Playbook] ************************************************************************************ TASK [Gathering Facts] ************************************************************************************ ok: [demo.example.com] TASK [Utility present] ************************************************************************************ ok: [demo.example.com] TASK [Rename db] ****************************************************************************************** changed: [demo.example.com] PLAY RECAP ************************************************************************************************ demo.example.com : ok=3

About this tutorial

  • Author: Luca Berton
  • Difficulty: Beginner
  • Read time: 4 min
  • Category: installation

Topics covered

Related video tutorials