字符串不匹配时的条件 [英] Ansible condition when string not matching
本文介绍了字符串不匹配时的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写Ansible剧本,仅在Nginx不存在且当前版本不存在时才对其进行编译.但是它每次都会编译,这是不希望的.
I am trying to write an Ansible playbook that only compiles Nginx if it's not already present and at the current version. However it compiles every time which is undesirable.
这就是我所拥有的:
- shell: /usr/local/nginx/sbin/nginx -v 2>&1
register: nginxVersion
- debug:
var=nginxVersion
- name: install nginx
shell: /var/local/ansible/nginx/makenginx.sh
when: "not nginxVersion == 'nginx version: nginx/1.8.0'"
become: yes
该脚本的工作原理与每次编译Nginx都运行shell脚本的事实不同. nginxVersion的调试输出为:
The script all works apart from the fact that it runs the shell script every time to compile Nginx. The debug output for nginxVersion is:
ok: [server] => {
"var": {
"nginxVersion": {
"changed": true,
"cmd": "/usr/local/nginx/sbin/nginx -v 2>&1",
"delta": "0:00:00.003752",
"end": "2015-09-25 16:45:26.500409",
"invocation": {
"module_args": "/usr/local/nginx/sbin/nginx -v 2>&1",
"module_name": "shell"
},
"rc": 0,
"start": "2015-09-25 16:45:26.496657",
"stderr": "",
"stdout": "nginx version: nginx/1.8.0",
"stdout_lines": [
"nginx version: nginx/1.8.0"
],
"warnings": []
}
}
}
根据我所掌握的文档,我缺少什么简单的窍门?
According to the documentation I am on the right lines, what simple trick am I missing?
推荐答案
尝试:
when: nginxVersion.stdout != 'nginx version: nginx/1.8.0'
或
when: '"nginx version: nginx/1.8.0" not in nginxVersion.stdout'
这篇关于字符串不匹配时的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文