我有以下Ansible任务,它被设计为当且仅当"nginx -v“的输出与预期不匹配时才运行nginx编译脚本。
- 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运行它时,我得到:
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.
Found: result.rc != 0 or result.stderr != "nginx version{{':'}}
nginx/{{nginx_version}}"我对如何在没有模板分隔符的情况下编写这条语句感到困惑。如果我删除冒号字符周围的模板变量,我会得到:
- name: download and compile nginx
include: install.yml
when: result.rc != 0 or result.stderr != "nginx version: nginx/{{nginx_version}}"我得到了:
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 }}"我也试过了,但还是收到了警告:
- name: download and compile nginx
include: install.yml
when: result.rc != 0 or result.stderr != "nginx version: nginx/" + nginx_version有什么建议吗?似乎如果他们对此发出警告,应该有一种方法来编写它来删除警告,但我到目前为止还没有找到。
发布于 2021-06-20 17:59:31
将字符串放入变量中,例如
- 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 }}'给出
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'https://stackoverflow.com/questions/68052542
复制相似问题