2

I am installing postgres using ansible. Below is my ansible playbook which works perfectly.

- name: Install PostgreSQL
  hosts: all
  become: true

  vars_files:
    - variables.yaml

  tasks:
    - name: Install PostgreSQL repository key
      apt_key:
        url: https://www.postgresql.org/media/keys/ACCC4CF8.asc
        state: present

    - name: Add PostgreSQL repository
      apt_repository:
        repo: "deb http://apt.postgresql.org/pub/repos/apt/ {{ ansible_distribution_release }}-pgdg main"
        state: present

    - name: Install PostgreSQL server package
      apt:
        name: postgresql
        state: latest

    - name: Install specific PostgreSQL version
      apt:
        name: "postgresql-{{ postgres_version }}"
        state: present

Below is my variables.yaml file.

postgres_version: 14

I want to give the default value for postgres_version as 12 which is used only when the variable is not found. Any one any idea?

  • 2
    Does this answer your question? [Ansible - Use default if a variable is not defined](https://stackoverflow.com/questions/35105615/ansible-use-default-if-a-variable-is-not-defined) – juanlumn May 16 '23 at 10:04

1 Answers1

1

According to the pointed in this answer you can do something like this:

- name: Install specific PostgreSQL version
  apt:
    name: "postgresql-{{ postgres_version | default('12')}}"
    state: present
juanlumn
  • 6,155
  • 2
  • 30
  • 39