diff --git a/sudoers/macros.sls b/sudoers/macros.sls new file mode 100644 index 0000000..f716b40 --- /dev/null +++ b/sudoers/macros.sls @@ -0,0 +1,22 @@ +{%- macro sudoers(for_user, sudo, config, priority='20', state='present') -%} +{%- set filename = priority ~ '-' ~ for_user|replace('.','-') ~ '-' ~ sudo %} + +{% if state == 'present' %} +/etc/sudoers.d/{{ filename }}: + file.managed: + - user: root + - group: root + - mode: 440 + - template: jinja + - source: salt://sudoers/templates/sudoers.jinja + - context: + for_user: {{ for_user }} + sudo: {{ sudo }} + config: {{ config|tojson }} + +{% elif state == 'absent' %} +/etc/sudoers.d/{{ filename }}: + file.absent +{% endif %} + +{%- endmacro %} diff --git a/sudoers/templates/sudoers.jinja b/sudoers/templates/sudoers.jinja new file mode 100644 index 0000000..2c37f93 --- /dev/null +++ b/sudoers/templates/sudoers.jinja @@ -0,0 +1,7 @@ +{%- if config.command is iterable and config.command is not string -%} +{% for command in config.command -%} +{{ for_user }} {{ config.privileges }}: {{ command }} +{% endfor %} +{%- else -%} +{{ for_user }} {{ config.privileges }}: {{ config.command }} +{%- endif -%}