From 70e66c97be41f09b0be4b35fd4a86a09ea7df11d Mon Sep 17 00:00:00 2001 From: Alkivi Date: Wed, 24 Apr 2019 11:03:27 +0200 Subject: [PATCH 1/3] feat(macro): to use in several state --- sudoers/macros.sls | 22 ++++++++++++++++++++++ sudoers/templates/sudoers.jinja | 7 +++++++ 2 files changed, 29 insertions(+) create mode 100644 sudoers/macros.sls create mode 100644 sudoers/templates/sudoers.jinja diff --git a/sudoers/macros.sls b/sudoers/macros.sls new file mode 100644 index 0000000..4bbef20 --- /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 }} + +{% 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..455b4d8 --- /dev/null +++ b/sudoers/templates/sudoers.jinja @@ -0,0 +1,7 @@ +{%- if config.command is string -%} +{{ for_user }} {{ config.privileges }}: {{ config.command }} +{%- else -%} +{% for command in config.command -%} +{{ for_user }} {{ config.privileges }}: {{ command }} +{% endfor %} +{%- endif -%} From a1b88a98454e4c095c388e05e5ff1f6d05adf4ec Mon Sep 17 00:00:00 2001 From: Anthony Martinet Date: Wed, 24 Apr 2019 17:23:04 +0200 Subject: [PATCH 2/3] fix(macros) --- sudoers/templates/sudoers.jinja | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sudoers/templates/sudoers.jinja b/sudoers/templates/sudoers.jinja index 455b4d8..2c37f93 100644 --- a/sudoers/templates/sudoers.jinja +++ b/sudoers/templates/sudoers.jinja @@ -1,7 +1,7 @@ -{%- if config.command is string -%} -{{ for_user }} {{ config.privileges }}: {{ config.command }} -{%- else -%} +{%- 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 -%} From d8a0e12abf4c23692cc1c2d3f4b4659f79740877 Mon Sep 17 00:00:00 2001 From: Anthony Martinet Date: Wed, 24 Apr 2019 17:29:58 +0200 Subject: [PATCH 3/3] fix(macros) --- sudoers/macros.sls | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sudoers/macros.sls b/sudoers/macros.sls index 4bbef20..f716b40 100644 --- a/sudoers/macros.sls +++ b/sudoers/macros.sls @@ -12,7 +12,7 @@ - context: for_user: {{ for_user }} sudo: {{ sudo }} - config: {{ config }} + config: {{ config|tojson }} {% elif state == 'absent' %} /etc/sudoers.d/{{ filename }}: