有条件地运行ansible角色处理程序

有条件地运行ansible角色处理程序,ansible,Ansible,我有一个任务可以触发一个角色处理程序,这个处理程序也可以正常工作。现在,我试图确保处理程序仅在满足条件时运行。在我的例子中,它是一个通过vars_输入读取的变量值。然而,我得到了一个例外。有没有办法解决这个问题 任务示例(在role/tasks/main.yml文件中): - name: test conditional handler command: /bin/true notify: - test handler --- - name: test handler uri

我有一个任务可以触发一个角色处理程序,这个处理程序也可以正常工作。现在,我试图确保处理程序仅在满足条件时运行。在我的例子中,它是一个通过vars_输入读取的变量值。然而,我得到了一个例外。有没有办法解决这个问题

任务示例(在role/tasks/main.yml文件中):

- name: test conditional handler
  command: /bin/true
  notify:
   - test handler
---
- name: test handler
  uri: 
    url: http://validurl
    method: GET
  when: {{ test_var }} != "some value"
Points to the "!=".  
We could be wrong, but this one looks like it might be an issue with missing quotes. 
处理程序(在role/handlers/main.yml文件中):

- name: test conditional handler
  command: /bin/true
  notify:
   - test handler
---
- name: test handler
  uri: 
    url: http://validurl
    method: GET
  when: {{ test_var }} != "some value"
Points to the "!=".  
We could be wrong, but this one looks like it might be an issue with missing quotes. 
我可以访问任务中的test_var值

例外情况:

- name: test conditional handler
  command: /bin/true
  notify:
   - test handler
---
- name: test handler
  uri: 
    url: http://validurl
    method: GET
  when: {{ test_var }} != "some value"
Points to the "!=".  
We could be wrong, but this one looks like it might be an issue with missing quotes. 

您不需要在when语句中使用大括号表示法(例如,
{{test\u var}}
),因为该值被解释为Jinja2表达式。以下工作将起作用:

when: test_var != "some value"

您不需要在when语句中使用大括号表示法(例如,
{{test\u var}}
),因为该值被解释为Jinja2表达式。以下工作将起作用:

when: test_var != "some value"