1

Would there be any way to retrieve only the value of row_count from STDOUT and ansible playbook to then fail if the row_count is more than 10?
(row_count > 10)

STDOUT:

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.

row_count = 13

1 Answer 1

2

Next to the stdout attribute the registered variable has also the attribute stdout_lines. For example (simplified for testing),

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

Declare the variable. Get the last line and split the value

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

gives

  row_count: '3'

Test the number of rows

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

Example of a complete playbook for testing

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .