- name:postgresql_query - simple select query to test_table
become_user:"{{ pg_user }}"
become:yes
postgresql_query:
login_user:"{{ pg_user }}"
db:postgres
query:SELECT * FROM test_table
register:result
ignore_errors:yes
- assert:
that:
- result.changed == false
- result.query == 'SELECT * FROM test_table'
- result.rowcount == 3
- result.statusmessage == 'SELECT 3' or result.statusmessage == 'SELECT'
- result.query_result[0].id == 1
- result.query_result[1].id == 2
- result.query_result[2].id == 3
- result.query_result[0].story == 'first'
- result.query_result[1].story == 'second'
- result.query_result[2].story == 'third'
# Select query with named_args:
- name:postgresql_query - select query with named args
become_user:"{{ pg_user }}"
become:yes
postgresql_query:
login_user:"{{ pg_user }}"
db:postgres
query:SELECT id FROM test_table WHERE id = %(id_val)s AND story = %(story_val)s
named_args:
id_val:1
story_val:first
register:result
ignore_errors:yes
- assert:
that:
- result.changed == false
- result.query == "SELECT id FROM test_table WHERE id = 1 AND story = 'first'" or result.query == "SELECT id FROM test_table WHERE id = 1 AND story = E'first'"
- result.rowcount == 1
- result.statusmessage == 'SELECT 1' or result.statusmessage == 'SELECT'
- result.query_result[0].id == 1
# Select query with positional arguments:
- name:postgresql_query - select query with positional arguments
become_user:"{{ pg_user }}"
become:yes
postgresql_query:
login_user:"{{ pg_user }}"
db:postgres
query:SELECT story FROM test_table WHERE id = %s AND story = %s
positional_args:
- 2
- second
register:result
ignore_errors:yes
- assert:
that:
- result.changed == false
- result.query == "SELECT story FROM test_table WHERE id = 2 AND story = 'second'" or result.query == "SELECT story FROM test_table WHERE id = 2 AND story = E'second'"
- result.rowcount == 1
- result.statusmessage == 'SELECT 1' or result.statusmessage == 'SELECT'
- result.query_result[0].story == 'second'
# Simple update query (positional_args and named args were checked by the previous tests):
- name:postgresql_query - simple update query
become_user:"{{ pg_user }}"
become:yes
postgresql_query:
login_user:"{{ pg_user }}"
db:postgres
query:UPDATE test_table SET story = 'new' WHERE id = 3
register:result
ignore_errors:yes
- assert:
that:
- result.changed == true
- result.query == "UPDATE test_table SET story = 'new' WHERE id = 3"
- result.rowcount == 1
- result.statusmessage == 'UPDATE 1'
- result.query_result == {}
# Try to update not existing row:
- name:postgresql_query - try to update not existing row
become_user:"{{ pg_user }}"
become:yes
postgresql_query:
login_user:"{{ pg_user }}"
db:postgres
query:UPDATE test_table SET story = 'new' WHERE id = 100
register:result
ignore_errors:yes
- assert:
that:
- result.changed == false
- result.query == "UPDATE test_table SET story = 'new' WHERE id = 100"
- result.rowcount == 0
- result.statusmessage == 'UPDATE 0'
- result.query_result == {}
# Simple insert query positional_args:
- name:postgresql_query - insert query
become_user:"{{ pg_user }}"
become:yes
postgresql_query:
login_user:"{{ pg_user }}"
db:postgres
query:INSERT INTO test_table (id, story) VALUES (%s, %s)
positional_args:
- 4
- fourth
register:result
ignore_errors:yes
- assert:
that:
- result.changed == true
- result.query == "INSERT INTO test_table (id, story) VALUES (4, 'fourth')" or result.query == "INSERT INTO test_table (id, story) VALUES (4, E'fourth')"