Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add postgresql_query module to database collection #20793

Closed

Conversation

wrouesnel
Copy link
Contributor

Note: resubmitted due to github not letting the branch be updated.

Issue Type:
  • New Module Pull Request
Plugin Name:

postgresql_query

Ansible Version:
ansible 2.1.0 (devel 11f081cb99) last updated 2016/02/20 18:38:49 (GMT +1100)
  lib/ansible/modules/core: (fix_docker_submit f90e143521) last updated 2016/02/20 22:35:25 (GMT +1100)
  lib/ansible/modules/extras: (detached HEAD postgres_q) last updated 2016/03/03 23:15:21 (GMT +1100)
  config file = 
  configured module search path = Default w/o overrides

Summary:

This patch adds the postgresql_query module. This fills a gap in ansible functionality in allowing running queries as part of a playbook to retrieve values or deploy applications (e.g. setting authentication in gogs).

Example output:

The following is an example response from a select query against a database which contains JSON strings:

- postgresql_query:
      db: "TestDb"
      host: localhost
      user: testuser
      password: testpassword
      sslmode: prefer
      query: |
        SELECT * FROM testtable;
TASK [postgresql_query] ********************************************************
ok: [localhost] => {"changed": false, "query_result": [{"columnA": "valueA"},{"columnA": "valueB"}], "rowcount": 1}

This patch adds a postgresql query module. This allows running arbitrary queries
from ansible scripts with safe escaping of of the supplied values.

@wrouesnel
Copy link
Contributor Author

Migrated from ansible/ansible-modules-extras#1802

@ansibot ansibot added affects_2.3 This issue/PR affects Ansible v2.3 community_review In order to be merged, this PR must follow the community review workflow. module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. new_module This PR includes a new module. new_plugin This PR includes a new plugin. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed community_review In order to be merged, this PR must follow the community review workflow. labels Jan 28, 2017
@wrouesnel wrouesnel force-pushed the ansible-modules-extras/pull/1802 branch from 7d03612 to f4fa368 Compare January 28, 2017 13:52
@bcoca bcoca removed the needs_triage Needs a first human triage before being processed. label Jan 30, 2017
@mattclay
Copy link
Member

@wrouesnel CI failure due to python 2.4 syntax error:

2017-01-28 13:57:13 Compiling ./lib/ansible/modules/database/postgresql/postgresql_query.py ...
2017-01-28 13:57:13   File "./lib/ansible/modules/database/postgresql/postgresql_query.py", line 196
2017-01-28 13:57:13     except Exception as e:
2017-01-28 13:57:13                       ^
2017-01-28 13:57:13 SyntaxError: invalid syntax

Here's an example of how to handle exceptions that will work with python 2.4+:

https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/files/file.py#L286

@mattclay mattclay added the ci_verified Changes made in this PR are causing tests to fail. label Feb 1, 2017
@wrouesnel wrouesnel force-pushed the ansible-modules-extras/pull/1802 branch from f4fa368 to bc99242 Compare March 8, 2017 15:22
@ansibot ansibot removed the ci_verified Changes made in this PR are causing tests to fail. label Mar 8, 2017
@wrouesnel wrouesnel force-pushed the ansible-modules-extras/pull/1802 branch 4 times, most recently from 7b103c4 to d9213d7 Compare March 8, 2017 16:11
@ansibot
Copy link
Contributor

ansibot commented Mar 9, 2017

The test ansible-test sanity --test pep8 failed with the following error:

lib/ansible/modules/database/postgresql/postgresql_query.py:170:11: E121 continuation line under-indented for hanging indent

The test ansible-test sanity --test validate-modules failed with the following error:

lib/ansible/modules/database/postgresql/postgresql_query.py:0:0: E316 ANSIBLE_METADATA.version: not a valid value for dictionary value @ data['version']. Got '0.1'

click here for bot help

