【问题标题】:PostgreSQL failing peer authentication with AnsiblePostgreSQL 使用 Ansible 的对等身份验证失败
【发布时间】:2014-11-03 06:09:01
【问题描述】:

我在 FreeBSD 上运行 PostgreSQL 9.3。 FreeBSD 使用pgsql 作为 PostgreSQL 的默认系统用户。我的/usr/local/pgsql/data/pg_hba.conf 看起来像这样:

# TYPE  DATABASE        USER            ADDRESS                 METHOD
local   all             pgsql                                   peer
local   all             all                                     md5
host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5

通过这个配置,我可以在没有密码的情况下以pgsql 的身份连接到数据库。

$ su pgsql
$ psql template1
template1=# \l
                         List of databases
...

按预期工作。

在远程机器上,我有一个 Ansible 任务来在 FreeBSD 服务器上创建一个数据库。

- name: Create the postgresql database
  postgresql_db: name=mydatabase login_user=pgsql

执行此任务失败,错误为Peer authentication failed for user "pgsql"

PLAY [web] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [host.example.org]

TASK: [database | Create the postgresql database] *****************************
failed: [host.example.org] => {"failed": true}
msg: unable to connect to database: FATAL:  Peer authentication failed for user "pgsql"


FATAL: all hosts have already failed -- aborting

当用户 pgsql 的对等身份验证明显有效时,为什么这会失败?

【问题讨论】:

  • 您必须以pgsql 用户身份运行ansible 模块吗?例如,将:sudo: truesudo_user: pgsql 添加到您的命令中
  • 您最终找到解决方案了吗?

标签: postgresql authentication freebsd ansible ansible-playbook


【解决方案1】:

这个问题是由于/etc/postgresql/10/main/pg_hba.conf配置了postgres认证方式,配置位置可能会根据安装的版本不同,/etc/postgresql //main/pg_hba.conf

sample ansible recipe
---
- name : "Dump PostgreSQL database"
  gather_facts: true
  hosts: posgre_host
  tasks:
    - name: Dump existing PostgreSQL database
      community.postgresql.postgresql_db:
        name: demo
        state: dump
        target: /tmp/backup/backup.sql.gz
      become: true

来自

local           all           postgres           peer

local           all             postgres        trust

这里信任认证方式任何连接到服务器的人都有权访问数据库,由于ansible是基于ssh工作的,所以可以安全地更改。

