From 38eb852bce2d0a0eb5199e6a420f922cb0563604 Mon Sep 17 00:00:00 2001 From: puneet kandhari Date: Mon, 6 Apr 2015 09:25:27 -0500 Subject: [PATCH] Make the formula fully pillar driven/customizable - split package and param into separate state files - use defaults.yml to specify all configurable options of the formula - setup map.jinja to read in defaults.yml, filter by os_family for Arch, and merge in sysctl:lookup pillar - Updated README --- README.rst | 15 ++++++++------- pillar.example | 18 ++++++++++-------- sysctl/defaults.yml | 14 ++++++++++++++ sysctl/init.sls | 27 +++++---------------------- sysctl/map.jinja | 31 +++++++++++++++++++++++++------ sysctl/package.sls | 10 ++++++++++ sysctl/param.sls | 17 +++++++++++++++++ 7 files changed, 89 insertions(+), 43 deletions(-) create mode 100644 sysctl/defaults.yml create mode 100644 sysctl/package.sls create mode 100644 sysctl/param.sls diff --git a/README.rst b/README.rst index 16344a8..4724e18 100644 --- a/README.rst +++ b/README.rst @@ -10,13 +10,14 @@ Here's a small pillar data sample:: sysctl: lookup: pkg: procps-ng - config_location: /etc/sysctl.d - params: - vm.swappines: - value: 20 - config: 10-swap.conf - fs.file-max: - value: 10000 + config: + location: /etc/sysctl.d + params: + vm.swappines: + value: 20 + config: 10-swap.conf + fs.file-max: + value: 10000 The state sysclt will ensure these are present on the system diff --git a/pillar.example b/pillar.example index c632b30..21b0061 100644 --- a/pillar.example +++ b/pillar.example @@ -1,11 +1,13 @@ sysctl: lookup: pkg: procps-ng - params: - fs.file-max: - value: 100000 - config: fs.conf - vm.swappiness: - value: 20 - - + config: + location: '/etc/sysctl.d' + params: + - + name: fs.file-max + value: 100000 + config: fs.conf + - + name: vm.swappiness + value: 20 diff --git a/sysctl/defaults.yml b/sysctl/defaults.yml new file mode 100644 index 0000000..670d4c3 --- /dev/null +++ b/sysctl/defaults.yml @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- +# vim: ft=yaml +sysctl: + pkg: procps-ng + config: + location: '/etc/sysctl.d' + params: + - + name: fs.file-max + value: 100000 + config: fs.conf + - + name: vm.swappiness + value: 20 diff --git a/sysctl/init.sls b/sysctl/init.sls index 33cb305..c09db24 100644 --- a/sysctl/init.sls +++ b/sysctl/init.sls @@ -1,23 +1,6 @@ -{% from "sysctl/map.jinja" import sysctl with context %} - -sysctl: - pkg.installed: - - name: {{ sysctl.pkg|json }} - -{%- set config = pillar.get("sysctl", {} )%} -{%- for name, item in config.get('params', {}).items() %} -{%- if item == None -%} -{% set item = {} -%} -{%- endif -%} -{%- set value = item.get('value', {}) %} -{%- set config = item.get('config') %} - -{{ name }}: - sysctl.present: - - name: {{ name }} - - value: {{ value }} - {%- if 'config' in item %} - - config: {{ sysctl.config_location}}/{{ config }} - {%- endif %} -{%- endfor -%} +# -*- coding: utf-8 -*- +# vim: ft=sls +include: + - sysctl.package + - sysctl.param diff --git a/sysctl/map.jinja b/sysctl/map.jinja index b02095f..da3ef81 100644 --- a/sysctl/map.jinja +++ b/sysctl/map.jinja @@ -1,6 +1,25 @@ -{% set sysctl = salt['grains.filter_by']({ - 'Arch': { - 'pkg' : 'procps-ng', - 'config_location' : '/etc/sysctl.d', - }, -}, merge=salt['pillar.get']('sysctl:lookup')) %} +# -*- coding: utf-8 -*- +# vim: ft=jinja + +{## Start with defaults from defaults.sls ##} +{% import_yaml 'sysctl/defaults.yml' as default_settings %} + +{## setup variable using grains['os_family'] based logic ##} +{% set os_family_map = salt['grains.filter_by']({ + 'Arch': { + "config": { + "location": '/etc/sysctl.d', + } + } + }, grain="os_family") +%} +{## Merge the flavor_map to the default settings ##} +{% do default_settings.sysctl.update(os_family_map) %} + +{## Merge in sysctl:lookup pillar ##} +{% set sysctl_settings = salt['pillar.get']( + 'sysctl:lookup', + default=default_settings.sysctl, + merge=True + ) +%} diff --git a/sysctl/package.sls b/sysctl/package.sls new file mode 100644 index 0000000..9e0abfb --- /dev/null +++ b/sysctl/package.sls @@ -0,0 +1,10 @@ +# -*- coding: utf-8 -*- +# vim: ft=sls + +{## import settings from map.jinja ##} +{% from "sysctl/map.jinja" import sysctl_settings with context %} + +sysctl-pkg: + pkg.installed: + - name: {{ sysctl_settings.pkg }} + - failhard: True diff --git a/sysctl/param.sls b/sysctl/param.sls new file mode 100644 index 0000000..15977a8 --- /dev/null +++ b/sysctl/param.sls @@ -0,0 +1,17 @@ +# -*- coding: utf-8 -*- +# vim: ft=sls + +{## import settings from map.jinja ##} +{% from "sysctl/map.jinja" import sysctl_settings with context %} + +{% for param in sysctl_settings.get('params', {}) %} + {% if param is mapping %} +sysctl-present-{{ param.name }}: + sysctl.present: + - name: {{ param.name }} + - value: {{ param.version }} + {% if param.config is defined %} + - config: {{ sysctl_settings.config.location }}/{{ param.config }} + {% endif %} + {% endif %} +{% endfor %}