mariadb-database-exists

Example:

# Install MariaDB and create a MySQL database and user with full access to it.
- mariadb-database-exists:
    db_name: my_database
    db_user: freckles
    db_password: password123

Description

Installs MariaDB service on a host, and ensures a database with the provided name is present.

If a database dump file is provided, the database will be imported from it. Otherwise an empty table will be created.

If a database dump file is provided, the database name can also be 'all', in which case all databases contained in it will be imported.

If a username is supplied, but no db_user_priv, the user 'db_name.*:ALL' privileges granted.

Resources

Variables

Name Type Default Description

db_name

string --

The name of the database to use from the dump (or 'all'). Required

db_dump_file

string --

An (optional) database dump file.

db_host

string --

The 'host' part of the MySQL username.

db_import

boolean False

Whether to use a sql dump file to create the database.

db_user

string --

The name of the database user.

db_user_password

string --

The password for the database user.

db_user_priv

string --

The user privileges.

mysql_group_id

integer --

An (optional) custom mysql group id

mysql_user_id

integer --

An (optional) custom mysql user id

Examples

Example 1

Install MariaDB and create a MySQL database and user with full access to it.

Code
- mariadb-database-exists:
    db_name: my_database
    db_user: freckles
    db_password: password123
Description

Be aware, that if you do it like this the user password is used in plain text, which could be a security issue. Please check the freckles security documentation for details and options.

Code

doc:
  short_help: Installs MariaDB (if necessary), and makes sure a specified database
    exists.
  help: |
    Installs MariaDB service on a host, and ensures a database with the provided name is present.

    If a database dump file is provided, the database will be imported from it.
    Otherwise an empty table will be created.

    If a database dump file is provided, the database name can also be 'all',
    in which case all databases contained in it will be imported.

    If a username is supplied, but no ``db_user_priv``, the user 'db_name.*:ALL' privileges granted.


  references:
    "'mysql_user' Ansible module": https://docs.ansible.com/ansible/latest/modules/mysql_user_module.html
    "'mysql_db' Ansible module": https://docs.ansible.com/ansible/latest/modules/mysql_db_module.html

  examples:
  - title: Install MariaDB and create a MySQL database and user with full access to
      it.
    desc: |
      Be aware, that if you do it like this the user password is used in plain text, which could be a security issue.
      Please check [the freckles security documentation](https://freckles.io/doc/security/) for details and options.
    vars:
      db_name: my_database
      db_user: freckles
      db_password: password123

args:
  _import: mariadb-service
#  mysql_group_id:
#    doc:
#      short_help: "An (optional) custom mysql group id"
#    type: integer
#    required: false
#  mysql_user_id:
#    doc:
#      short_help: "An (optional) custom mysql user id"
#    type: integer
#    required: false
  db_import:
    doc:
      short_help: Whether to use a sql dump file to create the database.
    type: boolean
    required: false
    default: false
    cli:
      is_flag: true
  db_dump_file:
    doc:
      short_help: An (optional) database dump file.
    type: string
    required: false
  db_name:
    doc:
      short_help: The name of the database to use from the dump (or 'all').
    type: string
    required: true
  db_user:
    doc:
      short_help: The name of the database user.
    type: string
    required: false
  db_host:
    doc:
      short_help: The 'host' part of the MySQL username.
    type: string
    required: false
  db_user_password:
    doc:
      short_help: The password for the database user.
    type: string
    required: false
    secret: true
  db_user_priv:
    doc:
      short_help: The user privileges.
    type: string
    required: false

meta:
  tags:
  - mysql
  - mariadb
  - database

frecklets:
- mariadb-service:
    mysql_group_id: '{{:: mysql_group_id ::}}'
    mysql_user_id: '{{:: mysql_user_id ::}}'
- task:
    become: true
  frecklet:
    name: mysql_db
    type: ansible-module
    skip: '{{:: db_import ::}}'
    properties:
      idempotent: true
      elevated: true
      internet: false
    desc:
      short: 'create database: {{:: db_name ::}}'
      references:
        Ansible 'mysql_db' module: https://docs.ansible.com/ansible/latest/modules/mysql_db_module.html
  vars:
    name: '{{:: db_name ::}}'
- task:
    become: true
  frecklet:
    name: mysql_db
    type: ansible-module
    skip: '{{:: db_import | negate ::}}'
    properties:
      idempotent: true
      elevated: true
      internet: false
    desc:
      short: importing mysql dump
      references:
        Ansible 'mysql_db' module: https://docs.ansible.com/ansible/latest/modules/mysql_db_module.html
  vars:
    state: import
    name: '{{:: db_name ::}}'
    target: '{{:: db_dump_file ::}}'
- task:
    become: true
  frecklet:
    name: mysql_user
    type: ansible-module
    skip: '{{:: db_user | true_if_empty ::}}'
    properties:
      idempotent: true
      elevated: true
      internet: false
    desc:
      short: 'set up mysql db user {{:: db_user ::}}'
      references:
        Ansible 'mysql_user' module: https://docs.ansible.com/ansible/latest/modules/mysql_user_module.html
      long: |
        Setting up mariadb/mysql user '{{:: db_user ::}}', using the provided password, and granting access to database '{{:: db_name ::}}' with permission string '{{:: db_user_priv | default(db_name + '.*:ALL') ::}}'.
  vars:
    name: '{{:: db_user ::}}'
    host: '{{:: db_host ::}}'
    password: '{{:: db_user_password ::}}'
    priv: "{{:: db_user_priv | default(db_name + '.*:ALL') ::}}"
    state: present
frecklecute mariadb-database-exists --help

Usage: frecklecute mariadb-database-exists [OPTIONS]

  Installs MariaDB service on a host, and ensures a database with the
  provided name is present.

  If a database dump file is provided, the database will be imported from
  it. Otherwise an empty table will be created.

  If a database dump file is provided, the database name can also be 'all',
  in which case all databases contained in it will be imported.

  If a username is supplied, but no ``db_user_priv``, the user
  'db_name.*:ALL' privileges granted.

Options:
  --db-name DB_NAME               The name of the database to use from the
                                  dump (or 'all').  [required]
  --db-dump-file DB_DUMP_FILE     An (optional) database dump file.
  --db-host DB_HOST               The 'host' part of the MySQL username.
  --db-import / --no-db-import    Whether to use a sql dump file to create the
                                  database.
  --db-user DB_USER               The name of the database user.
  --db-user-password DB_USER_PASSWORD
                                  The password for the database user.
  --db-user-priv DB_USER_PRIV     The user privileges.
  --mysql-group-id MYSQL_GROUP_ID
                                  An (optional) custom mysql group id
  --mysql-user-id MYSQL_USER_ID   An (optional) custom mysql user id
  --help                          Show this message and exit.
# -*- coding: utf-8 -*-


#
# module path: pycklets.mariadb_database_exists.MariadbDatabaseExists
#


from dataclasses import dataclass
from pyckles import AutoPycklet
from typing import *    # noqa

@dataclass
class MariadbDatabaseExists(AutoPycklet):
    """Installs MariaDB service on a host, and ensures a database with the provided name is present.

     If a database dump file is provided, the database will be imported from it.
     Otherwise an empty table will be created.

     If a database dump file is provided, the database name can also be 'all',
     in which case all databases contained in it will be imported.

     If a username is supplied, but no ``db_user_priv``, the user 'db_name.*:ALL' privileges granted.

       Args:
         db_dump_file: An (optional) database dump file.
         db_host: The 'host' part of the MySQL username.
         db_import: Whether to use a sql dump file to create the database.
         db_name: The name of the database to use from the dump (or 'all').
         db_user: The name of the database user.
         db_user_password: The password for the database user.
         db_user_priv: The user privileges.
         mysql_group_id: An (optional) custom mysql group id
         mysql_user_id: An (optional) custom mysql user id

    """

    FRECKLET_ID = "mariadb-database-exists"

    db_dump_file: str = None
    db_host: str = None
    db_import: bool = None
    db_name: str = None
    db_user: str = None
    db_user_password: str = None
    db_user_priv: str = None
    mysql_group_id: int = None
    mysql_user_id: int = None


    def __post_init__(self):
        super(MariadbDatabaseExists, self).__init__(var_names=["db_dump_file", "db_host", "db_import", "db_name", "db_user", "db_user_password", "db_user_priv", "mysql_group_id", "mysql_user_id"])


frecklet_class = MariadbDatabaseExists
# -*- coding: utf-8 -*-


#
# module path: pycklets.mariadb_database_exists.MariadbDatabaseExists
#


from pyckles import AutoPycklet

class MariadbDatabaseExists(AutoPycklet):
    """Installs MariaDB service on a host, and ensures a database with the provided name is present.

     If a database dump file is provided, the database will be imported from it.
     Otherwise an empty table will be created.

     If a database dump file is provided, the database name can also be 'all',
     in which case all databases contained in it will be imported.

     If a username is supplied, but no ``db_user_priv``, the user 'db_name.*:ALL' privileges granted.

       Args:
         db_dump_file: An (optional) database dump file.
         db_host: The 'host' part of the MySQL username.
         db_import: Whether to use a sql dump file to create the database.
         db_name: The name of the database to use from the dump (or 'all').
         db_user: The name of the database user.
         db_user_password: The password for the database user.
         db_user_priv: The user privileges.
         mysql_group_id: An (optional) custom mysql group id
         mysql_user_id: An (optional) custom mysql user id

    """

    FRECKLET_ID = "mariadb-database-exists"

    def __init__(self, db_dump_file=None, db_host=None, db_import=None, db_name=None, db_user=None, db_user_password=None, db_user_priv=None, mysql_group_id=None, mysql_user_id=None):

        super(MariadbDatabaseExists, self).__init__(var_names=["db_dump_file", "db_host", "db_import", "db_name", "db_user", "db_user_password", "db_user_priv", "mysql_group_id", "mysql_user_id"])
        self._db_dump_file = db_dump_file
        self._db_host = db_host
        self._db_import = db_import
        self._db_name = db_name
        self._db_user = db_user
        self._db_user_password = db_user_password
        self._db_user_priv = db_user_priv
        self._mysql_group_id = mysql_group_id
        self._mysql_user_id = mysql_user_id

    @property
    def db_dump_file(self):
        return self._db_dump_file

    @db_dump_file.setter
    def db_dump_file(self, db_dump_file):
        self._db_dump_file = db_dump_file

    @property
    def db_host(self):
        return self._db_host

    @db_host.setter
    def db_host(self, db_host):
        self._db_host = db_host

    @property
    def db_import(self):
        return self._db_import

    @db_import.setter
    def db_import(self, db_import):
        self._db_import = db_import

    @property
    def db_name(self):
        return self._db_name

    @db_name.setter
    def db_name(self, db_name):
        self._db_name = db_name

    @property
    def db_user(self):
        return self._db_user

    @db_user.setter
    def db_user(self, db_user):
        self._db_user = db_user

    @property
    def db_user_password(self):
        return self._db_user_password

    @db_user_password.setter
    def db_user_password(self, db_user_password):
        self._db_user_password = db_user_password

    @property
    def db_user_priv(self):
        return self._db_user_priv

    @db_user_priv.setter
    def db_user_priv(self, db_user_priv):
        self._db_user_priv = db_user_priv

    @property
    def mysql_group_id(self):
        return self._mysql_group_id

    @mysql_group_id.setter
    def mysql_group_id(self, mysql_group_id):
        self._mysql_group_id = mysql_group_id

    @property
    def mysql_user_id(self):
        return self._mysql_user_id

    @mysql_user_id.setter
    def mysql_user_id(self, mysql_user_id):
        self._mysql_user_id = mysql_user_id



frecklet_class = MariadbDatabaseExists