Home>

When executing the patch installation, I want to set "OK" if the host has already been installed, "changed" if the installation can be performed on the uninstalled host, and "FAILED" if the installation fails.

fatal: [node01]: FAILED! =>{"changed": false, "cmd": ["sh", "install.sh"], "delta": "0: 00: 01.709752", "end": " 2020-11-24 16: 13: 06.643825 "," msg ":" non-zero return code "," rc ": 1," start ":" 2020-11-24 16: 13: 04.934073 "," stderr " : "", "stderr_lines": [], "stdout": "Check system ...... \ nStarting to install package. Please wait. \ NThe current installation of ServerProtect on your system is splx-3.0-sp1-patch8 -1.0-1575.x86_64. \ nPackage has been installed. Install process exits. "," stdout_lines ": ["Check system ...... "," Starting to install package. Please wait. "," The current installation of ServerProtect on your system is splx-3.0-sp1-patch8-1.0-1575.x86_64. "," Package has been installed. Install process exits. "]}
Corresponding source code
--name: install criticalpatch
  command: sh install.sh
  register: patch_result
  args:
    chdir:/tmp/splx/splx_30_lx_en_criticalpatch1633
  changed_when:'"Package has been installed. Install process exits." not in patch_result.stdout'
Supplementary information

If the patch has already been installed, the following message will be returned when you execute it.

[root @ node01 splx_30_lx_en_criticalpatch1633] # ./install.sh
Check system ......
Starting to install package. Please wait.
The current installation of ServerProtect on your system is splx-3.0-sp1-patch8-1.0-1575.x86_64.
Package has been installed. Install process exits.
[root @ node01 splx_30_lx_en_criticalpatch1633] #
  • Answer # 1

    It looks like you're running a shell, so I'm sure it will be changed or failed.
    To become failed, please judge the condition in install.sh andexit 1If it is (other than 0), it will be treated as an error.
    However, it seems that the status of ok cannot be achieved by the return value of shell execution.

    There is also a method that does not force changed, so please refer to it.
    https://qiita.com/ryurock/items/639e5f7e49d25c3b6d75