首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法尝试使用community模块查询AWS TG以获取TG状态。

无法尝试使用community模块查询AWS TG以获取TG状态。
EN

Stack Overflow用户
提问于 2021-02-01 14:39:43
回答 1查看 88关注 0票数 0

我试图使用社区-aws模块查询AWS以获取TG状态,我希望每隔几秒钟查询一次特定的TG,直到看到该TG的状态是“健康的”为止。

到目前为止,我在“ansible”中完成了以下任务:

代码语言:javascript
复制
- name: Gather information about the target group attached to a particular LB
  vars:
    ansible_python_interpreter: /usr/bin/python3.6
  register: target_health
  community.aws.elb_target_group_info:
    region: "{{AWS_REGION}}"
    target_group_arns: "{{TARGET_GROUP_ARN}}"
    collect_targets_health: yes
  delegate_to: 127.0.0.1

- debug: msg="return_target_health ={{target_health}}"

- name: iterate items
  debug:
    msg: "{{ item.targets_health_description }}"
  with_items: "{{ target_health.target_groups }}"

剧本输出:

代码语言:javascript
复制
  TASK [service : debug] ****************************************************
  ok: [service.devbed-vpc.] => {
      "msg": "return_target_health ={'target_groups': [{'target_group_arn': 'arn:aws:elasticloadbalancing:us-east-1:4795703XXXXX:targetgroup/Testbed-Vee-8124-TG/b8b282d82426331c', 'target_group_name': 'Testbed-Vee-8124-TG', 'protocol': 'HTTP', 'port': 8124, 'vpc_id': 'vpc-19333d7f', 'health_check_protocol': 'HTTP', 'health_check_port': '8124', 'health_check_enabled': True, 'health_check_interval_seconds': 10, 'health_check_timeout_seconds': 5, 'healthy_threshold_count': 5, 'unhealthy_threshold_count': 2, 'health_check_path': '/health', 'matcher': {'http_code': '200'}, 'load_balancer_arns': ['arn:aws:elasticloadbalancing:us-east-1:4795703XXXXX:loadbalancer/app/Testbed-Vee-ALB/e2b8546cb7196017'], 'target_type': 'instance', 'protocol_version': 'HTTP1', 'stickiness_enabled': 'false', 'deregistration_delay_timeout_seconds': '300', 'stickiness_type': 'lb_cookie', 'stickiness_lb_cookie_duration_seconds': '86400', 'slow_start_duration_seconds': '0', 'load_balancing_algorithm_type': 'round_robin', 'tags': {'Env': 'Testbed'}, 'targets_health_description': [{'target': {'id': 'i-0b9b6e5a2775bXXXX', 'port': 8124}, 'health_check_port': '8124', 'target_health': {'state': 'healthy'}}, {'target': {'id': 'i-0feb307f8bdf6XXXX', 'port': 8124}, 'health_check_port': '8124', 'target_health': {'state': 'healthy'}}]}], 'failed': False, 'changed': False}"

  TASK [service : iterate items] ********************************************
  ok: [service.devbed-vpc.] => (item={'target_group_arn': 'arn:aws:elasticloadbalancing:us-east-1:4795703XXXXX:targetgroup/Testbed-Vee-8124-TG/b8b282d82426331c', 'target_group_name': 'Testbed-Vee-8124-TG', 'protocol': 'HTTP', 'port': 8124, 'vpc_id': 'vpc-19333d7f', 'health_check_protocol': 'HTTP', 'health_check_port': '8124', 'health_check_enabled': True, 'health_check_interval_seconds': 10, 'health_check_timeout_seconds': 5, 'healthy_threshold_count': 5, 'unhealthy_threshold_count': 2, 'health_check_path': '/health', 'matcher': {'http_code': '200'}, 'load_balancer_arns': ['arn:aws:elasticloadbalancing:us-east-1:4795703XXXXX:loadbalancer/app/Testbed-Vee-ALB/e2b8546cb7196017'], 'target_type': 'instance', 'protocol_version': 'HTTP1', 'stickiness_enabled': 'false', 'deregistration_delay_timeout_seconds': '300', 'stickiness_type': 'lb_cookie', 'stickiness_lb_cookie_duration_seconds': '86400', 'slow_start_duration_seconds': '0', 'load_balancing_algorithm_type': 'round_robin', 'tags': {'Env': 'Testbed'}, 'targets_health_description': [{'target': {'id': 'i-0b9b6e5a2775bXXXX', 'port': 8124}, 'health_check_port': '8124', 'target_health': {'state': 'healthy'}}, {'target': {'id': 'i-0feb307f8bdf6XXXX', 'port': 8124}, 'health_check_port': '8124', 'target_health': {'state': 'healthy'}}]}) => {
      "msg": [
          {
              "health_check_port": "8124",
              "target": {
                  "id": "i-0b9b6e5a2775bXXXX",
                  "port": 8124
              },
              "target_health": {
                  "state": "UNhealthy"
              }
          },
          {
              "health_check_port": "8124",
              "target": {
                  "id": "i-0feb307f8bdf6XXXX",
                  "port": 8124
              },
              "target_health": {
                  "state": "healthy"
              }
          }
      ]
  }

我想运行这个任务,直到两个"target_health:states"都恢复正常为止。我做不到,我能够将输出放到文件中,然后运行一个shell脚本来检查字符串"state":"healthy“是否不止一次累积。

但是后来我意识到这个文件实际上是静态的,我只给它写了一次,然后我在一个循环中运行这个脚本,这是没有任何意义的。

有没有一种方法来创建这个查询,直到得到我想要的结果,而不把它写到文件中?

EN

回答 1

Stack Overflow用户

发布于 2021-02-01 17:02:56

until:关键字会让您感兴趣

代码语言:javascript
复制
- name: Gather information about the target group attached to a particular LB
  vars:
    ansible_python_interpreter: /usr/bin/python3.6
  register: target_health
  community.aws.elb_target_group_info:
    region: "{{AWS_REGION}}"
    target_group_arns: "{{TARGET_GROUP_ARN}}"
    collect_targets_health: yes
  retries: 12
  delay: 5
  until: >-
    {{ (target_health.target_groups[0].targets_health_description|length) 
    ==  target_health.target_groups[0].targets_health_description
        | selectattr("target_health.state", "eq", "healthy") | list | length }}
  delegate_to: 127.0.0.1

或者,您可以使用威斯克利的等待,如果您希望让awscli为您服务(这不是很好的ansible-y,但它减少了大量的剧本日志输出作为ansible重试)

代码语言:javascript
复制
- command: >-
    aws --region {{ AWS_REGION }} elbv2 wait 
    target-in-service --target-group-arn {{ TARGET_GROUP_ARN | quote }}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65994616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档