How to get postgresql_query results from Ansible

半腔热情 提交于 2021-02-10 18:37:17

问题


I'm trying to print the output of PostgreSQL query that is run by Ansible. Unfortunately I'm not sure how to get ahold of the return value.

    - name: Get specific tables
      postgresql_query:
        db: "{{ database_name }}"
        login_host: "{{ my_host }}"
        login_user: "{{ my_user }}"
        login_password: "{{ my_password }}"
        query: SELECT * FROM pg_tables t WHERE t.tableowner = current_user

Googling just says to use register:, but the PostgreSQL ansible module does not have a register param:

fatal: [xx.xxx.xx.xx]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (postgresql_query) module: register Supported parameters include: ca_cert, db, login_host, login_password, login_unix_socket, login_user, named_args, path_to_script, port, positional_args, query, session_role, ssl_mode"}

The Ansible docs list return values for this module but there are no examples on how to use them, and everything I search for leads right back to register:.


回答1:


Sounds like you are very close, but have register at the wrong indentation. It's a parameter of the task itself, not the postgresql module.

Try:

    - name: Get specific tables
      postgresql_query:
        db: "{{ database_name }}"
        login_host: "{{ my_host }}"
        login_user: "{{ my_user }}"
        login_password: "{{ my_password }}"
        query: SELECT * FROM pg_tables t WHERE t.tableowner = current_user
      register: result

    - debug:
        var: result


来源:https://stackoverflow.com/questions/57487357/how-to-get-postgresql-query-results-from-ansible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!