Home > other >  Adding postgresql repo to ubuntu 22.04 via ansible fails
Adding postgresql repo to ubuntu 22.04 via ansible fails

Time:12-06

From the following 2 tasks

- name: Add key for Postgres repo
  apt_key: 
    url: https://www.postgresql.org/media/keys/ACCC4CF8.asc
    state: present
  become: true

- name: Add Postgres repo to sources list
  apt_repository:
    repo: 'deb http://apt.postgresql.org/pub/repos/apt/ ubuntu-pgdg main'
    state: present
  become: true

the second one fails as follows:

TASK [common : Add Postgres repo to sources list] ******************************
fatal: [master-node]: FAILED! => {"changed": false, "msg": "apt cache update failed"}

Why is that, given I am sudo-ing to execute it?

CodePudding user response:

The third field of an apt repository in the list file should be the codename of your distribution, not the distribution itself:

The sources.list man page specifies this package source format:

deb uri distribution [component1] [component2] [...]

and gives an example:

deb https://deb.debian.org/debian stable main contrib non-free

The distribution part (stable in this case) specifies a subdirectory in $ARCHIVE_ROOT/dists. It can contain additional slashes to specify subdirectories nested deeper, eg. stable/updates. distribution typically corresponds to Suite or Codename specified in the Release files.

Source: https://wiki.debian.org/DebianRepository/Format#Overview

Example of the Release file, containing the codename:

$ head -n 5 /etc/os-release 
PRETTY_NAME="Ubuntu 22.04.1 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.1 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy

If you don't want to hardcode it, this can be easily achieved with the help of Ansible facts:

- name: Add Postgres repo to sources list
  apt_repository:
    repo: >-
      deb http://apt.postgresql.org/pub/repos/apt/ 
      {{ ansible_distribution_release }}-pgdg main
    state: present
  become: true

Which presuppose that you, at least, gather the minimal facts, e.g.:

- hosts: localhost
  gather_subset:
    - min

Or that you gather everything, i.e.: that you have no gather_facts: false, at the play level.

  • Related