在Ansible Playbook中指定PostgreSQL密码
Ansible新增,运行2.1.0版。我已经编写了一个Ansible playbook,它对一组主机运行PostgreSQL查询。当我在shell命令中指定sqldb密码时,它可以工作,但是我希望针对一组主机运行playbook,并且需要一种更好的方法来输入密码,因为它们都是唯一的。有人能提出更好的方法吗在Ansible Playbook中指定PostgreSQL密码,postgresql,ansible,ansible-playbook,Postgresql,Ansible,Ansible Playbook,Ansible新增,运行2.1.0版。我已经编写了一个Ansible playbook,它对一组主机运行PostgreSQL查询。当我在shell命令中指定sqldb密码时,它可以工作,但是我希望针对一组主机运行playbook,并且需要一种更好的方法来输入密码,因为它们都是唯一的。有人能提出更好的方法吗 --- - hosts: Test_Hosts sudo: yes sudo_user: root gather_facts: yes tasks:
---
- hosts: Test_Hosts
sudo: yes
sudo_user: root
gather_facts: yes
tasks:
- name: Login to DB and run command
shell: export PGPASSWORD='Password'; psql -U 'user' -d 'db' -c 'select * FROM table';
register: select_all_from_table
- name: Display table contents
debug: msg="{{ select_all_from_table.stdout }}"
我看到了关于该主题的另一个线程,但不确定如何实现该建议:Ansible允许您使用设置任务的环境变量到任何任务 因此,在您的情况下,您可以这样做:
- name: Login to DB and run command
shell: psql -U 'user' -d 'db' -c 'select * FROM table';
register: select_all_from_table
environment:
PGPASSWORD: '{{ pgpassword }}'
然后将
pgpassword
变量设置为or级别。我今天遇到了这个问题,这对我来说很有用。在Linux上,您可以将所有凭据打包到一个~/.pgpass
隐藏文件中
只需在本地创建它(在本例中为/files/pgpass
),然后在运行psql命令之前使用ansible将其复制到主机上
- name: set passwd file for PSQL
copy:
src: files/pgpass
dest: ~/.pgpass
mode: 0600 ### important: will not work with wrong permissions
- name: PSQL command
shell: "psql -U 'user' -d 'db' -c 'select * FROM table'"
register: select_all_from_table
文件内容必须采用以下格式:
hostname:port:database:username:password
但是,您可以使用通配符,因此我的通配符如下所示,例如:
*:*:db1:user1:passwd1
*:*:db2:user2:passwd2
有关更多详细信息,请参阅文档:
谢谢您的回复,我将尝试您的建议。