【问题标题】:Ansible try to query an AWS TG using community-aws module to get TG statusAnsible 尝试使用 community-aws 模块查询 AWS TG 以获取 TG 状态
【发布时间】:2021-05-05 17:49:09
【问题描述】:

我正在尝试使用 community-aws module 查询 AWS TG 以获取 TG 状态, 我想每隔几秒查询一次特定的 TG,直到我看到该 TG 的状态为“健康”。

到目前为止,我在 ansible 中有这个任务:

- 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 }}"

剧本输出:

  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" 是否累积不止一次。

但后来我意识到该文件实际上是静态的,我只对其写入一次,然后我才在没有任何意义的循环中运行脚本。

有没有办法创建这个查询,直到我得到我想要的正确结果而不将它写入文件?

【问题讨论】:

    标签: ansible


    【解决方案1】:

    retries:, delay:, and until: 关键字会让您感兴趣

    - 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 为您停止,也可以使用 awscli's wait(它不是很可靠,但它可以减少大量的 playbook 日志输出作为 ansible 重试)

    - command: >-
        aws --region {{ AWS_REGION }} elbv2 wait 
        target-in-service --target-group-arn {{ TARGET_GROUP_ARN | quote }}
    

    【讨论】:

    • 我猜解析中有问题,因为我得到:fatal: [service.devbed-vpc.]: FAILED! => {"msg": "条件检查 '{{ (target_health.target_groups|length) != target_health.target_groups\n | selectattr(\"target_health.state\", \"eq\", \"healthy\" ) | list | length }}' 失败。错误是:评估条件时出错 ({{ (target_health.target_groups|length) != target_health.target_groups\n | selectattr(\"target_health.state\", \"eq\ ", \"healthy\") | list | length }}): 'dict object' 没有属性 'target_health'"}
    • 啊,抱歉,我错过了msg: {{ item.targets_health_description }} 部分,所以selectattr 太低了一级
    • 首先非常感谢,只是想告诉你我最终使用了 awscli,它运行良好。关于更 ansbley 的方式,它仍然失败,看起来比较中的某些东西不起作用,因为它似乎无法验证我的健康 TG。失败 - 重试:收集有关附加到特定 LB(2) 的目标组的信息(剩余 2 次重试)。失败 - 重试:收集有关附加到特定 LB(2) 的目标组的信息(剩余 1 次重试)。
    • 啊,我明白了:我发明了这个测试,因为until: 等待真实,但我用!= 编写了它;抱歉,我没有 TG 来测试行为,所以在文本框中进行编码。但我很高兴 awscli 方法对您有用。请考虑接受答案,让其他人知道它解决了您的问题
    猜你喜欢
    • 2019-05-17
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    相关资源
    最近更新 更多