Ansible 从 stdout 获取值,如果变量超过定义的阈值,则失败

Ansible 从 stdout 获取值,如果变量超过定义的阈值,则失败

ansible如果 row_count 大于 10,是否有任何方法可以从 STDOUT 和 playbook 中仅检索 row_count 的值,然后失败?
(行数 > 10)

标准输出:

temp_id,order_id,status,created_at
854556545610443,,order_success,2022-08-23 09:29:29
854556545610444,,order_success,2022-08-23 09:37:02
854556545610445,,order_success,2022-08-23 09:38:47
854556545610446,,order_success,2022-08-23 12:40:41
854556545610447,,order_success,2022-08-24 07:53:54
854556545610448,,order_success,2022-08-24 10:11:48
854556545610449,,order_success,2022-08-24 14:34:37
854556545610450,,order_success,2022-08-24 23:49:52
854556545611146,,order_success,2022-09-16 12:55:57
854556545611147,,order_success,2022-09-16 12:56:00
854556545611148,,order_success,2022-09-16 12:56:07
854556545611149,,order_success,2022-09-16 12:56:07
854556545611150,,order_success,2022-09-16 12:56:10   
13 row(s) has been generated successfully.

行数 = 13

答案1

旁边的标准输出属性 注册变量也有属性标准输出行。例如(为了测试而简化),

    out:
      stdout_lines:
        - line_01
        - line_02
        - line_03
        - 3 row(s) has been generated successfully.
        - row_count = 3

声明变量。获取最后一行并拆分值

  row_count: "{{ out.stdout_lines|last|split('=')|last|int }}"

给出

  row_count: '3'

测试行数

    - assert:
        that: row_count|int < 3
        fail_msg: "[ERR] More than 2 rows. row_count={{ row_count }}"

用于测试的完整剧本示例

- hosts: localhost

  vars:

    out:
      stdout_lines:
        - line_01
        - line_02
        - line_03
        - 3 row(s) has been generated successfully.
        - row_count = 3

    row_count: "{{ out.stdout_lines|last|split('=')|last|int }}"

  tasks:

    - debug:
        var: row_count

    - assert:
        that: row_count|int < 3
        fail_msg: "[ERR] More than 2 rows. row_count={{ row_count }}"

相关内容