我尝试通过Ansible启动JBoss服务,并使用wai_for模块等待JBoss启动。但是,JBoss服务已启动,但wait_for仍在运行,直到出现超时然后命中错误。下面是我的代码
- name: Get the contents of the last line from log
command: "tail -n 1 /home/nityo/application.log"
register: tail_output
- name: Create a variable with a meaningful name, just for clarity
set_fact:
last_line_of_the_log_file: "{{ tail_output.stdout }}"
- name: JBoss service starting
service:
name: "jboss.service"
state: started
become: yes
become_user: root
- name:Wait for server started
wait_for:
path: "//home/nityo/application.log"
search_regex: "{{ last_line_of_the_log_file }}\r(.*\r)*.*JBoss EAP.*started.*"
timeout: 600除此之外,我们是否可以将所有这些合并为一个任务,而不是拥有多个任务
输出日志示例
2020-10-11 01:13:42,009 INFO [org.jboss.as] JBoss EAP 7.2 (WildFly Core) running in 100281ms - service to be running
2020-10-11 01:13:42,005 INFO [org.jboss.as] processing data
2020-10-11 01:13:43,009 INFO [org.jboss.as] JBoss EAP 7.2 (WildFly Core) stopped in 100281ms - service to be stopped
-
-
-
-
-
-
-
2020-10-11 01:13:48,009 INFO [org.jboss.as] JBoss EAP 7.2 (WildFly Core) started in 100281ms - service to be started发布于 2020-10-11 20:00:31
它永远不会在最后一个任务中匹配,因为您正在搜索最后一个日志文件行加上一些额外的内容。您在regex中使用了一个变量,该变量包含一个字符串,如果您在其末尾添加一些其他人员,则该字符串永远不会匹配。
因此,跳过第一个和第二个任务,并更改最后一个任务的search_regex参数。
您可以使用可分析的事实ansible_date_time (在Ansible date variable中描述),并使用日期、时间和/或纪元秒的一部分来通过set_fact模块构建您的正则表达式。因此,您的正则表达式将匹配时间戳不超过10分钟的日志文件行。
使用date,您可以为特定的小时创建时间戳。攻略可能如下所示:
---
- name: check logfile last 10 Minutes
hosts:
- localhost
gather_facts: no
tasks:
- name: set now timestring
command: 'date +"%Y-%m-%d %H"'
register: date_now
- name: set_now
set_fact:
now: "{{ date_now.stdout }}"
- name: set 10 minutes ago timestring
command: 'date +"%Y-%m-%d %H" -d "10 minutes ago"'
register: date_10min_ago
- name: set 10min_ago
set_fact:
ten_min_ago: "{{ date_10min_ago.stdout }}"
- name: debug
debug:
msg: "({{ now }}|{{ ten_min_ago }}).*JBoss EAP.*started.*"
- name: Wait for server started
wait_for:
path: "//home/nityo/application.log"
search_regex: "({{ now }}|{{ ten_min_ago }}).*JBoss EAP.*started"
timeout: 600如果达到daliy限制,则时间戳完全不同。在中间的一个小时,它似乎是多余的。Debug-输出如下所示的regex:
TASK [debug] **********************
ok: [localhost] => {
"msg": "(2020-10-12 08|2020-10-12 08).*JBoss EAP.*started.*"
}发布于 2020-10-11 21:46:23
您存储在last_line_of_the_log_file变量中的内容可能不会在wait_for模块到达此任务时进行检查的默认间隔内进行匹配。
通过解析日志文件来检查服务状态可能很棘手。但是,我认为最干净的方法是匹配日志文件中的模式JBoss EAP.*started.*。但是,为了使其可靠地工作,最好在启动服务之前从一个空的日志文件开始。
# Backup the log file with date/time stamp
- shell: cat /home/nityo/application.log >> /home/nityo/application-$(date %b-%d-%H-%M).log
# Empty the file contents before starting service
- command: truncate -s 0 /home/nityo/application.log
- wait_for:
path: "/home/nityo/application.log"
search_regex: "JBoss EAP.*started.*"
timeout: 600如果日志在给定的时间跨度内以不同的方式生成,那么解析日志文件可能会出错。您可以调整时间、重试次数和搜索模式,以获得更一致的结果。
更新:
最好等待一段时间,以超过tail命令获取的默认行(10),以避免与以前的启动匹配。然后使用egrep
# Pause and wait for logs to roll beyond 10 lines fetched by 'tail'
- pause:
seconds: 30
- shell: tail /home/nityo/application.log | grep -e "JBoss EAP.*started"
register: file_tail
until: file_tail is success
# use appropriate values as per the rate of logging
retries: 30
delay: 20https://stackoverflow.com/questions/64302459
复制相似问题