无法使用ansible创建postgres数据库

时间:2019-03-19 13:10:07

标签: postgresql ansible

这是我的代码:

- name: Instalation of postgresql-9.6
  apt:
    name: postgresql-9.6

- name: start postgresql service
  service: name=postgresql state=restarted enabled=yes

- name: create a database
  become_user: postgres
  postgresql_db:
    name: managys
    encoding: UTF-8
    lc_collate: de_DE.UTF-8
    template: template0
    state: present

- name: crate user for database
  become: yes
  become_user: postgres
  postgresql_user:
    db: template0
    name: Odoo
    password: Odoo
    priv: ALL
    state: present

这是执行主要剧本后的错误:

完整的回溯是:

Traceback (most recent call last):
  File "/tmp/ansible_postgresql_db_payload_ce8a5D/__main__.py", line 421, in main
    db_connection = psycopg2.connect(database=maintenance_db, **kw)
  File "/usr/lib/python2.7/dist-packages/psycopg2/__init__.py", line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
OperationalError: FATAL:  Peer authentication failed for user "postgres"


fatal: [172.17.0.2]: FAILED! => {
    "changed": false, 
    "invocation": {
        "module_args": {
            "db": "managys", 
            "encoding": "UTF-8", 
            "lc_collate": "de_DE.UTF-8", 
            "lc_ctype": "", 
            "login_host": "", 
            "login_password": "", 
            "login_unix_socket": "", 
            "login_user": "postgres", 
            "maintenance_db": "postgres", 
            "name": "managys", 
            "owner": "", 
            "port": 5432, 
            "ssl_mode": "prefer", 
            "ssl_rootcert": null, 
            "state": "present", 
            "target": "", 
            "target_opts": "", 
            "template": "template0"
        }
    }, 
    "msg": "unable to connect to database: FATAL:  Peer authentication failed for user \"postgres\"\n"
}

1 个答案:

答案 0 :(得分:1)

begin_user:postgres仅在与ansible一起执行的用户位于目标服务器的sudoers文件中时才起作用。如果没有,那么我们必须更新postgres的配置文件。

下面的代码对我有用

{
  "currentIntent": {
    "name": "intent-name",
    "slots": {
      "slot name": "value",
      "slot name": "value"
    },
    "slotDetails": {
      "slot name": {
        "resolutions" : [
          { "value": "resolved value" },
          { "value": "resolved value" }
        ],
        "originalValue": "original text"
      },
      "slot name": {
        "resolutions" : [
          { "value": "resolved value" },
          { "value": "resolved value" }
        ],
        "originalValue": "original text"
      }
    },
    "confirmationStatus": "None, Confirmed, or Denied (intent confirmation, if configured)"
  },
  "bot": {
    "name": "bot name",
    "alias": "bot alias",
    "version": "bot version"
  },
  "userId": "User ID specified in the POST request to Amazon Lex.",
  "inputTranscript": "Text used to process the request",
  "invocationSource": "FulfillmentCodeHook or DialogCodeHook",
  "outputDialogMode": "Text or Voice, based on ContentType request header in runtime API request",
  "messageVersion": "1.0",
  "sessionAttributes": { 
     "key": "value",
     "key": "value"
  },
  "requestAttributes": { 
     "key": "value",
     "key": "value"
  }
}