Skip to content

Ansible custom module runs arbitrary SQL queries against MySQL database. This repository includes also a CI/CD pipline using Jenkins for building a test environment to deliver the module

Notifications You must be signed in to change notification settings

RamyChaabane/mysql_query

Repository files navigation

mysql_query

This repository includes also a CI/CD pipline using Jenkins for building a test environment to deliver the module

Synopsis

Ansible module runs arbitrary MySQL queries.

Requirements

The below requirements are needed on the host that executes this module.

PyMySQL

Parameters

Parameter Choices/Defaults Comments
autocommit
boolean
Choices:
false ←
true
Execute in autocommit mode when the query
db
string
Name of database to connect to and run queries against
login_host
string
Default:
localhost
Host running the database
should be execluded if config_file was provided
login_password
string
The password used to authenticate with
should be execluded if config_file was provided
login_unix_socket
string
Default:
/var/lib/mysql/mysql.sock
Path to a Unix domain socket
login_user
string
The username used to authenticate with
named_args
dictionary
Dictionary of key-value arguments to pass to the query
positional_args
list
List of values to be passed as positional arguments to the query
query
string
SQL query to run
fetchone
boolean
get only the first result of select SQL query
config_file
string
ini file containing client database credentials
If this parameter is specified, login_user and login_password should be excluded

Examples

- name: Select query to db acme with positional arguments
  mysql_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
  mysql_query:
    db: acme
    login_user: django
    login_password: mysecretpass
    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 test_table in db test_db
  mysql_query:
    db: acme
    login_user: django
    login_password: mysecretpass
    query: INSERT INTO test_table (id, story) VALUES (2, 'my_long_story')

- name: fetch only the first result of select SQL query
  mysql_query:
    db: test
    config_file: '/root/.my.cnf'
    query: select * from test_table
    fetchone: true

Return Values

key Returned Description
query
string
always Query that was tried to be executed
query_result
list
changed List of dictionaries in column:value form representing returned rows
rowcount
integer
always Number of affected rows.

About

Ansible custom module runs arbitrary SQL queries against MySQL database. This repository includes also a CI/CD pipline using Jenkins for building a test environment to deliver the module

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages