代码之家  ›  专栏  ›  技术社区  ›  GMichael

is\U文件在Ansible中产生语法错误

  •  0
  • GMichael  · 技术社区  · 8 年前

    我的项目使用Ansible 2.3.2.0。在这个项目中,我有一个任务使用 is_file 测试:

    - name: Create repository.ini file
      template: src="{{ playbook_dir }}/../../../repository.ini" dest="/{{ repository_dir }}/repository.ini"
      when: "{{ playbook_dir }}/../../..//repository.ini"|is_file
    

    此任务产生以下错误:

    The offending line appears to be:
    
      template: src="{{ playbook_dir }}/../../../repository.ini" dest="/{{ jse_repository_dir }}/repository.ini"
      when: "{{ playbook_dir }}/../../../repository.ini"|is_file
                                                         ^ 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:
    . . . . 
    

    我尽了我所能,但没有成功。

    1 回复  |  直到 8 年前
        1
  •  5
  •   techraf    8 年前
    1. 当用引号开始YAML键值时,必须引用整个值,因此如果其中有任何其他字符串,则应使用不同的引号(例如,单引号+双引号)。

    2. Ansible期望 when 是Jinja2语句,因此不需要使用 {{ 在…内

    3. 因为2。需要使用Jinja2语法进行字符串串联 variable + 'string' .

    结合起来,您的条件应为:

    when: "(playbook_dir+'/../../../repository.ini')|is_file"