Ansible 解析 stdout_lines 以验证特定项目的值
Ansible parse stdout_lines to verify values of a particular item
我正在使用 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 的列表元素并验证其中的其他元素。我试图抬头看,但找不到任何可以帮助我的东西。任何人都可以阐明如何执行此操作。感谢你的帮助。
提前致谢,
这是一个可以帮助您入门的工作示例。我用 test_var
.
模拟了你的 stdout_lines
- 我们解析
test_var
以获得 6 列的行,当用 |
拆分时。
- 我们解析上述任务中的行列表,并尝试找到第 2 列 =
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) 的输出并验证与特定名称对应的信息。 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 的列表元素并验证其中的其他元素。我试图抬头看,但找不到任何可以帮助我的东西。任何人都可以阐明如何执行此操作。感谢你的帮助。
提前致谢,
这是一个可以帮助您入门的工作示例。我用 test_var
.
stdout_lines
- 我们解析
test_var
以获得 6 列的行,当用|
拆分时。 - 我们解析上述任务中的行列表,并尝试找到第 2 列 =
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]$
希望对您有所帮助