在我的剧本下,我有一个执行脚本的任务:
- name: Execute Update on selected databases
shell: "./script_update.sh server007 {{item}} {{bdd_config[item].updateFile }}"
with_items: "{{selected_DBS}}"
register: updateResult
此脚本的结果可能是这样的:
INFO - ================================================================================
INFO - BEGIN 'script_update' ON ini99db1 AT 2019/05/22 12:22:06
INFO - ================================================================================
INFO - THE MySQL SERVER server007 EXISTS
INFO - THE MySQL SERVER server007 IS ON
INFO - THE DATABASE myDB EXISTS
INFO - FILE /opt/myscode_In_progress.sql EXISTS.
ERROR - ERROR 1064 (42000) at line 4 in file: '/opt/myscode_In_progress.sql': You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'azdazdazdazdazd' at line 1
INFO - SCRIPT OUTPUT : SEE LOG FILE 1
INFO - THE DB HAS BEEN CORRECTLY UPDATED
INFO - --------------------------------------------------------------------------------
INFO - THE PROCESS TERMINATED SUCCESSFULLY
INFO - SEE THE LOG FILE /opt/mysql/log/app_20190522_12H22.log
在显示我的注册变量(updateResult)的输出时,它给出了如下内容:
"msg": {
"changed": true,
"msg": "All items completed",
"results": [
{
"_ansible_ignore_errors": null,
"_ansible_item_label": "nomadisdb",
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "./rcd_db_update.ksh myserver01 nomadisdb nomadisdb_In_progress.sql",
"delta": "0:00:12.786607",
"end": "2019-05-22 12:36:52.817077",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "./rcd_db_update.ksh myserver01 nomadisdb nomadisdb_In_progress.sql",
"_uses_shell": true,
"argv": null,
"chdir": "/opt/application/i99/current/sh",
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"warn": true
}
},
"item": "nomadisdb",
"rc": 0,
"start": "2019-05-22 12:36:40.030470",
"stdout_lines": [
"\tINFO - ================================================================================",
"\tINFO - BEGIN 'rcd_db_update' ON ini99db1 AT 2019/05/22 12:36:50",
"\tINFO - ================================================================================",
"\tINFO - THE MySQL SERVER myserver01 EXISTS",
"\tINFO - THE MySQL SERVER myserver01 IS ON",
"\tINFO - THE DATABASE nomadisdb EXISTS",
"\tINFO - FILE /opt/application/i99/current/sql/nomadisdb_In_progress.sql EXISTS.",
"\tERROR - ERROR 1060 (42S21) at line 4 in file: '/opt/myDB_In_progress.sql': Duplicate column name 'con_habilitation_version'",
"\tINFO - SCRIPT OUTPUT : SEE LOG FILE 1",
"\tINFO - THE DB HAS BEEN CORRECTLY UPDATED",
"\tINFO - --------------------------------------------------------------------------------",
"\tINFO - THE PROCESS TERMINATED SUCCESSFULLY",
"\tINFO - SEE THE LOG FILE /opt/mysql/log/rcd_db_update_myserver01_nomadisdb_20190522_12H36.log",
"\tINFO - ================================================================================",
"\tINFO - END 'rcd_db_update.ksh' ON ini99db1 AT 2019/05/22 12:36:50",
"\tINFO - ================================================================================"
]
}
]
}
}
我的目的是在此输出消息中进行grep并搜索任何 ERROR 行,例如:
"\tERROR - ERROR 1060 (42S21) at line 4 in file: '/opt/myDB_In_progress.sql': Duplicate column name 'con_habilitation_version'",
我可以使用 regExp 搜索它,查找以“ ERROR ”开头并且包括“ atline ”和“ < strong>在文件中”
最后我应该得到整行
我试图使用这样的正则表达式:
- name: set regex
set_fact:
regExpOfSqlError: ' '
become_user: mysql
- set_fact:
errorLine: "{{ updateResult.results | regex_search(regExpOfSqlError, ' ') }}"
become_user: mysql
但是我仍然想知道应该输入什么 RegExp 来搜索合适的行并输出
建议?
答案 0 :(得分:0)
一个简单的解决方案是使用json_query
过滤器。
注意:
json_query
依赖于jmespath
。您需要pip(3) install jmespath
才能运行示例。to_json | from_json
函数时,work arround a known bug需要contains
黑客。这需要modification on jmespath side仍在等待批准/开发jmespath
documentation以获得更多详细信息,但这是我的示例中对查询字符串的快速说明:创建所有输入元素(updateResults.result
)的列表投影,然后选择所有元素在包含文本stdout_lines
的{{1}}条目中,最后将结果投影为平面列表。ERROR
哪个给:
---
- name: SO Test
hosts: localhost
vars:
# This result var mimics your current captured result
# I only kept the relevant part for the example.
updateResult:
results:
- stdout_lines: [
"\tINFO - ================================================================================",
"\tINFO - BEGIN 'rcd_db_update' ON ini99db1 AT 2019/05/22 12:36:50",
"\tINFO - ================================================================================",
"\tINFO - THE MySQL SERVER myserver01 EXISTS",
"\tINFO - THE MySQL SERVER myserver01 IS ON",
"\tINFO - THE DATABASE nomadisdb EXISTS",
"\tINFO - FILE /opt/application/i99/current/sql/nomadisdb_In_progress.sql EXISTS.",
"\tERROR - ERROR 1060 (42S21) at line 4 in file: '/opt/myDB_In_progress.sql': Duplicate column name 'con_habilitation_version'",
"\tINFO - SCRIPT OUTPUT : SEE LOG FILE 1",
"\tINFO - THE DB HAS BEEN CORRECTLY UPDATED",
"\tINFO - --------------------------------------------------------------------------------",
"\tINFO - THE PROCESS TERMINATED SUCCESSFULLY",
"\tINFO - SEE THE LOG FILE /opt/mysql/log/rcd_db_update_myserver01_nomadisdb_20190522_12H36.log",
"\tINFO - ================================================================================",
"\tINFO - END 'rcd_db_update.ksh' ON ini99db1 AT 2019/05/22 12:36:50",
"\tINFO - ================================================================================"
]
tasks:
- name: Capture a list of all lines containing an error in all results
set_fact:
error_lines: "{{ updateResult.results | to_json | from_json | json_query(\"[].stdout_lines[?contains(@, 'ERROR')][]\") }}"
- name: Show lines with errors
debug:
var: error_lines