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

时间:2016-05-10 20:04:17

标签: 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" )

这不起作用,并出现错误:

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 }}"

让我知道我错在哪里。

1 个答案:

答案 0 :(得分:5)

我认为您不必使用"{{ bld_env }}" == "rc"

只需将变量与值bld_env == "rc"进行比较,依此类推,因为它写在documentation