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

New module postgresql_query #52555

Merged
merged 12 commits into from
Feb 25, 2019
Merged

Conversation

Andersson007
Copy link
Contributor

SUMMARY
  • Runs arbitraty PostgreSQL queries.
  • Can run queries from SQL script files.
ISSUE TYPE
  • New Module Pull Request
EXAMPLES
- 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
RETURN
query:
    description: Query that was tried to be executed.
    returned: always
    type: str
    sample: 'SELECT * FROM bar'
statusmessage:
    description: Attribute containing the message returned by the command.
    returned: always
    type: str
    sample: 'INSERT 0 1'
query_result:
    description:
    - List of dictionaries in column:value form representing returned rows.
    returned: changed
    type: list
    sample: [{"Column": "Value1"},{"Column": "Value2"}]
rowcount:
    description: Number of affected rows.
    returned: changed
    type: int
    sample: 5

@Andersson007
Copy link
Contributor Author

+label postgresql
+label database

@ansibot
Copy link
Contributor

ansibot commented Feb 19, 2019

The test ansible-test sanity --test pylint [explain] failed with 1 error:

lib/ansible/modules/database/postgresql/postgresql_query.py:208:4: unreachable Unreachable code

click here for bot help

@ansibot
Copy link
Contributor

ansibot commented Feb 19, 2019

@ansibot ansibot added affects_2.8 This issue/PR affects Ansible v2.8 database Database category module This issue/PR relates to a module. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. 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. postgresql PostgreSQL community support:community This issue/PR relates to code supported by the Ansible community. test This PR relates to tests. labels Feb 19, 2019
@Andersson007
Copy link
Contributor Author

ready_for_review

@ansibot
Copy link
Contributor

ansibot commented Feb 19, 2019

@Dorn- @andytom @b6d @dschep @jbscalia @jensdepuydt @kostiantyn-nemchenko @kustodian @matburt @nerzhul @sebasmannem

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 Feb 19, 2019
Copy link
Contributor

@resmo resmo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks solid, two minors (no blockers)

@ansibot ansibot removed the needs_triage Needs a first human triage before being processed. label Feb 19, 2019
Copy link
Contributor

@gundalow gundalow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know much about Postgres, though just one minor comment

import psycopg2
HAS_PSYCOPG2 = True
except ImportError:
HAS_PSYCOPG2 = False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are you checking this?

In Ansible 2.8 we added ansible/community#346 (comment) which hopefully will give give clearer error messages to users.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good notice! Thank you, fixed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gundalow , is it necessary to use traceback here ?

import traceback

PSYCOPG2_IMP_ERR = None
...
    PSYCOPG2_IMP_ERR = traceback.format_exc()

I'm asking because I've recently heard that it's not necessary.
If we use module.fail_json(msg=missing_required_lib('psycopg2'), exception=PSYCOPG2_IMP_ERR)
the output is a little different:

The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_postgresql_ping_payload_HnnFhj/__main__.py", line 113, in <module>
    import psycopg2
ImportError: No module named psycopg2

spblnx176 | FAILED! => {
    "changed": false, 
    "invocation": {
        "module_args": {
            "db": null, 
            "login_host": "", 
            "login_password": "", 
            "login_unix_socket": "", 
            "login_user": "postgres", 
            "port": 5432, 
            "ssl_mode": "prefer", 
            "ssl_rootcert": null
        }
    }, 
    "msg": "Failed to import the required Python library (psycopg2) on spblnx176.lan's Python /usr/bin/python. Please read module documentation and install in the appropriate location"
}

module.fail_json(msg=missing_required_lib('psycopg2') without traceback

The full traceback is:
WARNING: The below traceback may *not* be related to the actual failure.
  File "/tmp/ansible_postgresql_ping_payload_y3izFs/__main__.py", line 111, in <module>
    import psycopg2

spblnx176 | FAILED! => {
    "changed": false, 
    "invocation": {
        "module_args": {
            "db": null, 
            "login_host": "", 
            "login_password": "", 
            "login_unix_socket": "", 
            "login_user": "postgres", 
            "port": 5432, 
            "ssl_mode": "prefer", 
            "ssl_rootcert": null
        }
    }, 
    "msg": "Failed to import the required Python library (psycopg2) on spblnx176.lan's Python /usr/bin/python. Please read module documentation and install in the appropriate location"
}

Should I add traceback to all my new modules including this or not ? (now I use just module.fail_json(msg=missing_required_lib('psycopg2') here)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what you've got it good, thanks

@Dorn-
Copy link
Contributor

Dorn- commented Feb 19, 2019

Hello, the code is looking good to me.
I'm just wondering if this module make sense, does Ansible is really needed to request a database. Is it really why Ansible have been created ?
The cons for me:

  • The module won't be idempotent (at least for select or others cmd drop , truncate ...)
  • AFAIK there are no query modules for other databases (mysql mongo ...).
  • This is not really the Ansible "mindset"

Pros:

  • This is a clean way to request the database, better than using the shell or cmd module

I don't have the answer tbh, this is more a philosophy question. Maybe I'm totally wrong, anyway I would be really glad to read your thought.

Kind regards.

@ansibot
Copy link
Contributor

ansibot commented Feb 19, 2019

The test ansible-test sanity --test pylint [explain] failed with 2 errors:

lib/ansible/modules/database/postgresql/postgresql_query.py:237:29: undefined-variable Undefined variable 'missing_required_lib'
lib/ansible/modules/database/postgresql/postgresql_query.py:237:73: undefined-variable Undefined variable 'PSYCOPG2_IMP_ERR'

click here for bot help

@ansibot ansibot added 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. and removed community_review In order to be merged, this PR must follow the community review workflow. labels Feb 19, 2019
@Andersson007
Copy link
Contributor Author

Andersson007 commented Feb 19, 2019

Hi, @Dorn- . Thank you! There is a lot of things that we can do using shell module but implemented by specific modules. At least, it’s more comfortable. Moreover, imho, it should be implemented for the other database modules too.

@Andersson007
Copy link
Contributor Author

Andersson007 commented Feb 19, 2019

plus we can use returned values

@ansibot ansibot removed the ci_verified Changes made in this PR are causing tests to fail. label Feb 19, 2019
@ansibot
Copy link
Contributor

ansibot commented Feb 19, 2019

The test ansible-test sanity --test pylint [explain] failed with 1 error:

lib/ansible/modules/database/postgresql/postgresql_query.py:237:29: undefined-variable Undefined variable 'missing_required_lib'

click here for bot help

@ansibot ansibot added the ci_verified Changes made in this PR are causing tests to fail. label Feb 19, 2019
@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 Feb 20, 2019
Copy link
Contributor

@resmo resmo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shipit

@ansibot ansibot added shipit This PR is ready to be merged by Core and removed community_review In order to be merged, this PR must follow the community review workflow. labels Feb 23, 2019
@Andersson007
Copy link
Contributor Author

@resmo , thank you for the review!

@gundalow gundalow merged commit be75c79 into ansible:devel Feb 25, 2019
@ansible ansible locked and limited conversation to collaborators Jul 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.8 This issue/PR affects Ansible v2.8 database Database category module This issue/PR relates to a module. new_module This PR includes a new module. new_plugin This PR includes a new plugin. postgresql PostgreSQL community shipit This PR is ready to be merged by Core support:community This issue/PR relates to code supported by the Ansible community. test This PR relates to tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants