how to use variable in "when" statement in ansible?

I am trying to use a variable in when statement in ansible , my code snippet looks like this :

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

This is not working and gives an a error as :

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

Let me know where I am wrong.

Upvotes: 3

Views: 18832

Answers (1)

Rafał Malinowski
Rafał Malinowski

Reputation: 1166

I think you don't have to use "{{ bld_env }}" == "rc".

Just compare variable to value bld_env == "rc" and so on as it is written in documentation

Upvotes: 10

Related Questions