如何将此PostgreSQL命令转换为Ansible?

时间:2019-06-20 14:12:07

标签: postgresql ansible

我想通过Ansible在PostgreSQL 9.6中创建一个用户和一个数据库。

在shell中,我是这样做的: 须藤-u postgres createuser -S -D -R -P ckan_default createdb -O ckan_default ckan_default -E utf-8

我正在用Ansible塔运行所有内容。

我尝试过的方法有多种选择,例如删除“变得”和“变得_用户”:

- name: Create postgres user 
  become: yes
  become_user: postgres
  postgresql_user:
    name: "ckan_default"
    password: "ckan_default"
    role_attr_flags: NOCREATEDB, NOCREATEROLE, NOSUPERUSER

- name: Create DB
  become: yes
  become_user: postgres
  postgresql_db:
    name: ckan_default
    encoding: UTF-8
    owner: ckan_default

我遇到的错误:

{
    "_ansible_parsed": true,
    "exception": "Traceback (most recent call last):\n  File \"/tmp/ansible_postgresql_db_payload_FsaEtw/__main__.py\", line 421, in main\n    db_connection = psycopg2.connect(database=maintenance_db, **kw)\n  File \"build/bdist.linux-x86_64/egg/psycopg2/__init__.py\", line 126, in connect\n    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)\nOperationalError: FATAL:  Peer authentication failed for user \"postgres\"\n\n",
    "_ansible_no_log": false,
    "changed": false,
    "invocation": {
        "module_args": {
            "ssl_rootcert": null,
            "ssl_mode": "prefer",
            "target": "",
            "encoding": "UTF-8",
            "login_user": "postgres",
            "login_host": "",
            "target_opts": "",
            "db": "ckan_default",
            "lc_ctype": "",
            "owner": "",
            "port": 5432,
            "login_unix_socket": "",
            "state": "present",
            "template": "",
            "login_password": "",
            "maintenance_db": "postgres",
            "lc_collate": "",
            "name": "ckan_default"
        }
    },
    "msg": "unable to connect to database: FATAL:  Peer authentication failed for user \"postgres\"\n"
}

2 个答案:

答案 0 :(得分:0)

您想要将begin_pass变量设置为用户postgres的密码(例如将postgres_password wich设置为存储在Vault中的vault_postgres_password)

from ast import literal_eval

with open(match, 'r') as searchfile:
    for line in searchfile:
        if 'Turn' in line:
            line = next(searchfile)
            line = next(searchfile)
            if 'p-values' in line:
                fields = line.split(": ")
                print (fields)
                pvalues = literal_eval(fields[1].strip('\n'))

print (pvalues)
# [0.0, 0.0, 0.0, 0.0, 0.6326530612244898, 0.3673469387755102, 0.0]

在group_vars / my_postgres_hosts.yml文件中:

- name: Create postgres user 
  become: yes
  become_user: postgres
  become_pass: "{{ postgres_password }}"
  postgresql_user:
    name: "ckan_default"
    password: "ckan_default"
    role_attr_flags: NOCREATEDB, NOCREATEROLE, NOSUPERUSER

在group_vars / all / vault文件中:

postgres_password: "{{ vault_postgres_password }}"

也请看一下最近的这篇文章: ambassador_id

答案 1 :(得分:0)

离您不太远:事实证明,必须更改/ etc / sudoers。

添加了第2行和第4行。也不再需要成为:是

https://plot.ly/python/reference/#scatter-marker-symbol