linux - 如果正在运行则如何检查进程然后退出否则删除并安装包
问题描述
我需要检查少数远程主机上的进程,如果进程正在运行,则退出而不做任何事情,如果进程没有运行,则删除现有包并安装不同的版本。
我已经尝试了下面的 ansible 代码,但是如果进程已经在运行,我不确定如何退出 ansible。有人可以在这里帮忙吗?提前致谢。
- hosts: all
become: yes
tasks:
- name: check if http is running
shell: "pgrep http"
register: running_processes
failed_when: running_processes.rc > 1
- debug:
var: running_processes
- block:
- debug:
msg: http is running. End of play.
- meta: end_host
when: running_processes.stdout_lines|length > 0
- debug:
msg: http is not running. Continue play.
- name: remove the old http package
yum:
name: http
state: absent
- name: install http
yum:
name: http.rpm
state: present
但是我在运行这个剧本时遇到了错误。
fatal: [host1]: FAILED! => {"msg": "The conditional check 'running_processes.stdout_lines|length > 0' failed. The error was: error while evaluating conditional (running_processes.stdout_lines|length > 0): 'dict object' has no attribute 'stdout_lines'\n\nThe error appears to have been in 'http.yml': line 14, column 10, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n - block:\n - debug:\n ^ here\n"}
running_processes 的输出
ok: [host1] => {
"running_processes": {
"changed": true,
"cmd": "pgrep http",
"delta": "0:00:00.025309",
"end": "2019-08-26 14:44:21.019275",
"failed": false,
"failed_when_result": false,
"msg": "non-zero return code",
"rc": 1,
"start": "2019-08-26 14:44:20.993966",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
}
我现在没有收到此错误,但是 - meta: end_host 正在引发错误。
ERROR! invalid meta action requested: end_host
The error appears to have been in 'http.yml': line 20, column 10, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
msg: http is running. End of play.
- meta: end_host
^ here
它在进程运行的地方按预期工作。但它正在跳过进程未运行的地方。
ok: [host1] => {
"running_processes": {
"changed": true,
"cmd": "pgrep http",
"delta": "0:00:00.019929",
"end": "2019-08-26 16:57:59.940856",
"failed": false,
"failed_when_result": false,
"rc": 0,
"start": "2019-08-26 16:57:59.920927",
"stderr": "",
"stderr_lines": [],
"stdout": "8743",
"stdout_lines": [
"8743"
]
}
}
ok: [host2] => {
"running_processes": {
"changed": true,
"cmd": "pgrep http",
"delta": "0:00:00.018915",
"end": "2019-08-26 16:57:59.338179",
"failed": false,
"failed_when_result": false,
"msg": "non-zero return code",
"rc": 1,
"start": "2019-08-26 16:57:59.319264",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
}
TASK [debug] ***********************************************************************************************************************************************************
ok: [host1] => {
"msg": "http is running. End of play."
}
skipping: [host2]
ERROR! invalid meta action requested: end_host
The error appears to have been in 'http.yml': line 19, column 10, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
msg: http is running. End of play.
- meta: end_host
^ here
解决方案
(ansible 2.8.3)
用于pgrep
避免这种情况,例如
- name: check if httpd is running
shell: "pgrep httpd"
register: running_processes
failed_when: running_processes.rc > 1
- block:
- debug:
msg: httpd is running. End of play.
- meta: end_play
when: running_processes.stdout_lines|length > 0
- debug:
msg: httpd is not running. Continue play.
笔记
- 等效条件是
when: running_processes.rc == 0
- meta: end_play
终止所有主机。仅用于- meta: end_host
结束当前主机。
以下是running_processes
没有匹配时的示例
ok: [localhost] => {
"running_processes": {
"changed": true,
"cmd": "pgrep httpd",
"delta": "0:00:00.013312",
"end": "2019-08-26 20:40:59.908060",
"failed": false,
"failed_when_result": false,
"msg": "non-zero return code",
"rc": 1,
"start": "2019-08-26 20:40:59.894748",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
}
推荐阅读
- javascript - 观察者从某个页面点击后退按钮(Reactjs)
- java - Java Springboot @Autowired Mybatis Mapper 为空
- sql - 在 PL/SQL 中使用布尔值添加新列
- python - 如何从numpy数组中找到正确的坐标
- firebase - 我将如何获取 Api 密钥和 API 密钥以在 Google Cloud Firebase 中启用使用 Twitter 进行身份验证
- django - Django 模型对象在更新后更改
- android - 为什么将页脚 LoadStateAdapter 与 RecyclerView 和 Paging 3 一起使用会使列表在错误的布局位置打开?
- python - dockerizing时的Django应用程序给出“在http://0.0.0.0:8000/启动开发服务器”但没有出现在浏览器上
- javascript - dom-to-image:在移动设备上捕获桌面组件
- python - 如果 __name__ != "__main__" 怎么执行?