如何配置Jinja配置文件以包含基于通用值的服务检查?

时间:2016-04-11 09:30:32

标签: ansible jinja2 ansible-playbook

我公司的ansible配置为某些服务构建Nagios模板。

例如:

      define service {
            use                     rabbit-critical-service         ; Name of service template to use
            service_description     RabbitMQ {{ queue_name }} queue size
            check_command                   check_graphite_data!1000!15000!rabbitmq._.rabbitmq_messages.{{ queue_name }}.value
            host_name               {{ rabbitmq_server }}
            notifications_enabled   1
            servicegroups           rabbitmq
    }

模板使用相同的所有队列阈值自动构建检查。队列名称在ansible_playbooks/roles/nagios/defaults/main.yml下的不同文件中配置,并使用Jinja自动生成。

我已经完成了任务,可以以一种简单的方式编辑配置,以便为某些特定检查包含不同的阈值,但我很难理解如何操作。< / p>

我想到了类似的东西: 将service_check模板添加到以下内容中:

{% if queue_size_specific_vars is not defined %}
create the check using the current configuration...
{% else %}
create the check using the specific configuration which will be found in the `default/main.yml` file in the newly created generic value "queue_size_specific_vars" which is supposed to include two values, one for warning value and one for critical value.
{% endif %}

我的问题是:

  
      
  1. 如何实现目标(同时考虑两个值而不是一个)?
  2.   
  3. 我不确定这是实现目标的最佳途径,你知道更好的方法吗?
  4.   

编辑#1:

似乎我没有正确解释自己,让我向您展示模板的更大部分:

{% for queue_name in queues %}
define service {
        use                     rabbit-critical-service         ; Name of service template to use
        service_description     RabbitMQ {{ queue_name }} queue active consumers
        check_command                   check_graphite_data!0.9!0.9!rabbitmq._.rabbitmq_consumers.{{ queue_name }}!reverse
        host_name               {{ rabbitmq_server }}
        notifications_enabled   1
        servicegroups           rabbitmq
        max_check_attempts      4
}

{% if vars_production is not defined %}
define service {
        use                     generic-service
        service_description     RabbitMQ {{ queue_name }} queue read/write ratio
        check_command                   check_graphite!'http://{{ graphite_server }}:{{ graphite_port }}/render/?from=-10minutes&target=scale(divideSeries(offset(prod-rabbit-1.rabbitmq._.rabbitmq_messages.{{queue_name}}.value,1),offset(derivative(sumSeries(prod-rabbit-1.rabbitmq._.rabbitmq_deliver_get.{{queue_name}})),1)),100)&rawData'!1500!2000!avg
        host_name               {{ rabbitmq_server }}
        notifications_enabled   1
        servicegroups           rabbitmq
        max_check_attempts      4
}
{% endif %}
define service {
        use                     rabbit-critical-service         ; Name of service template to use
        service_description     RabbitMQ {{ queue_name }} queue size
        check_command                   check_graphite_data!1000!15000!rabbitmq._.rabbitmq_messages.{{ queue_name }}.value
        host_name               {{ rabbitmq_server }}
        notifications_enabled   1
        servicegroups           rabbitmq
}

{% endfor %}

这意味着我应该实现的更改应该在队列级别而不是在主机名中,因为ansible&#34;知道&#34;只有主持人,不知道&#34;知道&#34;队列。

我们需要设置&#34; RabbitMQ {{queue_name}}队列大小&#34;每个队列,并使用默认值。 我期望做的改变应该发生在模板文件中。

示例:

{% if special_queue_exists %}
do ....
{% else %}
create it in the normal method
{% endif %}

编辑#2:

我的经理要我以类似这样的方式做到这一点: 我已经拥有包含所有队列的文件,让我们调用这个文件&#34; queues_file&#34;。 他希望我创建另一个文件,其中包含特定检查值列表(警告和严重),让我们调用此文件&#34; specific_values&#34;。

然后做这样的事情:

queues_with_specific_metrics:
  - entities:
    - warn: "1000"
    - crit: "20000"

然后检查我正在运行的队列(来自for循环)是否在&#34; specific_values&#34;中有特定的配置。文件,如果是,那么来自&#34; specific_values&#34; file应该覆盖默认值。 然后,我可以这样做:

{{ queues_with_specific_metrics.queue.warn | default(1000) }}

我是Ansible和Jinja的新手,这就是为什么我不能理解,对此感到抱歉。

编辑#3: 我按照您的建议编辑了配置,但我不确定如何编写if语句......

默认值/ main.yml包含所有队列名称的列表。

我已经打开了一个名为spec_params.yml的新文件,并作为main.yml文件驻留在默认文件夹中。

spec_params.yml文件:

nagios_specific_queue_params:
  queue1: {}
  entities:
    warn: 2000
    crit: 20000

rabbitmq.cfg.j2的相关部分:

{% if queue_name in nagios_specific_queue_params %}
define service {
        use                     rabbit-critical-service         ; Name of service template to use
        service_description     RabbitMQ {{ queue_name }} queue size
        check_command           check_graphite_data!{{ queues[queue_name] ['warn'] |default(1000) }}!{{ queues[queue_name] ['crit'] | default(15000) }}!rabbitmq._.rabbitmq_messages.{{ queue_name }}.value
        host_name               {{ rabbitmq_server }}
        notifications_enabled   1
        servicegroups           rabbitmq
}
{% else %}
define service {
        use                     rabbit-critical-service         ; Name of service template to use
        service_description     RabbitMQ {{ queue_name }} queue size
        check_command                   check_graphite_data!1000!15000!rabbitmq._.rabbitmq_messages.{{ queue_name }}.value
        host_name               {{ rabbitmq_server }}
        notifications_enabled   1
        servicegroups           rabbitmq
}
{% endif %}

当我运行剧本时,我收到以下错误:

fatal: [monitoring] => {'msg': "TypeError: argument of type 'StrictUndefined' is not iterable", 'failed': True}
fatal: [monitoring] => {'msg': 'One or more items failed.', 'failed': True, 'changed': False, 'results'

知道它失败的原因吗?

提前致谢

1 个答案:

答案 0 :(得分:0)

这是我完成任务所采取的步骤:

nagios/defaults/main.yml中,我添加了:

nagios_queue_size_default_values:
  warn: 1000
  crit: 15000
nagios_queue_size_override_values:
  entities_prod_whatever_2:
    warn: 600
    crit: 900

然后,在rabbitmq.cfg.j2中,我已将检查命令更改为此命令:

check_command     check_graphite_data!{{ nagios_queue_size_override_values.get(queue_name, nagios_queue_size_default_values).warn }}!{{ nagios_queue_size_override_values.get(queue_name, nagios_queue_size_default_values).crit }}!rabbitmq._.rabbitmq_messages.{{ queue_name }}.value

现在运行playbook会根据需要创建模板。