Ansible:如何测试注册的变量不为空? [英] Ansible: How to test that a registered variable is not empty?
本文介绍了Ansible:如何测试注册的变量不为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何测试 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 问题
这篇关于Ansible:如何测试注册的变量不为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文