From 0dc28d00f24fbe0c1315707700b7a0fa7ecc259d Mon Sep 17 00:00:00 2001 From: Sylvain as root Date: Thu, 19 May 2016 17:11:22 +0200 Subject: [PATCH] merge func with remove-user.sls #119 --- mysql/user.sls | 74 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 65 insertions(+), 9 deletions(-) diff --git a/mysql/user.sls b/mysql/user.sls index 1f21a705..10cd987b 100644 --- a/mysql/user.sls +++ b/mysql/user.sls @@ -1,4 +1,10 @@ -# vim: set ft=yaml: +{#- +# vim: set ft=jinja: +This state handle creation and deletion of mysql's user. +-#} +{#- +===== FETCH DATA ===== +-#} {% from "mysql/defaults.yaml" import rawmap with context %} {%- set mysql = salt['grains.filter_by'](rawmap, grain='os', merge=salt['pillar.get']('mysql:server:lookup')) %} {%- set mysql_root_user = salt['pillar.get']('mysql:server:root_user', 'root') %} @@ -9,19 +15,45 @@ {% set user_states = [] %} {% set user_hosts = [] %} - +{#- +===== MACRO DEFINITION ===== +-#} +{# this macro is the salt statement to remove a user #} +{% macro mysql_user_remove(name, host, where) %} +{% set state_id = 'mysql_user_remove_' ~ name ~ '_' ~ host %} +{{ state_id }}: + # {{ where }} + mysql_user.absent: + - name: {{ name }} + - host: '{{ host }}' + - connection_host: '{{ mysql_host }}' + - connection_user: '{{ mysql_salt_user }}' + {% if mysql_salt_pass %} + - connection_pass: '{{ mysql_salt_pass }}' + {% endif %} + - connection_charset: utf8 +{% endmacro %} +{#- +===== MAIN OUTPUT===== +-#} include: - mysql.python - +{#- +===== LOOP OVER DATA : users ===== +-#} {% for name, user in salt['pillar.get']('mysql:user', {}).items() %} - +{#- + >> select multiples host for the same user host: or hosts: in pillar ===== +-#} {% set user_host = salt['pillar.get']('mysql:user:%s:host'|format(name)) %} {% if user_host != '' %} {% set user_hosts = [user_host] %} {% else %} {% set user_hosts = salt['pillar.get']('mysql:user:%s:hosts'|format(name)) %} {% endif %} - +{#- + >> fetch something about mine, used to overwrite user_hosts at the end +-#} {% if not user_hosts %} {% set mine_target = salt['pillar.get']('mysql:user:%s:mine_hosts:target'|format(name)) %} {% set mine_function = salt['pillar.get']('mysql:user:%s:mine_hosts:function'|format(name)) %} @@ -31,10 +63,16 @@ include: {% set user_hosts = salt['mine.get'](mine_target, mine_function, mine_expression_form).values() %} {% endif %} {% endif %} - +{#- + ===== INNER LOOP OVER DATA : host -> fecthed above single or multiple ===== +-#} {% for host in user_hosts %} -{# Don't create absent user, see remove-user.sls #} -{% if user.absent is not defined or not user.absent %} +{% if user.absent is defined and user.absent %} +{{ mysql_user_remove(name, host, 'top') }} +{% else %} +{#- + CREATE USER +-#} {% set state_id = 'mysql_user_' ~ name ~ '_' ~ host%} {{ state_id }}: mysql_user.present: @@ -93,9 +131,27 @@ include: - mysql_user: {{ state_id }} {% endfor %} {% endif %} -{# endif user.absent #} + +{# END user.absent #} {% endif %} {% do user_states.append(state_id) %} +{#- + =============== END FOR host +-#} {% endfor %} + +{#- +extra remove user with multiples host see #119 for user.hosts_absent (list) +must be in user loop not in host loop. +-#} +{% set user_hosts_absent = salt['pillar.get']('mysql:user:%s:hosts_absent'|format(name)) %} +{% if user_hosts_absent != '' %} + {% for h in user_hosts_absent %} + {{ mysql_user_remove(name, h, 'end') }} + {% endfor %} +{% endif %} +{#- + =============== END FOR user +-#} {% endfor %}