7

我正在使用 Ansible 来配置一个从 Vagrant 开始的虚拟机。我已经使用(首选)VMware 提供程序和 VirtualBox 进行了测试,并且得到了相同的结果。

我正在使用以下一组任务来尝试创建一个名为 的数据库so,并拥有一个django具有访问权限的用户。但是,似乎没有设置数据库密码。如果我手动设置它,我可以连接,如果我事先尝试,我总是得到FATAL: password authentication failed for user "django".

我已经在下面发布了 Ansible 配置的相关部分,以及下面的调试相关部分(ansible.verbose = "vvv"在 vagrant 配置中)。

# Create Prostgres DB
- hosts: all
  sudo: True
  sudo_user: postgres

  vars:
    dbname: so
    dbuser: django
    dbpassword: 4967bKzCegrPxVH4tGgQe6kFn232t7KiFDXfedVi

  tasks: 
  - name: Ensure database exists
    postgresql_db: name={{ dbname }}

  - name: Ensure DB user has access to the DB
    postgresql_user: db={{ dbname }} name={{ dbuser }} password={{ dbpassword }} priv=ALL state=present

  # Leave user with ability to create databases. This prividge should be 
  # removed for production, but is required for running tests. 
    postgresql_user: name={{ dbuser }} role_attr_flags=NOSUPERUSER,CREATEDB

详细输出:

TASK: [Ensure DB user has access to the DB] *********************************** 
<127.0.0.1> ESTABLISH CONNECTION FOR USER: vagrant
<127.0.0.1> EXEC ['ssh', '-tt', '-q', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/Users/danielsgroves/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'Port=2222', '-o', 'IdentityFile=/Users/danielsgroves/.vagrant.d/insecure_private_key', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'User=vagrant', '-o', 'ConnectTimeout=10', '127.0.0.1', "/bin/sh -c 'mkdir -p /tmp/ansible-1387481596.93-132175356393082 && chmod a+rx /tmp/ansible-1387481596.93-132175356393082 && echo /tmp/ansible-1387481596.93-132175356393082'"]
<127.0.0.1> REMOTE_MODULE postgresql_user name=django role_attr_flags=NOSUPERUSER,CREATEDB
<127.0.0.1> PUT /var/folders/2j/n8ng8fdd5gj125w5zswg9kj00000gn/T/tmpvnrb37 TO /tmp/ansible-1387481596.93-132175356393082/postgresql_user
<127.0.0.1> EXEC ['ssh', '-tt', '-q', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/Users/danielsgroves/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'Port=2222', '-o', 'IdentityFile=/Users/danielsgroves/.vagrant.d/insecure_private_key', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'User=vagrant', '-o', 'ConnectTimeout=10', '127.0.0.1', "/bin/sh -c 'chmod a+r /tmp/ansible-1387481596.93-132175356393082/postgresql_user'"]
<127.0.0.1> EXEC ['ssh', '-tt', '-q', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/Users/danielsgroves/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'Port=2222', '-o', 'IdentityFile=/Users/danielsgroves/.vagrant.d/insecure_private_key', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'User=vagrant', '-o', 'ConnectTimeout=10', '127.0.0.1', '/bin/sh -c \'sudo -k && sudo -H -S -p "[sudo via ansible, key=isnxrvycjudgazbgyciehbcpiiswfczx] password: " -u postgres /bin/sh -c \'"\'"\'echo SUDO-SUCCESS-isnxrvycjudgazbgyciehbcpiiswfczx; /usr/bin/python /tmp/ansible-1387481596.93-132175356393082/postgresql_user\'"\'"\'\'']
<127.0.0.1> EXEC ['ssh', '-tt', '-q', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/Users/danielsgroves/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'Port=2222', '-o', 'IdentityFile=/Users/danielsgroves/.vagrant.d/insecure_private_key', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'User=vagrant', '-o', 'ConnectTimeout=10', '127.0.0.1', "/bin/sh -c 'rm -rf /tmp/ansible-1387481596.93-132175356393082/ >/dev/null 2>&1'"]
ok: [server] => {"changed": false, "user": "django"}
4

1 回答 1

-1

该剧本中的最终任务不会由 ansible 运行 - 它在模块名称前没有连字符。将其更改为:

- postgresql_user:
    name: "{{ dbuser }}"
    role_attr_flags: "NOSUPERUSER,CREATEDB"

或(我更喜欢命名所有任务)

- name: Set roles for DB user
  postgresql_user:
    name: "{{ dbuser }}"
    role_attr_flags: "NOSUPERUSER,CREATEDB"

然后您应该能够以 django 用户身份登录

$ psql -U django -h localhost so

如果没有设置任何角色,用户将无法登录。我认为 'LOGIN' 角色必须隐含在此处指定的角色中,尽管我尚未在 PostgreSQL 文档中确认这一点。

于 2014-02-10T05:39:49.560 回答