ttafsir.sqlite_utils.run_sql (1.4.0) — module

Execute SQL statements on an SQLite database.

Authors: Tafsir Thiam (@ttafsir)

Install collection

Install with ansible-galaxy collection install ttafsir.sqlite_utils:==1.4.0


Add to requirements.yml

  collections:
    - name: ttafsir.sqlite_utils
      version: 1.4.0

Description

This module allows you to execute SQL statements on an SQLite database.

For SELECT statements, it fetches and returns the data.

For non-SELECT statements, it returns the number of rows affected.

Supports only single SQL statements; multiple statements separated by semicolons are not supported.

The database file should exist, be a file (not a directory), and be readable.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Simple SELECT query without parameters
- name: Fetch data from database
  ttafsir.sqlite_utils.run_sql:
    db_path: /path/to/database.db
    query: "SELECT * FROM test;"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Parameterized SELECT query using a list
- name: Fetch data based on ID
  ttafsir.sqlite_utils.run_sql:
    db_path: /path/to/database.db
    query: "SELECT * FROM test WHERE id = ?;"
    params: [1]
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Parameterized SELECT query using a dictionary
- name: Fetch data based on name and age
  ttafsir.sqlite_utils.run_sql:
    db_path: /path/to/database.db
    query: "SELECT * FROM users WHERE name = :name AND age = :age;"
    params:
      name: "John"
      age: 25
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Update data based on ID
- name: Update data based on ID
  ttafsir.sqlite_utils.run_sql:
    db_path: database.sqlite
    query: "UPDATE emails SET subject = ? WHERE email_id = ?;"
    params: ["Hello World Updated", 1]
  sql_method: execute

Inputs

    
query:
    description:
    - SQL query string to execute. Only one statement is supported.
    required: true
    type: str

params:
    description:
    - Optional parameters for the SQL query. Can be either a list or a dictionary.
    - Helps in preventing SQL injection when used correctly.
    required: false
    type: raw

db_path:
    description:
    - Path to the SQLite database.
    - This path should point to an existing database file that is readable.
    required: true
    type: str

db_options:
    description:
    - Additional options passed to the sqlite_utils Database constructor.
    - Useful for advanced database configurations.
    required: false
    type: raw

sql_method:
    choices:
    - query
    - execute
    default: query
    description:
    - Specifies the type of SQL statement to execute.
    required: false
    type: str