1

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?

3
  • 1
    It looks like just a typo. Under the URL apt.postgresql.org/pub/repos/apt/dists are no ubuntu-pgdg. Shouldn't it be something like {{ YOUR_DISTRO }}-pgdg? Commented Dec 5, 2022 at 15:53
  • you mean like jammy-pgdg ? Commented Dec 5, 2022 at 16:12
  • Yes, like that. Commented Dec 5, 2022 at 16:36

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.