From e4ab3350773b79f84b6c36042d2015bd21bd9681 Mon Sep 17 00:00:00 2001 From: Daniel Dehennin Date: Thu, 30 Jul 2020 22:05:24 +0200 Subject: [PATCH] feat(map): generate a YAML file to validate `map.jinja` We provide a new `_mapdata` state which generate a `/tmp/salt_mapdata_dump.yaml` to be validated by `Inspec`. --- docs/README.rst | 7 +++++++ openssh/_mapdata/_mapdata.jinja | 13 +++++++++++++ openssh/_mapdata/init.sls | 25 +++++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 openssh/_mapdata/_mapdata.jinja create mode 100644 openssh/_mapdata/init.sls diff --git a/docs/README.rst b/docs/README.rst index be69b8a..d2f45a4 100644 --- a/docs/README.rst +++ b/docs/README.rst @@ -230,6 +230,13 @@ To completely disable adding IP addresses:: Manages the system wide ``/etc/ssh/moduli`` file. +``openssh._mapdata`` +^^^^^^^^^^^^^^^^^^ + +Testing state which dumps the ``map.jinja`` values in ``/tmp/salt_mapdata_dump.yaml``. +This state is not called by any include but is mostly used by kitchen and Inspec infrastructure to validate ``map.jinja``. + + Testing ------- diff --git a/openssh/_mapdata/_mapdata.jinja b/openssh/_mapdata/_mapdata.jinja new file mode 100644 index 0000000..ad54eaa --- /dev/null +++ b/openssh/_mapdata/_mapdata.jinja @@ -0,0 +1,13 @@ +# yamllint disable rule:indentation rule:line-length +# {{ grains.get('osfinger', grains.os) }} +--- +{#- use salt.slsutil.serialize to avoid encoding errors on some platforms #} +{{ salt['slsutil.serialize']( + 'yaml', + map, + default_flow_style=False, + allow_unicode=True, + ) + | regex_replace("^\s+'$", "'", multiline=True) + | trim +}} diff --git a/openssh/_mapdata/init.sls b/openssh/_mapdata/init.sls new file mode 100644 index 0000000..b9501a1 --- /dev/null +++ b/openssh/_mapdata/init.sls @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# vim: ft=sls +--- +{#- Get the `tplroot` from `tpldir` #} +{%- set tplroot = tpldir.split('/')[0] %} +{%- from tplroot ~ "/map.jinja" import openssh with context %} +{%- from tplroot ~ "/map.jinja" import ssh_config with context %} +{%- from tplroot ~ "/map.jinja" import sshd_config with context %} + +{%- set output_file = '/tmp/salt_mapdata_dump.yaml' %} +{%- set map = { + 'openssh': openssh, + 'ssh_config': ssh_config, + 'sshd_config': sshd_config, + } %} + +{%- do salt['log.debug']( map | yaml(False) ) %} + +{{ tplroot }}-mapdata-dump: + file.managed: + - name: {{ output_file }} + - source: salt://{{ tplroot }}/_mapdata/_mapdata.jinja + - template: jinja + - context: + map: {{ map | yaml }}