2015-10-05 123 views

回答

1

这是一个未经测试的例子,它显示了要领。它可能需要调整,具体取决于您的设置。

- name: Select all from example table 
    sudo: yes 
    sudo_user: postgres 
    command: psql -c "SELECT * FROM example_table" example_db 
    register: select_all_from_example 

- name: Display example table contents 
    debug: msg="{{ select_all_from_example.stdout }}" 
1

基于@Christofides的例子。我做了一些修改和测试。
这个工程:

- hosts: all 
    sudo: yes 
    sudo_user: postgres 
    tasks: 
    - name: Select all from example table 
     command: psql -c "SELECT * FROM example_table" example_db 
     register: select_all_from_example 

    - name : Display example table contents 
     debug: msg="{{ select_all_from_example.stdout }}" 

结果: enter image description here

+0

不错!谢谢yantaq!这确实有效。我只需要在格式上工作,但它看起来很有希望 – Sysadmin1234

+0

很高兴为你工作:) – yantaq

0

您可以使用提供了四个新模块一个新的角色:

  • postgresql_table:确保表是否存在(或不存在)in 数据库
  • postgresql_row:确保行中存在(或不存在)at能够
  • postgresql_query:在数据库中执行任意的查询和 返回结果
  • postgresql_command:在 数据库在本地执行任意查询

查找文档:https://github.com/rtshome/ansible_pgsql