如何在ansible中的"when"语句中使用变量?

Gri*_*cat 0 ansible ansible-playbook

我试图在ansible中的when语句中使用变量,我的代码片段如下所示:

- name: git repo for non prod env
  git:
    repo=http://url/repo.git
    dest=/opt/dest
    version={{ bld_env }}
  when: ( "{{ bld_env }}" == "rc" ) or ( "{{ bld_env }}" == "sandbox" ) or ( "{{ bld_env }}" == "dev" ) or ( "{{ bld_env }}" == "qa" )
Run Code Online (Sandbox Code Playgroud)

这不起作用,并给出一个错误:

The offending line appears to be:

    version={{ bld_env }}
  when: "{{ bld_env }}" == "rc"
                        ^ 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 }}"
Run Code Online (Sandbox Code Playgroud)

让我知道我错在哪里.

Raf*_*ski 5

我想你不必使用"{{ bld_env }}" == "rc".

只需将变量与值进行比较bld_env == "rc",就像在文档编写一样