【讨论】:

    【解决方案2】:

    如果您没有 sudo(debian 等)但可以访问 root

    - name: Create database
      remote_user: root
      become: yes
      become_method: su
      become_user: postgres
      postgresql_db:
        name: my_db
    

    【讨论】:

      【解决方案3】:

      对于那些遇到“无法对 Ansible 需要创建的临时文件设置权限...”以切换到具有 become_userpostgres 用户的用户,您可以在 Ubuntu 主机上利用流水线。

      在您的 playbook 目录中创建 ansible.cfg 并添加以下行:

      [ssh_connection]
      pipelining=True
      

      更新:根据@lolcode Ansible 2.9.0 已更新为ansible_pipelining

         [ssh_connection]
         ansible_pipelining = true
      

      2020 年 4 月 30 日更新: 对于仍有问题的用户,请尝试安装 acl,这将导致 Ansible 使用此 acl 文件系统来挂载需要由第二个用户访问的模块而不是让每个人都可以阅读它们。谢谢@Andreas Florath

      - name: install setfacl support
        become: yes
        apt: pkg=acl
      

      【讨论】:

      • 感谢您节省了我的生命:)
      • 没问题!无数人为我做过同样的事情。传下去。
      • 管道 = True 答案对我有用,但在 ansible 2.9.0 上,格式已更改为 ansible_pipelining = true 根据 [this ansible issue][1] [1]:github.com/ansible/ansible/issues/31125#issuecomment-333847610
      • 这对我不起作用。我需要按照描述安装 acl 工具:stackoverflow.com/questions/36646880/…
      • @DylanPierce 请注意,您在编辑中拼写了ansible_pipelining 有点错误(您写了piplining)。
      【解决方案4】:

      我注意到您的 Postgres 版本确实过时了 (9.3)。我最近在使用 Postgres 9.3 的 Ubuntu 14 服务器上工作时遇到了这个问题。

      我尝试了十几种不同的方法,最后奏效的是通过 apt 安装 acl 软件包。 Ansible 使用它来解决它的一些权限问题。该软件包默认安装在较新的发行版上,因此我只在旧服务器上看到此问题。

      【讨论】:

        【解决方案5】:

        由于这个威胁,我制作了 mdh 帖子的变体。当我建立一个数据库时,我为 postgres 用户生成一个密码,并将它存储在根目录下的一个文件中。

        我想为什么不将它也(或相反)存储在根的 .pgpass 文件中。所以我创建了一个这样的模板(只有最后一行很重要):

        #### password file for posgres connection ###
        
        #### *:*:*:*
        #### works like
        ####    *      :      *    :     *     :     *
        #### <ip addr> : <port nr> : <db name> : <password>
        
        127.0.0.1:*:*:postgres:{{ new_postgres_pass }}
        

        将 .pgpass 文件存储在 root 的主目录中。现在您可以以 root 身份使用该模块,而无需切换用户而不必更改 pg_hba.conf:

        - name: Ensure postgresql mydatabase
          postgresql_db:
            name: mydatabase
            login_user: postgres
            login_host: 127.0.0.1
        

        【讨论】:

          【解决方案6】:

          这对我有用:

          - name: Create postgres database
            become: true
            become_user: postgres
            postgresql_db:
              name: <database-name>
          

          在您的具体情况下,用户可能是pgsql,但我认为通常用户是postgres

          【讨论】:

            【解决方案7】:

            另一种解决方法是通过host (localhost) 而不是默认的local 对等身份验证方法进行连接:

            - name: Create the postgresql database
              postgresql_db:
                name: mydatabase
                login_user: postgres
                login_host: 127.0.0.1
            

            根据pg_hba.conf 中的设置,您可能还需要提供login_password。您可以通过设置来规避此问题

            host    all         postgres        127.0.0.1/32            md5
            

            host    all         postgres        127.0.0.1/32            trust
            

            【讨论】:

            • 你间接帮助了我。我正在使用 login_host:“{{ item.login_host | default('localhost') }}”。切换到默认('省略')解决了我的问题。
            【解决方案8】:

            我遇到了同样的问题。就我而言,我忽略了我将 Ansible-playbook 配置为作为另一个 Linux 用户运行,而不是具有对等访问权限的用户(在你的情况下为 pgsql)。解决方案是将 Ansible play 作为 pgsql 运行:

            - name: Create the postgresql database
              remote_user: pgsql
              postgresql_db: name=mydatabase login_user=pgsql
              ...
            

            或者以 root 身份运行,然后 su 到 pgsql 获取命令:

            - name: Create the postgresql database
              remote_user: root
              become: yes
              become_user: pgsql
              postgresql_db: name=mydatabase login_user=pgsql
              ...
            

            ...取决于您通过 ssh 的访问权限。

            这是使用 Ansible 2.0。

            【讨论】:

              【解决方案9】:

              或者使用稍微不同的语法(来自 Ansible 1.9)和用户创建(可能对某人有帮助)

              - name: Create postgres user
                postgresql_user: name={{ pg_user }} password={{ pg_password }}
                become: true
                become_user: postgres
              

              【讨论】:

              • Ansible sudo: yes; sudo_user: postgres
              【解决方案10】:

              所以如果我理解你在远程机器上,也许你应该更改 /usr/local/pgsql/data/pg_hba.conf 以允许远程连接“host all all 0.0.0.0/0 md5”或其他特定的网络地址。

              【讨论】:

                猜你喜欢
                • 2014-07-15
                • 2020-11-30
                • 2013-07-23
                • 2011-12-31
                • 2014-03-17
                • 2013-02-24
                • 2016-03-19
                • 2015-06-18
                • 2019-12-14
                相关资源
                最近更新 更多