Creating CloudWatch Alarm metrics using ansible

2019-09-10 11:14发布

问题:

I have a inventory file which looks like :

[database]
syd01-dev-shared.ce4l5of4bl3z.ap-southeast-2.rds.amazonaws.com

I need to configure 3 cloudwatch metrics for the above RDS endpoint, by reading the inventory. The DBInstanceIdentifier is actually syd01-dev-shared only, how can i actually pick this part and feed it to something like below??

- name: Create CPU utilization metric alarm
  sudo: false
  local_action: ec2_metric_alarm
                state=present
                region={{region}}
                name="HOW_TO_READ_DBInstanceOdentifier_NAME_FROM_Inventory"
                metric="CPUUtilization"
                statistic=Average comparison=">="
                threshold=80.0
                unit="Percent"
                period=300
                evaluation_periods=1
                description="It will be triggered when CPU utilization is more than 80% for 5 minutes"
                dimensions="InstanceId"="{{ec2_facts.ansible_facts.ansible_ec2_instance_id}}"
                alarm_actions=arn:aws:sns:us-east-1:123412341234:My_SNS_Notification
                ok_actions=arn:aws:sns:us-east-1:123412341234:My_SNS_Notification

I am not sure how to read the inventory and then split for what i am looking for.

回答1:

  - debug: var=groups.database[0].split('.')[0]

Output:

TASK: [debug var=groups.database[0].split('.')[0]] ****************************
ok: [127.0.0.1] => {
    "var": {
        "groups.database[0].split('.')[0]": "syd01-dev-shared"
    }
}