首页 > 解决方案 > Ansible 解析 stdout_lines 以验证特定项目的值

问题描述

我正在使用 ansible 编写一些测试。我必须解析命令(stdout_lines)的输出并验证与特定名称对应的信息。stdout_lines 如下所示。

输出是从 bash 中执行的 cli 命令获得的。

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

我想解析 stdout_lines 并找出与例如“Jake”相关的信息,然后验证相应的值是否正确。

如果在 Python 中,我会将字符串拆分为一个列表,找到 Jake 在 [0] 索引处的列表元素并验证其中的其他元素。我试着抬头看,但找不到任何可以帮助我的东西。任何人都可以阐明如何做到这一点。感谢你的帮助。

提前致谢,

标签: ansibleansible-2.x

解决方案


这是一个可以帮助您入门的工作示例。我stdout_linestest_var.

  • 我们解析test_var以得到 6 列的行,当使用|.
  • 我们从上面的任务中解析行列表并尝试找到第二列 = 的行Jake
  • 假设它只有 1 个结果(如果您可能有更多行,则需要额外的任务),获取 3 个变量中的 3 个属性,最后
  • 打印结果

剧本:

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

结果:

[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]$ 

希望能帮助到你


推荐阅读