首页 > 解决方案 > Ansible 正则表达式 tomcat 版本

问题描述

我已经为一个问题苦苦挣扎了几个小时,我希望在任务文件中只为特定版本的 tomcat 执行操作,例如:

- name: Copy new context.xml(Manager App) for tomcat8 or 9
  template:
    src: templates/tomcat8-9/manager/context.xml
    dest: /usr/share/tomcat/webapps/manager/META-INF/context.xml
    owner: tomcat
    group: tomcat
  when: "{{ tomcat_ver }}" is match("^[8-9]")

如果我的 tomcat_ver 是 9.0.20,那么它应该与模式匹配,但我遇到了一个奇怪的错误,我看不出我在哪里缺少任何报价

 fatal: [tomcatbis]: FAILED! => {"reason": "Syntax Error while loading YAML.\n  did not find expected key\n\nThe error appears to be in '/projects/ansibles/roles/tomcat/tasks/tomcat-setup-Debian.yml': line 69, column 28, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n    group: tomcat\n  when: \"{{ tomcat_ver }}\" is match(\"^[8-9]\")\n                           ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n"}

谢谢你的帮助。

标签: ansible

解决方案


它应该如下所示:

---
- hosts: localhost
  tasks:
    - name: plat
      debug:
        msg: "Success"
      when: '"{{ tomcat_ver }}" is match("^[8-9]")'

开头和结尾的单引号


推荐阅读