为何通过Ansible向Ubuntu22.04添加PostgreSQL仓库会失败?
Postgres仓库添加任务失败的常见原因及解决建议
你的Ansible任务代码
- 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
报错信息
TASK [common : Add Postgres repo to sources list] ****************************** fatal: [master-node]: FAILED! => {"changed": false, "msg": "apt cache update failed"}
可能的失败原因及解决方法
- 仓库发行版代号错误:你写的
ubuntu-pgdg不是有效的Ubuntu发行版代号(比如jammy、focal、bionic等),Postgres官方源的路径格式是[发行版代号]-pgdg。修正repo配置,用Ansible变量自动适配目标节点的发行版:repo: 'deb http://apt.postgresql.org/pub/repos/apt/ {{ ansible_distribution_release }}-pgdg main' - 网络访问问题:目标节点无法连接Postgres官方源服务器,可能是防火墙拦截、代理未配置或DNS解析失败。手动在目标节点执行
apt update,查看具体的网络相关报错,针对性解决。 - GPG密钥未正确导入:虽然第一个任务显示成功,但可能存在网络波动导致密钥未完全导入,apt验证仓库签名失败。在目标节点执行
apt-key list | grep ACCC4CF8检查密钥是否存在,若不存在则重新导入:curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - - 系统APT缓存损坏:目标节点的APT缓存可能已损坏,执行
sudo apt clean && sudo apt update手动清理并更新缓存,看是否能解决问题。
内容的提问来源于stack exchange,提问作者pkaramol
相关产品推荐
相关产品推荐

