gpt4 book ai didi

Ansible 解析 stdout_lines 以验证特定项目的值

转载 作者:行者123 更新时间:2023-12-03 21:20:37 25 4
gpt4 key购买 nike

我正在使用 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 中,我会将字符串拆分为一个列表,找到在 [0] 索引处有 Jake 的列表元素并验证其中的其他元素。我试着向上看,但找不到任何可以帮助我的东西。任何人都可以对如何做到这一点有所了解。感谢你的帮助。

提前致谢,

最佳答案

这是一个可以帮助您入门的工作示例。我模拟了你的 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]$

    希望能帮助到你

    关于Ansible 解析 stdout_lines 以验证特定项目的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54452070/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com