Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
[All] Handle "ignore" state in exclusive templates
Browse files Browse the repository at this point in the history
  • Loading branch information
nervo committed Sep 29, 2020
1 parent 25bb71e commit 53ba2cd
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 44 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- Configs can be individually ignored

## [2.0.4] - 2020-08-26
### Fixed
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,20 @@ manala_supervisor_configs:
- environment:
FOO: bar
BAR: 12
- file: bar.conf
state: absent
# Ensure config is absent
- file: absent.conf
state: absent # "present" by default
# Ignore config
- file: ignore.conf
state: ignore
```

Raw content

```yaml
manala_supervisor_configs:
- file: bar.conf
content: |
config: |
[program:example]
command=/usr/bin/example --loglevel=%(ENV_LOGLEVEL)s
```
Expand Down
20 changes: 16 additions & 4 deletions lookup_plugins/manala_templates_exclusive.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

from ansible.plugins.lookup import LookupBase
from ansible.errors import AnsibleError
from ansible.module_utils._text import to_text

import os


class LookupModule(LookupBase):

def run(self, terms, variables=None, **kwargs):
Expand All @@ -14,10 +16,13 @@ def run(self, terms, variables=None, **kwargs):

wantstate = kwargs.pop('wantstate', None)

templates = self._flatten(terms[0])
if wantstate and wantstate not in ['present', 'absent']:
raise AnsibleError('Expect a wanstate of "present" or "absent" but was "%s"' % to_text(wantstate))

templates = self._flatten(terms[0])
exclusives = self._flatten(terms[1])
dir = terms[2]
template = terms[3]
dir = terms[2]
template = terms[3]

itemDefault = {
'state': 'present',
Expand All @@ -39,14 +44,21 @@ def run(self, terms, variables=None, **kwargs):

# Must be a dict
if not isinstance(template, dict):
raise AnsibleError('Expect a dict')
raise AnsibleError('Expect a dict but was a %s' % type(template))

# Check file key
if 'file' not in template:
raise AnsibleError('Expect a "file" key')

item = itemDefault.copy()
item.update(template)

if item['state'] not in ['present', 'absent', 'ignore']:
raise AnsibleError('Expect a state of "present", "absent" or "ignore" but was "%s"' % to_text(item['state']))

if item['state'] == 'ignore':
continue

item.update({
'file': os.path.join(dir, item['file'])
})
Expand Down
File renamed without changes.
137 changes: 137 additions & 0 deletions templates/configs/_macros.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
{#- Deprecated -#}
{%- macro config(parameters, exclusions = [], indent = 0) -%}
{%- for parameter in parameters -%}
{%- if parameter is string or parameter is number -%}
{{ parameter }}{{ '\n' }}
{%- elif parameter is mapping -%}
{%- for parameter_key, parameter_value in parameter.items() -%}
{%- if parameter_key not in exclusions -%}
{{ _config_parameter(parameter_key, parameter_value, indent) }}{{ '\n' }}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- endfor -%}
{%- endmacro -%}

{#- Deprecated -#}
{%- macro config_row(parameters, key, default, indent = 0, placeholder = false) -%}
{%- set vars = {'values': []} -%}
{%- for parameter in parameters -%}
{%- if parameter is mapping -%}
{%- for parameter_key, parameter_value in parameter.items() -%}
{%- if parameter_key == key -%}
{%- if vars.update({'values': vars['values'] + [parameter_value]}) -%}{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- endfor -%}
{%- if vars['values']|length -%}
{%- for value in vars['values'] -%}
{{ _config_parameter(key, value, indent) }}{% if not loop.last %}{{ '\n' }}{% endif %}
{%- endfor -%}
{%- else -%}
{%- if placeholder -%}
{{ default }}
{%- else -%}
{%- for value in (default if (default is sequence and default is not string) else [default]) -%}
{{ _config_parameter(key, value, indent) }}{% if not loop.last %}{{ '\n' }}{% endif %}
{%- endfor -%}
{%- endif -%}
{%- endif -%}
{%- endmacro -%}

{#- Deprecated -#}
{%- macro config_row_sequence(parameters, key, default, indent = 0, placeholder = false) -%}
{%- set vars = {'values': []} -%}
{%- for parameter in parameters -%}
{%- if parameter is mapping -%}
{%- for parameter_key, parameter_value in parameter.items() -%}
{%- if parameter_key == key -%}
{%- if vars.update({'values': vars['values'] + [parameter_value]}) -%}{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- endfor -%}
{%- if vars['values']|length -%}
{%- for value in vars['values'] -%}
{{ _config_parameter_sequence(key, value, indent) }}{% if not loop.last %}{{ '\n' }}{% endif %}
{%- endfor -%}
{%- else -%}
{%- if placeholder -%}
{{ default }}
{%- else -%}
{%- for value in (default if (default is sequence and default is not string) else [default]) -%}
{{ _config_parameter_sequence(key, value, indent) }}{% if not loop.last %}{{ '\n' }}{% endif %}
{%- endfor -%}
{%- endif -%}
{%- endif -%}
{%- endmacro -%}

{#- Deprecated -#}
{%- macro _config_parameter(key, value, indent = 0) -%}
{%- if value is sequence and value is not string -%}
[{{ key }}]{{ '\n' }}{{ config(value, []) }}
{%- else -%}
{{ _config_parameter_key(key)|indent(indent, true) }}={{ _config_parameter_value(value) }}
{%- endif -%}
{%- endmacro -%}

{#- Deprecated -#}
{%- macro _config_parameter_sequence(key, values, indent = 0) -%}
{{ _config_parameter_key(key)|indent(indent, true) }}=
{%- for key, value in values.items() -%}
{{ key }}="{{ value }}"{% if not loop.last %},{% endif %}
{%- endfor -%}
{%- endmacro -%}

{#- Deprecated -#}
{%- macro _config_parameter_key(key) -%}
{{ key }}
{%- endmacro -%}

{#- Deprecated -#}
{%- macro _config_parameter_value(value) -%}
{%- if value is sameas none -%}
{%- elif value is sameas true -%}
true
{%- elif value is sameas false -%}
false
{%- elif value is string -%}
{{ value }}
{%- elif value is number -%}
{{ value }}
{%- endif -%}
{%- endmacro -%}

{%- macro config_sections(sections, excludes = []) -%}
{%- for section, parameters in sections|dictsort -%}
{%- if section not in excludes -%}
[{{ section }}]
{{- config_parameters(parameters) }}{{ '\n\n' }}
{%- endif -%}
{%- endfor -%}
{%- endmacro -%}

{%- macro config_parameters(parameters, excludes = []) -%}
{%- for key, value in parameters|dictsort -%}
{%- if key not in excludes -%}
{{ '\n' }}{{ config_parameter(key, value) }}
{%- endif -%}
{%- endfor -%}
{%- endmacro -%}

{%- macro config_parameter(key, value, whitespace= False) -%}
{{ key }}{{ whitespace|ternary(' = ', '=') }}{% if value is sameas true -%}
true
{%- elif value is sameas false -%}
false
{%- elif value is mapping -%}
{%- for k, v in value|dictsort -%}
{{ k }}="{{ v }}"{% if not loop.last %},{% endif %}
{%- endfor -%}
{%- elif value is iterable and value is not string -%}
{{ value | join(',') }}
{%- else -%}
{{ value }}
{%- endif -%}
{%- endmacro -%}
52 changes: 36 additions & 16 deletions tests/0300_configs.goss.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
# Default
{{ if has "default" .Vars.tags }}
file:
tmp/configs/default:
exists: true
filetype: directory
owner: root
group: root
mode: "0755"
tmp/configs/default/foo.conf:
exists: true
filetype: file
Expand Down Expand Up @@ -37,38 +43,44 @@ file:
- "command=/bin/bar"
- "[group:foo]"
- "programs=foo,bar"
tmp/configs/default/foo_template_inet_http_server.conf:
tmp/configs/default/foo_content.conf:
exists: true
filetype: file
owner: root
group: root
mode: "0644"
contains:
- "[inet_http_server]"
- "port=0.0.0.1:9001"
- "username=foo"
tmp/configs/default/foo_template_deprecated.conf:
- "[program:foo]"
- "command=/bin/foo"
- "priority=123"
- "autostart=true"
- "stopsignal=HUP"
- 'environment=BAR="123",FOO="bar"'
- "[program:bar]"
- "command=/bin/bar"
- "[group:foo]"
- "programs=foo,bar"
tmp/configs/default/foo_template.conf:
exists: true
filetype: file
owner: root
group: root
mode: "0644"
contains:
- "[program:foo]"
- "command=cat"
- "priority=123"
- "autostart=true"
- "stopsignal=HUP"
- 'environment=FOO="bar",BAR="123"'
tmp/configs/default/foo_content.conf:
- "Config foo"
tmp/configs/default/foo_template_deprecated.conf:
exists: true
filetype: file
owner: root
group: root
mode: "0644"
contains:
- "[program:foo]"
- "command=/bin/foo"
- "command=cat"
- "priority=123"
- "autostart=true"
- "stopsignal=HUP"
- 'environment=FOO="bar",BAR="123"'
tmp/configs/default/bar.conf:
exists: true
filetype: file
Expand Down Expand Up @@ -99,6 +111,11 @@ file:
group: root
mode: "0644"
size: 1
tmp/configs/state/qux.conf:
exists: true
filetype: file
tmp/configs/state/quux.conf:
exists: false
{{ end }}

# Defaults
Expand All @@ -111,16 +128,15 @@ file:
group: root
mode: "0644"
contains:
- "[inet_http_server]"
- "port=0.0.0.0:9001"
- "Config foo"
tmp/configs/defaults/bar.conf:
exists: true
filetype: file
owner: root
group: root
mode: "0644"
contains:
- "Bar"
- "Config bar"
{{ end }}

# Exclusive
Expand All @@ -137,4 +153,8 @@ file:
size: 1
tmp/configs/exclusive/baz.conf:
exists: false
tmp/configs/exclusive/qux.conf:
exists: false
tmp/configs/exclusive/quux.conf:
exists: false
{{ end }}
Loading

0 comments on commit 53ba2cd

Please sign in to comment.