如何测试一个注册变量不为空? [英] How to test that a registered variable is not empty?

查看:102
本文介绍了如何测试一个注册变量不为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何测试stderr不为空::

How can I test that stderr is non empty::

- name: Check script
  shell: . {{ venv_name }}/bin/activate && myscritp.py
  args:
    chdir: "{{ home }}"
  sudo_user: "{{ user }}"
  register: test_myscript

- debug: msg='myscritp is Ok'
  when: not test_myscript.stderr

因此,如果没有错误,我可以读::

So if there is no error I could read::

  TASK: [deploy | debug msg='critp is Ok] *******
  ok: [vagrant] => {
      "msg": "myscritp is Ok"
  }

如果stderr不为空,则会出现FATAl错误.

In case the stderr is not empty a FATAl error occurs.

推荐答案

您可以检查空字符串(当stderr为空时)

You can check for empty string (when stderr is empty)

- name: Check script
  shell: . {{ venv_name }}/bin/activate && myscritp.py
  args:
    chdir: "{{ home }}"
  sudo_user: "{{ user }}"
  register: test_myscript

- debug: msg='myscritp is Ok'
  when: test_myscript.stderr == ""

如果要检查是否失败:

- debug: msg='myscritp has error: {{test_myscript.stderr}}'
  when: test_myscript.stderr != ""

还要查看此 stackoverflow问题

这篇关于如何测试一个注册变量不为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