在 Ansible Playbook 中指定 PostgreSQL 密码

Sys*_*234 2 postgresql ansible ansible-playbook

Ansible 新手,运行版本 2.1.0。我编写了一个 Ansible playbook,它针对一组主机运行 PostgreSQL 查询。当我在 shell 命令中指定 SQL DB 密码时,它可以工作,但我希望针对一组主机运行剧本,并且需要一种更好的方法来输入密码,因为它们都是唯一的。谁能提出更好的方法来做到这一点?

---

- 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 }}"
Run Code Online (Sandbox Code Playgroud)

我看到了有关该主题的另一个线程,但不确定如何实施该建议: 使用 ansible playbook 运行 postgresql 命令。Postgresql 需要密码

yda*_*coR 5

Ansible 允许您使用任何任务的environment参数为任务设置环境变量。

所以在你的情况下,你可以这样做:

  - 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 }}'
Run Code Online (Sandbox Code Playgroud)

然后pgpassword主机级别设置变量。