基于ansible stdout值的回音信息。

问题描述 投票:0回答:1

团队:

不知道为什么我的带有echo的信息没有被打印出来。我想打印一个文本,如果之前运行的停止进程的命令是成功的。

在我的例子中,我正在检查apache2进程的状态,基本上,如果stdout是空的,则通过,如果stderr存在,则失败。基本上,如果stdout是空的就通过,如果stderr存在就失败。我也参考了这个链接,但是没有结果。https://stackoverflow.com/questions/26142343/ansible-conditional-based-on-stdout-of-result

- name: stop apache2 process
  command: systemctl stop apache2
  ignore_errors: no
  changed_when: false
  register: service_apache2_status
  become: true

- debug:
    var: service_apache2_status.stdout
- name: Check if apache2 stopped
  command: echo "Successfully Stopped Apache2"
#  when: service_apache2_status.stdout == " "
  when: service_apache2_status.stdout | length == 0

实际输出。

TASK [diskcache_prerequisites : stop apache2 process] **********************************************************************************
ok: [localhost]

TASK [diskcache_prerequisites : debug] *************************************************************************************************
ok: [localhost] => {
   "service_apache2_status.stdout": ""
}

TASK [diskcache_prerequisites : Check if apache2 stopped] ******************************************************************************
changed: [localhost]

预期的输出。

TASK [diskcache_prerequisites : Check if apache2 stopped] ******************************************************************************
changed: [localhost]
Successfully Stopped Apache2
ansible ansible-2.x ansible-facts
1个回答
0
投票

如果你使用的是 systemd 模块 而不是试图使用 command: 呼叫 systemctl 徒手

至于你的问题:如果你想 看到 输出,这就是 - debug: msg="Successfully stopped Apache2" 是为了;的 command: 模块不是一个通用的调试工具,它是用来运行 命令

© www.soinside.com 2019 - 2024. All rights reserved.