首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ansible警告模板变量是'when‘条件中字符串的一部分

Ansible警告模板变量是'when‘条件中字符串的一部分
EN

Stack Overflow用户
提问于 2021-06-20 11:58:04
回答 1查看 42关注 0票数 0

我有以下Ansible任务,它被设计为当且仅当"nginx -v“的输出与预期不匹配时才运行nginx编译脚本。

代码语言:javascript
复制
- name: get nginx version
  command: "{{ nginx_binary }} -v"
  register: result
  ignore_errors: True

- name: download and compile nginx
  include: install.yml
  when: result.rc != 0 or result.stderr != "nginx version{{':'}} nginx/{{nginx_version}}"

当我用最新版本的Ansible运行它时,我得到:

代码语言:javascript
复制
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. 
Found: result.rc != 0 or result.stderr != "nginx version{{':'}}
nginx/{{nginx_version}}"

我对如何在没有模板分隔符的情况下编写这条语句感到困惑。如果我删除冒号字符周围的模板变量,我会得到:

代码语言:javascript
复制
- name: download and compile nginx
  include: install.yml
  when: result.rc != 0 or result.stderr != "nginx version: nginx/{{nginx_version}}"

我得到了:

代码语言:javascript
复制
Syntax Error while loading YAML.
  mapping values are not allowed in this context

The error appears to be in '/Users/kevin/src/github.com/kevinburke/web-deployment/roles/nginx/tasks/main.yml': line 13, column 58, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  include: install.yml
  when: result.rc != 0 or result.stderr != "nginx version: nginx/{{nginx_version}}"
                                                         ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

我也试过了,但还是收到了警告:

代码语言:javascript
复制
- name: download and compile nginx
  include: install.yml
  when: result.rc != 0 or result.stderr != "nginx version: nginx/" + nginx_version

有什么建议吗?似乎如果他们对此发出警告,应该有一种方法来编写它来删除警告,但我到目前为止还没有找到。

EN

回答 1

Stack Overflow用户

发布于 2021-06-20 17:59:31

将字符串放入变量中,例如

代码语言:javascript
复制
    - name: get nginx version
      command: nginx -v
      register: result
      ignore_errors: True

    - debug:
        var: result.stderr

    - name: 'download and compile nginx {{ nginx_version }}'
      debug:
        msg: "include: install.yml"
      when: result.stderr != _nginx_version
      vars:
        nginx_version: '1.18.0'
        _nginx_version: 'nginx version: nginx/{{ nginx_version }}'

    - name: 'download and compile nginx {{ nginx_version }}'
      debug:
        msg: "include: install.yml"
      when: result.stderr != _nginx_version
      vars:
        nginx_version: '1.18.1'
        _nginx_version: 'nginx version: nginx/{{ nginx_version }}'

给出

代码语言:javascript
复制
TASK [debug] ***************************************************************
ok: [srv] => 
  result.stderr: 'nginx version: nginx/1.18.0'

TASK [download and compile nginx 1.18.0] ***********************************
skipping: [srv]

TASK [download and compile nginx 1.18.1] ***********************************
ok: [srv] => 
  msg: 'include: install.yml'
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68052542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档