【问题标题】:Ansible: skip the step based on a variable valueAnsible:根据变量值跳过步骤
【发布时间】:2017-07-05 06:37:45
【问题描述】:

如果在变量 yml 文件中将 Ansible 变量设置为 https: off,则应跳过以下代码。

default_setup.yml:

 # Other info
 ansible_cache_dir: /var/cache/ansible
 https: off

provision.yml:

 ---
 - hosts: webclient
   vars_files:
   - default_setup.yml

 - name: Update server.xml tomcat https conf
   template:
       src: conf_files/tomcat_server.xml
       dest: /opt/tomcat/apache-tomcat-{{ tomcat_ver }}/conf/server.xml
   become: true
   when: "{{ https }}" == "on"
   notify: restart tomcat

当我玩这个 Ansible provision.yml 时出现以下错误,即使我添加了引号(“”)

  ERROR! Syntax Error while loading YAML.


    The error appears to have been in '/home/centos/check-tomcat/roles/nginx/tasks/main.yml': line 24, column 23, but maybe elsewhere in the file depending on the exact syntax problem.

 The offending line appears to be:

  become: true
  when: "{{ https }}" == "on"
                  ^ 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 }}"

【问题讨论】:

    标签: ansible yaml ansible-2.x


    【解决方案1】:

    两件事:

    • YAML 中的off 是一个布尔值(falseno 的同义词),因此如果您保留https 的定义不变,使用以下条件就足够了:

      when: not https
      
    • 如果您将https 更改为字符串,即:

      https: "off"
      

      那么条件可能是:

      when: https == "off"
      

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 2015-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多