Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't create an expressión filter with more than one 'else if' #164

Closed
amateo opened this issue Oct 13, 2020 · 8 comments · Fixed by #165
Closed

Can't create an expressión filter with more than one 'else if' #164

amateo opened this issue Oct 13, 2020 · 8 comments · Fixed by #165
Labels
bug Something isn't working

Comments

@amateo
Copy link
Contributor

amateo commented Oct 13, 2020

Hi,

I need to create a ruleset with a rule with an expression with more than just one else if. Somethin like:

if $hostname == ["sip", "proxysipint", "proxysipintb" ] then {
  set $.filename = "kam-int.log";

    }
else if $hostname == ["sipext", "proxysipext" ] then {
  set $.filename = "kam-ext.log";

    }
else if $hostname == ["pbxum", "pbxum1", "pbxum1b"] then {
  set $.filename = "ast-core.log";

    }

So, my code is like:

  - expression_filter:
      filter:
        if:
          expression: '$hostname == ["sip", "proxysipint", "proxysipintb" ]'
          tasks:
            - set:
                '$.filename': '"kam-int.log"'
        'else if':
          expression: '$hostname == ["sipext", "proxysipext" ]'
          tasks:
            - set:
                '$.filename': '"kam-ext.log"'
        'else if':
          expression: '$hostname == ["pbxum", "pbxum1", "pbxum1b"]'
          tasks:
            - set:
                '$.filename': '"ast-core.log"'

the problem with this hiera is that I have to use the same else if key for two different hash elements, so the second overrides the first.

I've been looking for a way to create such an expression, but as the template for an expression is:

<%- |
$filter_name,
$conditionals
| -%>
# <%= $filter_name %>
<%- $conditionals.each |$conditional, $options| { -%>
  <%- if $conditional == 'else' { -%>
<%= $conditional %> {
  <%- } else { -%>
<%= "${conditional} ${options['expression']} then" %> {
  <%-}-%>
<%- $options['tasks'].each |$task| { -%>
<%= epp('rsyslog/tasks.epp', { 'tasks' => $task }) -%>
<%-}-%>
}
<%}-%>

I can't find any way to do it, because the conditional clause is taken always from the key of the element in the hash.

Is there any way to create such config?

@amateo
Copy link
Contributor Author

amateo commented Oct 13, 2020

I have found that coding the yaml like:

  - expression_filter:
      filter:
        if:
          expression: '$hostname == ["sip", "proxysipint", "proxysipintb" ]'
          tasks:
            - set:
                '$.filename': '"kam-int.log"'
        "# else if 1\nelse if":
          expression: '$hostname == ["sipext", "proxysipext" ]'
          tasks:
            - set:
                '$.filename': '"kam-ext.log"'
        "# else if 2\nelse if":
          expression: '$hostname == ["pbxum", "pbxum1", "pbxum1b"]'
          tasks:
            - set:
                '$.filename': '"ast-core.log"'

worarounds the problem, but I can't find a better way

@dhollinger dhollinger added the bug Something isn't working label Oct 14, 2020
@dhollinger
Copy link
Member

I'll take a look at what I can do. I have some ideas.

@dhollinger
Copy link
Member

dhollinger commented Oct 15, 2020

@amateo I pushed an update to master that changes how the expression filter YAML is formatted a little. It is still compatible with current formats, it just is designed to address the issue you ran into. Your workaround gave me the idea to name the conditions rather than try and rely on users putting the if, else if, and else in there.

see https://github.com/voxpupuli/puppet-rsyslog#expression-based-filters for some updated examples

I am planning on pushing up a release to the puppet forge either tonight or tomorrow if you'd rather wait for that than pull from git.

@amateo
Copy link
Contributor Author

amateo commented Oct 16, 2020

Thank you @dhollinger, I'll try it as soon as the new release is published.

@amateo
Copy link
Contributor Author

amateo commented Oct 20, 2020

@dhollinger, have you released it in the forge?

@dhollinger
Copy link
Member

@amateo Not yet, will be before the end of the day (local time)

@dhollinger
Copy link
Member

@amateo The release should be live now

@amateo
Copy link
Contributor Author

amateo commented Oct 27, 2020

Ok. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants