首页 > 解决方案 > 如果正在运行则如何检查进程然后退出否则删除并安装包

问题描述

我需要检查少数远程主机上的进程,如果进程正在运行,则退出而不做任何事情,如果进程没有运行,则删除现有包并安装不同的版本。

我已经尝试了下面的 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

标签: linuxshellansibleansible-inventory

解决方案


(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.

笔记
  1. 等效条件是
      when: running_processes.rc == 0
  1. - 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": []
        }
    }

推荐阅读