@wrouesnel wrouesnel force-pushed the ansible-modules-extras/pull/1802 branch 2 times, most recently from 764d9ac to 1a65d6f Compare March 9, 2017 12:33
@ansibot
Copy link
Contributor

ansibot commented Mar 9, 2017

@b6d @dschep @jensdepuydt

As a maintainer of a module in the same namespace this new module has been submitted to, your vote counts for shipits. Please review this module and add shipit if you would like to see it merged.

click here for bot help

@ansibot ansibot added community_review In order to be merged, this PR must follow the community review workflow. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Mar 9, 2017
@wrouesnel wrouesnel force-pushed the ansible-modules-extras/pull/1802 branch from 1a65d6f to 7f17171 Compare March 15, 2017 12:56
@ansibot
Copy link
Contributor

ansibot commented Mar 15, 2017

The test ansible-test sanity --test ansible-doc --python 2.6 failed with the following error:

Command "ansible-doc postgresql_query" returned exit status 1.
>>> Standard Error
ERROR! module postgresql_query missing documentation (or could not parse documentation): 'metadata_version'

The test ansible-test sanity --test ansible-doc --python 2.7 failed with the following error:

Command "ansible-doc postgresql_query" returned exit status 1.
>>> Standard Error
ERROR! module postgresql_query missing documentation (or could not parse documentation): 'metadata_version'

The test ansible-test sanity --test ansible-doc --python 3.5 failed with the following error:

Command "ansible-doc postgresql_query" returned exit status 1.
>>> Standard Error
b"ERROR! module postgresql_query missing documentation (or could not parse documentation): 'metadata_version'"

The test ansible-test sanity --test ansible-doc --python 3.6 failed with the following error:

Command "ansible-doc postgresql_query" returned exit status 1.
>>> Standard Error
b"ERROR! module postgresql_query missing documentation (or could not parse documentation): 'metadata_version'"

The test ansible-test sanity --test validate-modules failed with the following errors:

lib/ansible/modules/database/postgresql/postgresql_query.py:0:0: E316 ANSIBLE_METADATA.metadata_version: required key not provided @ data['metadata_version']. Got None
lib/ansible/modules/database/postgresql/postgresql_query.py:0:0: E316 ANSIBLE_METADATA.version: extra keys not allowed @ data['version']. Got '1.0'

click here for bot help

@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed community_review In order to be merged, this PR must follow the community review workflow. labels Mar 15, 2017
@mattclay mattclay added the ci_verified Changes made in this PR are causing tests to fail. label Mar 17, 2017
@ansibot ansibot added the ci_verified Changes made in this PR are causing tests to fail. label Jun 17, 2017
@wrouesnel wrouesnel force-pushed the ansible-modules-extras/pull/1802 branch from 7a57cd6 to b8b4083 Compare June 17, 2017 13:55
@ansibot ansibot removed the ci_verified Changes made in this PR are causing tests to fail. label Jun 17, 2017
@ansibot
Copy link
Contributor

ansibot commented Jun 17, 2017

The test ansible-test sanity --test validate-modules failed with the following errors:

lib/ansible/modules/database/postgresql/postgresql_query.py:0:0: E307 version_added should be 2.4. Currently 2.3
lib/ansible/modules/database/postgresql/postgresql_query.py:0:0: E316 ANSIBLE_METADATA.metadata_version: required key not provided @ data['metadata_version']. Got None
lib/ansible/modules/database/postgresql/postgresql_query.py:0:0: E316 ANSIBLE_METADATA.version: extra keys not allowed @ data['version']. Got '1.0'

click here for bot help

@ansibot ansibot added the ci_verified Changes made in this PR are causing tests to fail. label Jun 17, 2017
@wrouesnel wrouesnel force-pushed the ansible-modules-extras/pull/1802 branch from b8b4083 to 95f089d Compare June 17, 2017 14:14
@ansibot ansibot removed the ci_verified Changes made in this PR are causing tests to fail. label Jun 17, 2017
@ansibot
Copy link
Contributor

