New in version 2.8.
Parameter | Choices/Defaults | Comments |
---|---|---|
ca_cert
string
|
Specifies the name of a file containing SSL certificate authority (CA) certificate(s).
If the file exists, the server's certificate will be verified to be signed by one of these authorities.
aliases: ssl_rootcert |
|
db
string
|
Name of database to connect to and run queries against.
aliases: login_db |
|
login_host
string
|
Host running the database.
|
|
login_password
string
|
The password used to authenticate with.
|
|
login_unix_socket
string
|
Path to a Unix domain socket for local connections.
|
|
login_user
string
|
Default: "postgres"
|
The username used to authenticate with.
|
named_args
dictionary
|
Dictionary of key-value arguments to pass to the query.
Mutually exclusive with positional_args.
|
|
path_to_script
path
|
Path to SQL script on the remote host.
Returns result of the last query in the script.
Mutually exclusive with query.
|
|
port
integer
|
Default: 5432
|
Database port to connect to.
aliases: login_port |
positional_args
list
|
List of values to be passed as positional arguments to the query.
Mutually exclusive with named_args.
|
|
query
string
|
SQL query to run. Variables can be escaped with psycopg2 syntax http://initd.org/psycopg/docs/usage.html.
|
|
session_role
string
|
Switch to session_role after connecting. The specified session_role must be a role that the current login_user is a member of.
Permissions checking for SQL commands is carried out as though the session_role were the one that had logged in originally.
|
|
ssl_mode
string
|
|
Determines whether or with what priority a secure SSL TCP/IP connection will be negotiated with the server.
See https://www.postgresql.org/docs/current/static/libpq-ssl.html for more information on the modes.
Default of
prefer matches libpq default. |
Note
postgres
account on the host.postgresql
, libpq-dev
, and python-psycopg2
packages on the remote host before using this module.- name: Simple select query to acme db
postgresql_query:
db: acme
query: SELECT version()
- name: Select query to db acme with positional arguments and non-default credentials
postgresql_query:
db: acme
login_user: django
login_password: mysecretpass
query: SELECT * FROM acme WHERE id = %s AND story = %s
positional_args:
- 1
- test
- name: Select query to test_db with named_args
postgresql_query:
db: test_db
query: SELECT * FROM test WHERE id = %(id_val)s AND story = %(story_val)s
named_args:
id_val: 1
story_val: test
- name: Insert query to db test_db
postgresql_query:
db: test_db
query: INSERT INTO test_db (id, story) VALUES (2, 'my_long_story')
- name: Run queries from SQL script
postgresql_query:
db: test_db
path_to_script: /var/lib/pgsql/test.sql
positional_args:
- 1
Common return values are documented here, the following are the fields unique to this module:
Key | Returned | Description |
---|---|---|
query
string
|
always |
Query that was tried to be executed.
Sample:
SELECT * FROM bar
|
query_result
list
|
changed |
List of dictionaries in column:value form representing returned rows.
Sample:
[{'Column': 'Value1'}, {'Column': 'Value2'}]
|
rowcount
integer
|
changed |
Number of affected rows.
Sample:
5
|
statusmessage
string
|
always |
Attribute containing the message returned by the command.
Sample:
INSERT 0 1
|
Hint
If you notice any issues in this documentation you can edit this document to improve it.