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

时间:2019-01-31 01:26:44

标签: ansible ansible-2.x

我正在使用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的列表元素,并验证其中的其他元素。我试着抬头,但没有发现任何对我有帮助的东西。任何人都可以对如何做到这一点有所了解。感谢您的帮助。

谢谢,

1 个答案:

答案 0 :(得分:1)

这是一个使您入门的有效示例。我用stdout_lines模拟了您的test_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]$ 

希望有帮助