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

Using apply in a service causes service_name not to be applied to service leading to duplicate resource issues #429

Closed
rick-pri opened this issue Feb 15, 2018 · 5 comments
Labels

Comments

@rick-pri
Copy link

When using apply in an exported icinga2::object::service resource the resulting service does not have a service name meaning if you want to use the same variable sets for different service check commands you get duplicate resource issues.

  @@::icinga2::object::service { 'Varnish Backends':                                                     
    target           => "${icinga2_conf_dir}/services.conf",                                             
    apply            => 'backends => config in host.vars.varnish',                                       
    import           => [ 'generic-service' ],      
    command_endpoint => 'host.name',                
    check_command    => 'check_varnish_backends',   
    vars             => 'vars + config',            
    assign           => [ 'host.vars.varnish' ],    
  }                                                 

  @@::icinga2::object::service { 'Varnish Cache':   
    target           => "${icinga2_conf_dir}/services.conf",                                             
    apply            => 'cache => config in host.vars.varnish',                                          
    import           => [ 'generic-service' ],      
    command_endpoint => 'host.name',                
    check_command    => 'check_varnish_cache',      
    vars             => 'vars + config',            
    assign           => [ 'host.vars.varnish' ],    
  }  

Should result in something like the following because the resource title is used for the service name:

apply Service "Varnish Backends" for (backends => config in host.vars.varnish) to Host {
  import "generic-service"

  check_command = "check_varnish_backends"
  command_endpoint = host.name
  vars = vars + config
  assign where host.vars.varnish
}

apply Service "Varnish Cache" for (cache => config in host.vars.varnish) to Host {
  import "generic-service"

  check_command = "check_varnish_cache"
  command_endpoint = host.name
  vars = vars + config
  assign where host.vars.varnish
}

But actually results in:

apply Service for (backends => config in host.vars.varnish) to Host {
  import "generic-service"

  check_command = "check_varnish_backends"
  command_endpoint = host.name
  vars = vars + config
  assign where host.vars.varnish
}

apply Service for (cache => config in host.vars.varnish) to Host {
  import "generic-service"

  check_command = "check_varnish_cache"
  command_endpoint = host.name
  vars = vars + config
  assign where host.vars.varnish
}

I can see that this doesn't happen when I don't use apply on a service object.

As such I get the following error on a reload of icinga2 if I don't manually add the service name:

critical/config: Error: An object with type 'Service' and name 'icinga2-master-1.c.gcp.internal!varnish' already exists (in /usr/local/etc/icinga2/conf.d/services.conf: 12:1-12:64), new declaration: in /usr/local/etc/icinga2/conf.d/services.conf: 3:1-3:67
Location: in /usr/local/etc/icinga2/conf.d/services.conf: 3:1-3:67
/usr/local/etc/icinga2/conf.d/services.conf(1): # This file is managed by Puppet. DO NOT EDIT.
/usr/local/etc/icinga2/conf.d/services.conf(2): 
/usr/local/etc/icinga2/conf.d/services.conf(3): apply Service for (backends => config in host.vars.varnish) to Host {
                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/usr/local/etc/icinga2/conf.d/services.conf(4):   import "generic-service"
/usr/local/etc/icinga2/conf.d/services.conf(5): 

If I add the service names for the two Service objects manually on the master then I don't get the configuration conflict issue and the Services are applied correctly and as expected.

My guess that it's an issue with the way that the object template is contructed but having looked at it it's impenetrably incomprehensible to me so I can't work out how to change it to make a pull request.

  • Module version mod 'icinga-icinga2', '1.3.5'
  • Puppet version 4.10.8
  • FreeBSD 11.1

Kind regards,
Richard

@lbetz
Copy link
Member

lbetz commented Feb 15, 2018

Looks completly correct for me. The 'name' to an 'apply for' acts as an prefix to the service name. In your case the service name is given by the content of backends or cache. And this depends on the dictionaries you use.

@lbetz
Copy link
Member

lbetz commented Feb 15, 2018

By the way you're use the same dictionary to apply two differnet services, set the prefix attribute to get different names.

@rick-pri
Copy link
Author

Hmmm, okay, if I change the puppet code to this:

  @@::icinga2::object::service { 'Varnish Backends':                                                     
    target           => "${icinga2_conf_dir}/services.conf",                                             
    apply            => 'backends => config in host.vars.varnish.backends',                                       
    import           => [ 'generic-service' ],      
    command_endpoint => 'host.name',                
    check_command    => 'check_varnish_backends',   
    vars             => 'vars + config',            
    assign           => [ 'host.vars.varnish' ],    
  }                                                 

  @@::icinga2::object::service { 'Varnish Cache':   
    target           => "${icinga2_conf_dir}/services.conf",                                             
    apply            => 'cache => config in host.vars.varnish.cache',                                          
    import           => [ 'generic-service' ],      
    command_endpoint => 'host.name',                
    check_command    => 'check_varnish_cache',      
    vars             => 'vars + config',            
    assign           => [ 'host.vars.varnish' ],    
  }  

I have a different problem then, I get an error saying that the service doesn't match anywhere:

warning/ApplyRule: Apply rule '' (in /usr/local/etc/icinga2/conf.d/services.conf: 103:1-103:76) for type 'Service' does not match anywhere!
warning/ApplyRule: Apply rule '' (in /usr/local/etc/icinga2/conf.d/services.conf: 112:1-112:70) for type 'Service' does not match anywhere!

As you can see it doesn't get a name here either. I think that I'm not understanding something about how the hashed data is accessed. This is my structure in yaml:

deploy::host_vars:
  'os': "%{facts.operatingsystem}"
  'notification':
    'mail':
      'users': ['non-production']
  'varnish':
    'varnish':
      'backends':
        'backend': 'MAIN.backend_fail -w 1 -c 2'
      'cache':
        'ratio': 'ratio -w 20:90 -c 15:95'

And my hosts' vars look like this:

  vars.varnish["varnish"] = {
    backends = {
      backend = "MAIN.backend_fail -w 1 -c 2"
    }
    cache = {
      ratio = "ratio -w 20:90 -c 15:95"
    }
  }

Yet I can see that these checks are not applied.

@lbetz
Copy link
Member

lbetz commented Feb 16, 2018

Please use the parameter prefix of service.

@rick-pri
Copy link
Author

Sorry I posted this comment and then I went and checked the prefix parameter out and this is exactly what I needed. Many thanks @lbetz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants