2013-12-19 63 views
6

我使用Ansible来配置以Vagrant启动的虚拟机。我已经使用(首选的)VMware提供程序和VirtualBox进行测试,并且每个都得到相同的结果。Ansible没有设置Postgres用户密码

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

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

# 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"} 
+0

有趣的是,这似乎在物理盒子上工作,而不是在Vagrant虚拟机上。我会在一分钟后进一步调查。 –

+0

你有没有想过这个? – jrg

+0

不幸的不是。然而,相同的命令在用于数字海洋托管的生产箱的不同供应商中工作。 –

回答

-1

是不是因为你需要在连接时提供主机名?例如

$ psql -U django -h localhost so 

这就是我的解决方案。

编辑:

在剧本最后的任务将不会被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 

,然后你应该能够

$ psql -U django -h localhost so 

登录为Django的用户没有任何的角色设置,用户无法登录。我认为'LOGIN'角色必须隐含在那里指定的角色中,尽管我还没有在PostgreSQL文档中确认。

+0

这个问题是关于如何用可靠的,而不是命令行来做到这一点。你能否提供一个合理的任务来添加主机? – adeady