ansibot commented Jun 17, 2017

The test ansible-test sanity --test pep8 failed with the following error:

lib/ansible/modules/database/postgresql/postgresql_query.py:156:1: E302 expected 2 blank lines, found 1

click here for bot help

@ansibot ansibot added the ci_verified Changes made in this PR are causing tests to fail. label Jun 17, 2017
This patch adds a postgresql query module. This allows running arbitrary queries
from ansible scripts with safe escaping of of the supplied values.

A custom type conversion for datetime -> string so exit_json() can work, the
full space of needed type conversions to return data is not yet known.
@wrouesnel wrouesnel force-pushed the ansible-modules-extras/pull/1802 branch from 95f089d to a6fa09a Compare June 17, 2017 16:18
@ansibot ansibot added community_review In order to be merged, this PR must follow the community review workflow. and removed ci_verified Changes made in this PR are causing tests to fail. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Jun 17, 2017
@wrouesnel
Copy link
Contributor Author

@b6d @dschep @jensdepuydt

This module fills out an important gap in Ansible's database functionality when setting up applicaitons which use Postgres. It'd be really nice to get it merged.

@archf
Copy link

archf commented Jun 18, 2017

@wrouesnel: I happy to see new progress on this. I made #25411 initially just because there was no activity on this PR for a long time. We all have busy lives. The code you did was a nice starting point and I think you nailed the way module input should be.

In #25411, I added new features, mainly the ability to throw .sql script as input file to module. I also shared/used code to make it somewhat more inline with other postgresql modules. I think community would benefit from this. Let's not make it a race in between 2 PR. I proposed we work together on #25411 and we add your authorship to it. Would you take a look and see if it works?

Regards,

@wrouesnel
Copy link
Contributor Author

@archf I'll take a look this week. So long as we wind up with good functionality I'm not fussed who's PR it is. This got caught up in me trying to get done traction on a bunch of old PRs this weekend.

@ansibot ansibot added stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. support:community This issue/PR relates to code supported by the Ansible community. labels Jun 27, 2017
@japsu
Copy link

japsu commented Sep 27, 2017

I propose we support the use case of ensuring some row exists in the database, such as creating an initial user for an application. Something like this:

- postgresql_query:
    query: INSERT INTO users (username, password_hash) VALUES (…, …)
    only_if_not_exists: SELECT id FROM users WHERE username = 'joeadmin'

What should the "run this query first and only if it returns and empty set, run the actual query" parameter be called? only_if_not_exists? test? ok_when?

The semantics should be that the query reports ok (not changed) iff the only_if_not_exists query returns a non-empty set.

@maxamillion
Copy link
Contributor

Is everyone happy with continuing the work on #25411? Can we close this one?

@maxamillion
Copy link
Contributor

needs_info

@ansibot ansibot added needs_info This issue requires further information. Please answer any outstanding questions. and removed community_review In order to be merged, this PR must follow the community review workflow. labels Feb 7, 2018
@ansibot
Copy link
Contributor

ansibot commented Mar 11, 2018

@wrouesnel This pullrequest is waiting for your response. Please respond or the pullrequest will be closed.

click here for bot help

@wrouesnel
Copy link
Contributor Author

I'm happy sorry 25411 capturing the work. Closing this.

@wrouesnel wrouesnel closed this Mar 11, 2018
@dagwieers dagwieers added the postgresql PostgreSQL community label Jan 28, 2019
@dagwieers dagwieers added the database Database category label Feb 13, 2019
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.3 This issue/PR affects Ansible v2.3 database Database category module This issue/PR relates to a module. needs_info This issue requires further information. Please answer any outstanding questions. new_module This PR includes a new module. new_plugin This PR includes a new plugin. postgresql PostgreSQL community stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. support:community This issue/PR relates to code supported by the Ansible community.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants