在我的Ansible剧本下,我要执行脚本的任务:
- 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 - ================================================================================"
]
}
]
}
}
我的目的是掌握此输出msg并搜索任何 error line,诸如此类:
"tERROR - ERROR 1060 (42S21) at line 4 in file: '/opt/myDB_In_progress.sql': Duplicate column name 'con_habilitation_version'",
我可以使用 REGEXP 搜索它,该行寻找以" error "开头的行,并包括" in Line "one_answers"strong>在文件中
最后,我应该得到整个行,结果
我试图使用这样的言论:
- name: set regex
set_fact:
regExpOfSqlError: ' '
become_user: mysql
- set_fact:
errorLine: "{{ updateResult.results | regex_search(regExpOfSqlError, ' ') }}"
become_user: mysql
但我仍然想知道我应该放置什么 regexp ,搜索线条,然后输出它们
建议?
一个简单的解决方案是使用json_query
过滤器。
注意:
-
json_query
取决于jmespath
。在运行示例之前,您将需要pip(3) install jmespath
。 - 在使用Ansible中使用JMespath
contains
函数时,需要to_json | from_json
hack来工作一个已知错误。这需要对Jmespath方面进行修改,仍在等待批准/开发 - 您应该阅读
jmespath
文档以获取更多详细信息,但这是我示例中查询字符串的快速说明:创建所有输入元素的列表投影(updateResults.result
(,然后选择包含文本ERROR
和的stdout_lines
条目中的所有元素最后将结果投影为平坦的列表。
---
- 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
给出:
PLAY [SO Test] *****************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Capture a list of all lines containing an error in all results] **********
ok: [localhost]
TASK [Show lines with errors] **************************************************
ok: [localhost] => {
"error_lines": [
"tERROR - ERROR 1060 (42S21) at line 4 in file: '/opt/myDB_In_progress.sql': Duplicate column name 'con_habilitation_version'"
]
}
PLAY RECAP *********************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0