Hello, I'm Maneshwar. I'm working on FreeDevTools online currently building **one place for all dev tools, cheat codes, and TLDRs* — a free, open-source hub where developers can quickly find and use tools without any hassle of searching all over the internet.
When you're automating infrastructure, a clean fol
der structure and roles make your Ansible setup scalable.
Here's how to install PostgreSQL the right way—with a proper Ansible layout and custom role.
Folder Structure
Start by generating a role using:
ansible-galaxy init roles/db --offline
This gives you a reusable structure inside your Ansible project:
ansible/
├── ansible.cfg
├── hosts.ini
├── install-db.yml
├── install_ansible.sh
├── requirements.yml
├── README.md
├── roles/
│ └── db/
│ ├── tasks/
│ │ └── main.yml # Actual installation steps
│ ├── defaults/ # Default vars if needed
│ ├── handlers/ # For service restart triggers
│ ├── meta/ # Role metadata
│ ├── templates/ # Jinja templates (unused here)
│ ├── files/ # Static files (unused here)
│ ├── vars/ # Hardcoded vars (unused here)
│ ├── tests/ # Smoke tests for the role
│ └── README.md
install-db.yml
This is your playbook entrypoint:
- name: Install PostgreSQL
hosts: all
become: true
roles:
- db
Run it using:
ansible-playbook -i hosts.ini install-db.yml
roles/db/tasks/main.yml
Here’s the PostgreSQL installation broken down into Ansible tasks:
- name: Update apt cache
apt:
update_cache: yes
- name: Install postgresql-common
apt:
name: postgresql-common
state: present
- name: Run PGDG setup script
command: /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
args:
creates: /etc/apt/sources.list.d/pgdg.list
- name: Update apt cache after PGDG script
apt:
update_cache: yes
- name: Install PostgreSQL and contrib
apt:
name:
- postgresql
- postgresql-contrib
state: present
Other Files (Optional)
-
ansible.cfg
: Set your default inventory and SSH options. -
hosts.ini
: Define target servers like so:
[db]
node1 ansible_host=192.168.1.10 ansible_user=ubuntu
-
install_ansible.sh
: If you want a script to bootstrap Ansible on your local. -
requirements.yml
: If you plan to use Galaxy roles (not used here).
Testing (optional)
Inside roles/db/tests/test.yml
, you could write a basic test to ensure PostgreSQL is installed.
- name: Test PostgreSQL installation
hosts: all
tasks:
- name: Check if postgres is installed
command: psql --version
register: pg_version
changed_when: false
failed_when: pg_version.rc != 0
- debug:
var: pg_version.stdout
Done
Now you've got:
- A structured Ansible project
- A reusable role (
db
) for PostgreSQL - Aneasy way to install and test PostgreSQL on any server
I’ve been building FreeDevTools.
A collection of UI/UX-focused tools crafted to simplify workflows, save time, and reduce friction in searching tools/materials.
Any feedback or contributors are welcome!
It’s online, open-source, and ready for anyone to use.
👉 Check it out: FreeDevTools
⭐ Star it on GitHub: freedevtools
Let’s make it even better together.
Top comments (0)