字符串不匹配时的 Ansible 条件 [英] Ansible condition when string not matching

查看:26
本文介绍了字符串不匹配时的 Ansible 条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 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'

这篇关于字符串不匹配时的 Ansible 条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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