使用 ec2_metric_alarm 时获得 "AlarmName must not contain leading or trailing whitespace"

Getting "AlarmName must not contain leading or trailing whitespace" when using ec2_metric_alarm

我正在尝试使用 ansible 模块 ec2_metric_alarm,为我的 EC2 实例创建实例状态警报,但我确实不断收到错误消息 "AlarmName must not contain leading or trailing whitespace",不确定我在这里做错了什么我的警报名称中没有使用任何空格,但我仍然不断收到相同的错误。任何建议将不胜感激。

这是我的代码:

- name: Create Instance Check Alarm
  ec2_metric_alarm:
    state: present
    security_token: "{{security_token}}"
    region: "{{region}}"
    name: "{{cluster_name}}_StatusCheckFailed_Instance_{{new_instance_id}}"
    metric: "StatusCheckFailed_Instance"
    namespace: "AWS/EC2"
    statistic: "Maximum"
    evaluation_periods: 2
    comparison: ">="
    threshold: 1.0
    period: 60
    unit: "Count"
    description: "This will alarm will Reboot Instance when there is a Instance Check fails"
    dimensions: {'InstanceId': "{{new_instance_id}}"}
    alarm_actions: ["arn:aws:sns:{{region}}:SNS_TOPIC:NotifyMe","arn:aws:automate:{{region}}:ec2:reboot"]
  delegate_to: localhost

- name: Create System Fail Alarm
  ec2_metric_alarm:
    state: present
    security_token: "{{security_token}}"
    region: "{{region}}"
    name: "{{cluster_name}}_StatusCheckFailed_Instance_{{new_instance_id}}"
    metric: "StatusCheckFailed_System"
    namespace: "AWS/EC2"
    statistic: "Maximum"
    evaluation_periods: 2
    comparison: ">="
    threshold: 1.0
    period: 60
    unit: "Count"
    description: "This will alarm will Auto Recover Instance when there is a System Failure Identified"
    dimensions: {'InstanceId': "{{new_instance_id}}"}
    alarm_actions: ["arn:aws:sns:{{region}}:SNS_TOPIC:NotifyMe","arn:aws:automate:{{region}}:ec2:recover"]
  delegate_to: localhost



 The full traceback is:
Traceback (most recent call last):
  File "/uhome/user/.ansible/tmp/ansible-tmp-1573183650.92-219299995957066/AnsiballZ_ec2_metric_alarm.py", line 113, in <module>
    _ansiballz_main()
  File "/uhome/user/.ansible/tmp/ansible-tmp-1573183650.92-219299995957066/AnsiballZ_ec2_metric_alarm.py", line 105, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/uhome/user/.ansible/tmp/ansible-tmp-1573183650.92-219299995957066/AnsiballZ_ec2_metric_alarm.py", line 48, in invoke_module
    imp.load_module('__main__', mod, module, MOD_DESC)
  File "/tmp/ansible_ec2_metric_alarm_payload_F0yFcU/__main__.py", line 320, in <module>
  File "/tmp/ansible_ec2_metric_alarm_payload_F0yFcU/__main__.py", line 314, in main
  File "/tmp/ansible_ec2_metric_alarm_payload_F0yFcU/__main__.py", line 171, in create_metric_alarm
  File "/uhome/user/lib/python2.7/site-packages/boto/ec2/cloudwatch/__init__.py", line 382, in describe_alarms
    [('MetricAlarms', MetricAlarms)])
  File "/uhome/user/lib/python2.7/site-packages/boto/connection.py", line 1186, in get_list
    raise self.ResponseError(response.status, response.reason, body)
boto.exception.BotoServerError: BotoServerError: 400 Bad Request
<ErrorResponse xmlns="http://monitoring.amazonaws.com/doc/2010-08-01/">
  <Error>
    <Type>Sender</Type>
    <Code>ValidationError</Code>
    <Message>AlarmName must not contain leading or trailing whitespace</Message>
  </Error>
  <RequestId>ab8f10ca-d9cd-4964-945e-34727c77a09a</RequestId>
</ErrorResponse>

i am not using any spaces in my Alarm Name, but still i keep getting the same error

我敢打赌,仅根据您提供的代码片段,cluster_name 有前导 space。我可以用

来演示
- hosts: all
  vars:
    cluster_name: " x"
  tasks:
  - set_fact:
      alarm_name: "{{cluster_name}}y"
  - debug:
      msg: alarm name has {{alarm_name|length}} characters

当然会发出 alarm name has 3 characters 因为前导 space.

您可以通过以下两种方法之一解决该问题:实际追踪 space 被引入 cluster_name 的位置,或者回避该问题并使用 cluster_name 进行过滤=17=] 完成一项任务