Skip to content

Instantly share code, notes, and snippets.

@bujordan
Last active February 25, 2016 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bujordan/ed400a7a82bb74c41727 to your computer and use it in GitHub Desktop.
Save bujordan/ed400a7a82bb74c41727 to your computer and use it in GitHub Desktop.
mysql salt setup (loosely based on the mysql-formula)
{% set mysql_root_user = salt['pillar.get']('mysql:root_user', 'root') %}
{% set mysql_root_pass = salt['pillar.get']('mysql:root_password', salt['grains.get']('server_id')) %}
{% set mysql_host = salt['pillar.get']('mysql:host', 'localhost') %}
{% set db_states = [] %}
{% set mysql_salt_user = salt['pillar.get']('mysql:salt_user', mysql_root_user) %}
{% set mysql_salt_pass = salt['pillar.get']('mysql:salt_user_password', mysql_root_pass) %}
{% for database in salt['pillar.get']('mysql:database', []) %}
{% set state_id = 'mysql_db_' ~ loop.index0 %}
{{ state_id }}:
mysql_database.present:
- name: {{ database }}
- connection_host: '{{ mysql_host }}'
- connection_user: '{{ mysql_salt_user }}'
{% if mysql_salt_pass %}
- connection_pass: '{{ mysql_salt_pass }}'
{% endif %}
- connection_charset: utf8
{% if salt['pillar.get'](['mysql', 'schema', database, 'load']|join(':'), False) %}
{{ state_id }}_schema:
file.managed:
- name: /etc/mysql/{{ database }}.schema
- source: {{ salt['pillar.get'](['mysql', 'schema', database, 'source']|join(':')) }}
{%- set template_type = salt['pillar.get'](['mysql', 'schema', database, 'template']|join(':'), False) %}
{%- if template_type %}
- template: {{ template_type }}
{% endif %}
- user: {{ salt['pillar.get']('mysql:user', 'mysql') }}
- makedirs: True
{{ state_id }}_load:
cmd.wait:
- name: mysql -u {{ mysql_salt_user }} -p{{ mysql_salt_pass }} {{ database }} < /etc/mysql/{{ database }}.schema
- watch:
- file: {{ state_id }}_schema
- mysql_database: {{ state_id }}
{% endif %}
{% do db_states.append(state_id) %}
{% endfor %}
➜ salt git:(master) ✗ sudo salt minion1 state.apply mysql test=True
minion1:
The minion function caused an exception: Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/minion.py", line 1161, in _thread_return
return_data = func(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/modules/state.py", line 296, in apply_
return sls(mods, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/modules/state.py", line 681, in sls
ret = st_.state.call_high(high_)
File "/usr/lib/python2.7/dist-packages/salt/state.py", line 2067, in call_high
ret = dict(list(disabled.items()) + list(self.call_chunks(chunks).items()))
File "/usr/lib/python2.7/dist-packages/salt/state.py", line 1623, in call_chunks
running = self.call_chunk(low, running, chunks)
File "/usr/lib/python2.7/dist-packages/salt/state.py", line 1769, in call_chunk
self._mod_init(low)
File "/usr/lib/python2.7/dist-packages/salt/state.py", line 612, in _mod_init
self.states['{0}.{1}'.format(low['state'], low['fun'])] # pylint: disable=W0106
File "/usr/lib/python2.7/dist-packages/salt/utils/lazy.py", line 90, in __getitem__
raise KeyError(key)
KeyError: 'mysql_database.present'
# MySQL State file
{% from 'mysql/database.sls' import db_states with context %}
{% macro requisites(type, states) %}
{%- for state in states %}
- {{ type }}: {{ state }}
{%- endfor -%}
{% endmacro %}
debconf-utils:
pkg.installed
mysql_setup:
debconf.set:
- name: mysql-server
- data:
'mysql-server/root_password': {'type': 'password', 'value': '{{ salt['pillar.get']('mysql:root_password', '') }}' }
'mysql-server/root_password_again': {'type': 'password', 'value': '{{ salt['pillar.get']('mysql:root_password', '') }}' }
- require:
- pkg: debconf-utils
mysql_python:
pkg.installed:
- name: python-mysqldb
# not needed on Ubuntu?
#mysql_python:
# pkg.installed
mysql:
pkg.installed:
- name: mysql-server
- require:
- debconf: mysql-server
- pkg: mysql_python
#- pkg: mysql_python
service:
- running
- watch:
- pkg: mysql-server
- file: /etc/mysql/my.cnf
/etc/mysql/my.cnf:
file.managed:
- source: salt://mysql/my.cnf.jinja
- template: jinja
- user: root
- group: root
- mode: 640
- require:
- pkg: mysql-server
/etc/salt/minion.d/mysql.conf:
file.managed:
- source: salt://mysql/minion.d/mysql.conf
- user: root
- group: root
- mode: 640
- require:
- service: mysql
/etc/mysql/salt.cnf:
file.managed:
- source: salt://mysql/salt.cnf.jinja
- template: jinja
- user: root
- group: root
- mode: 640
- require:
- service: mysql
restart_minion_for_mysql:
service.running:
- name: salt-minion
- watch:
- file: /etc/salt/minion.d/mysql.conf
mysql_user_salt_localhost:
mysql_user.present:
- name: {{ salt['pillar.get']('mysql:salt_user', '') }}
- host: localhost
- password: {{ salt['pillar.get']('mysql:salt_user_password', '') }}
- connection_user: {{ salt['pillar.get']('mysql:root_user', 'root') }}
- connection_pass: {{ salt['pillar.get']('mysql:root_password', '') }}
- require:
- service: mysql
- pkg: mysql_python
mysql_salt_user_grants:
mysql_grants.present:
- grant: ALL PRIVILEGES
- database: '*.*'
- user: {{ salt['pillar.get']('mysql:salt_user', '') }}
- connection_user: {{ salt['pillar.get']('mysql:root_user', 'root') }}
- connection_pass: {{ salt['pillar.get']('mysql:root_password', '') }}
include:
- mysql.database
{% if (db_states|length()) > 0 %}
extend:
mysql:
service:
- require_in:
{{ requisites('mysql_database', db_states) }}
{% endif %}
mysql.default_file: '/etc/mysql/salt.cnf'
# MySQL Pillar File
mysql:
root_user: root
root_password: somepass
salt_user: salt
salt_user_password: someotherpass
server:
user: mysql
host: localhost
#user:
# frank:
# password: 'frankpass'
# host: localhost
database:
- foo
schema:
foo:
load: True
source: salt://mysql/schemas/foo.schema
[client]
host = localhost
user = root
socket = /var/run/mysqld/mysqld.sock
password = {{ salt['pillar.get']('mysql:root_pw', '') }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment