Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible parse stdout_lines to verify values of a particular item

I am using ansible to write some tests. I have to parse through the output of a command (stdout_lines) and verify the information corresponding to a particular name. The stdout_lines looks like the following.

The output is obtained from a cli command executed in bash.

"stdout_lines": [
        "----------------------------------------------------------------------------------------",
        "|       Name               |      Count   |  Score | State|",
        "----------------------------------------------------------------------------------------",
        "| Jake                     |             5| 10     |   CA |",
        "| Mike                     |             3| 15     |   AR |",
        "----------------------------------------------------------------------------------------",
        "|Total Scores: 2                                          |",
        "----------------------------------------------------------------------------------------"
    ]

I would like to parse over the stdout_lines and find out the information related to, say for example 'Jake', and then verify if the the corresponding values are correct.

If in Python, I would Split the string into a list, find list-element that has Jake at [0] index and verify the other elements in it. I tried looking up but couldnot stumble upon anything that could help me. Can anyone throw some light on how to do this. Appreciate your help.

Thanks in advance,

like image 455
user3543477 Avatar asked Jan 31 '19 01:01

user3543477


1 Answers

here is a working example to get you started. i simulated your stdout_lines with the test_var.

  • we parse the test_var to get lines with 6 columns, when split with |.
  • we parse the list of rows from above task and try to find rows with 2nd column = Jake.
  • assuming its only 1 result (if you may have more rows, additional tasks are needed), get the 3 attributes in 3 variables and finally
  • print results

playbook:

---
- hosts: localhost
  gather_facts: false
  vars:
    search_name: Jake
    test_var: 
        - "----------------------------------------------------------------------------------------"
        - "|       Name               |      Count   |  Score | State|"
        - "----------------------------------------------------------------------------------------"
        - "| Jake                     |             5| 10     |   CA |"
        - "| Mike                     |             3| 15     |   AR |"
        - "| Jane                     |             3| 15     |   AR |"
        - "----------------------------------------------------------------------------------------"
        - "|Total Scores: 2                                          |"
        - "----------------------------------------------------------------------------------------"

  tasks:
  - name: pick up the lines we are interested in.
    set_fact:
      important_lines: "{{ important_lines|default([]) +  [item] }}"
    when: item.split('|') | length == 6
    with_items:
    - "{{ test_var }}"

  - name: find the line with the name we are looking for in 2nd column
    set_fact:
      target_line: "{{ item }}"
    when: item|trim is search(search_name)
    with_items:
    - "{{ important_lines }}"

  - name: get the 3 attributes from the target line
    set_fact:
      attribute_count: "{{ target_line.split('|')[2]|trim }}"
      attribute_score: "{{ target_line.split('|')[3]|trim }}"
      attribute_state: "{{ target_line.split('|')[4]|trim }}"

  - name: print results
    debug:
      msg: "name: {{ search_name }}, count: {{ attribute_count }}, score: {{ attribute_score }}, state: {{ attribute_state }}"

result:

[http_offline@greenhat-29 tests]$ ansible-playbook test.yml 

PLAY [localhost] *******************************************************************************************************************************************************************************************************

TASK [pick up the lines we are interested in.] *************************************************************************************************************************************************************************
skipping: [localhost] => (item=----------------------------------------------------------------------------------------) 
ok: [localhost] => (item=|       Name               |      Count   |  Score | State|)
skipping: [localhost] => (item=----------------------------------------------------------------------------------------) 
ok: [localhost] => (item=| Jake                     |             5| 10     |   CA |)
ok: [localhost] => (item=| Mike                     |             3| 15     |   AR |)
ok: [localhost] => (item=| Jane                     |             3| 15     |   AR |)
skipping: [localhost] => (item=----------------------------------------------------------------------------------------) 
skipping: [localhost] => (item=|Total Scores: 2                                          |) 
skipping: [localhost] => (item=----------------------------------------------------------------------------------------) 

TASK [find the line with the name we are looking for in 2nd column] ****************************************************************************************************************************************************
skipping: [localhost] => (item=|       Name               |      Count   |  Score | State|) 
ok: [localhost] => (item=| Jake                     |             5| 10     |   CA |)
skipping: [localhost] => (item=| Mike                     |             3| 15     |   AR |) 
skipping: [localhost] => (item=| Jane                     |             3| 15     |   AR |) 

TASK [get the 3 attributes from the target line] ***********************************************************************************************************************************************************************
ok: [localhost]

TASK [print results] ***************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "name: Jake, count: 5, score: 10, state: CA"
}

PLAY RECAP *************************************************************************************************************************************************************************************************************
localhost                  : ok=4    changed=0    unreachable=0    failed=0   

[http_offline@greenhat-29 tests]$ 

hope it helps

like image 82
ilias-sp Avatar answered Sep 27 '22 21:09

ilias-sp