0

Is there a LAMP-MySQL (or equivalent) agnostic way to create a DB stack that includes:

  1. DB-user and a relative password
  2. DB with the same name as of the user
  3. Setting a host for the DB (say, localhost)
  4. Give the user all privileges

The reason I need such a way or approach is to have easier life when working with different LAMP-MySQL (or equivalent) DB programs as part of LAMP.

For example, not all LAMP stacks has particularly MySQL or MariaDB and the SQL syntax or DB-CLUI (Bash) extension syntax might be a tiny bit different for each DB SQL variant, hence I seek standardization in a LAMP-RDBMS agnostic fashion.

1
+50

I am not sure whether anything already exists (i.e. in Galaxy), but Ansible certainly provides all the building blocks to do this. A reason this may well not have been done, or at least released publically, is that there is no one way to configure a database, much less an agreed method that any DB technology should be generally configured. So I would guess you will need to roll this yourself.

As for how to go about doing that, an approach I would likely investigate:

  • Write a general DB role, that expects a common series of parameters to be passed to it (i.e. DB technology, default users, default DB's)
  • Write a role for each DB technology type, that can be passed only the common parameters. This role would then run the DB technology specific tasks to provision your instance
  • Add role dependencies to your general DB role, conditional on the DB technology type

A pseudo example:

playbook.yml

---

# Apply the 'db' role to DB hosts
- hosts: db_hosts
  roles:
    - db

roles/db/defaults/main.yml

---

# Provide a default tech so one does not have to be passed at run time
db_technology: postgresql

roles/db/meta/main.yml

---

# Include the specific DB tech role, based on the value of 'db_technology'
dependencies:
  - { role: postgresql, db_params: "{{ db_params }}", when: db_technology == 'postgresql' }
  - { role: mysql, db_params: "{{ db_params }}",  when: db_technology == 'mysql' }

roles/db/tasks/main.yml

---

# Do tasks that are common to all DB types here

roles/postgresql/tasks/main.yml

---

# Do postgresql specific tasks here

roles/mysql/tasks/main.yml

---

# Do mysql specific tasks here

Finally, this could then be run with:

ansible-playbook -e "db_technology=mysql, db_params={'users': {'someuser': 'somepassword'}, 'dbs': ['some_db_name', 'another_db_name']" playbook.yml

Please do be aware, this is a very general, very incomplete example to give you an idea of the structure you could use to solve this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.