如何通过 Ansible Playbook 和 postgresql_query 模块自动化运行 PostgreSQL 数据库中的 SQL 查询或命令?

在 PostgreSQL 中运行 SQL 命令/查询

Ansible 模块:community.postgresql.postgresql_query

community.postgresql.postgresql_query 是 Ansible 社区维护的 PostgreSQL 模块集合的一部分,用于在 PostgreSQL 数据库中运行 SQL 查询。 该模块需要 Ansible Core 2.11 或更高版本,不支持早期版本(如 2.9 或 2.10)。

主要参数:

  • db(字符串):要连接并运行查询的数据库名称。
  • query(字符串):要运行的 SQL 查询。
  • positional_args / named_args(列表):传递给查询的参数,可以是位置参数或命名参数。
  • encoding(字符串):设置当前会话的客户端编码(例如 UTF-8)。
  • autocommit(布尔值):是否启用自动提交模式(默认关闭)。
  • login_user / login_password / login_unix_socket / login_host / port:连接参数,例如主机名、用户名、密码或 Unix 套接字。默认连接到本地主机(127.0.0.1)的 5432 端口,使用 postgres 用户登录。

注意事项:

  • 确保 PostgreSQL 的 peer 认证方法在连接参数中启用。
  • 该模块依赖于 Python 的 PostgreSQL 库 psycopg2,因此在使用此模块之前,必须确保目标主机上已安装 python3-psycopg2。

示例:通过 Ansible Playbook 在 PostgreSQL 中运行 SQL 查询

接下来,我们将通过一个实际的 Ansible Playbook 在 testdb 数据库中运行一个简单的查询,显示 PostgreSQL 服务器的当前版本。

Ansible Playbook 示例代码:

---
- name: PostgreSQL 示例
  hosts: all
  become: true
  vars:
    db_name: testdb
  tasks:
    - name: 确保 psycopg2 工具已安装
      ansible.builtin.package:
        name: python3-psycopg2
        state: present

    - name: 运行 SQL 查询
      community.postgresql.postgresql_query:
        db: "{{ db_name }}"
        query: SELECT version()
      become: true
      become_user: postgres
      register: sql_data

    - name: 打印查询结果
      ansible.builtin.debug:
        var: sql_data

执行过程:

$ ansible-playbook -i virtualmachines/demo/inventory postgresql/query_version.yml
PLAY [postgresql 示例] ******************************************************
TASK [收集主机信息] ******************************************************
ok: [demo.example.com]
TASK [确保 psycopg2 工具已安装] ******************************************************
ok: [demo.example.com]
TASK [运行 SQL 查询] ******************************************************
ok: [demo.example.com]
TASK [打印查询结果] ******************************************************
ok: [demo.example.com] => {
    "sql_data": {
        "changed": false,
        "failed": false,
        "query": "SELECT version()",
        "query_all_results": [
            [
                {
                    "version": "PostgreSQL 10.21 on x86_64-redhat-linux-gnu, compiled by gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-10), 64-bit"
                }
            ]
        ],
        "query_list": [
            "SELECT version()"
        ],
        "query_result": [
            {
                "version": "PostgreSQL 10.21 on x86_64-redhat-linux-gnu, compiled by gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-10), 64-bit"
            }
        ],
        "rowcount": 1,
        "statusmessage": "SELECT 1"
    }
}
PLAY RECAP *******************************************************************
demo.example.com           : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

幂等性测试:

$ ansible-playbook -i virtualmachines/demo/inventory postgresql/query_version.yml
PLAY [postgresql 示例] ******************************************************
TASK [收集主机信息] ******************************************************
ok: [demo.example.com]
TASK [确保 psycopg2 工具已安装] ******************************************************
ok: [demo.example.com]
TASK [运行 SQL 查询] ******************************************************
ok: [demo.example.com]
TASK [打印查询结果] ******************************************************
ok: [demo.example.com] => {
    "sql_data": {
        "changed": false,
        "failed": false,
        "query": "SELECT version()",
        "query_all_results": [
            [
                {
                    "version": "PostgreSQL 10.21 on x86_64-redhat-linux-gnu, compiled by gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-10), 64-bit"
                }
            ]
        ],
        "query_list": [
            "SELECT version()"
        ],
        "query_result": [
            {
                "version": "PostgreSQL 10.21 on x86_64-redhat-linux-gnu, compiled by gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-10), 64-bit"
            }
        ],
        "rowcount": 1,
        "statusmessage": "SELECT 1"
    }
}
PLAY RECAP *******************************************************************
demo.example.com           : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

说明:

  1. 查询结果:通过 register 将查询结果存储到变量 sql_data 中,并通过 debug 模块打印出来。
  2. 幂等性:由于查询操作不会修改数据库状态,因此多次运行 Playbook 时不会产生任何变化(changed 状态始终为 false)。

通过上述操作,我们成功地使用 Ansible 在 PostgreSQL 数据库 testdb 中运行了一个简单的 SQL 查询,并打印了 PostgreSQL 服务器的版本信息。