Showing results for 
Search instead for 
Did you mean: 
Flight Engineer
Flight Engineer

failed_when question

- block:
        - name: Install {{ web_package }} package
            name: "{{ web_package }}"
            state: present
          failed_when: web_package == "httpd"

 In this case, why there's no quotation on web_package for failed_when?

I thought as this is the variable and first element, quotations should've been used.

Labels (1)
0 Kudos
1 Reply

@spurs Hello

In Ansible, when you're working with conditional statements like when, you might wonder if you need to put quotes around variables. Here's a simplified explanation:

YAML Basics: YAML, the format Ansible playbooks are written in, is pretty flexible. It can understand what you mean most of the time, whether you use quotes or not.
Variables in Ansible: When you use variables (like {{ my_variable }}) in conditions, Ansible is smart enough to know what you're talking about without needing quotes. It looks at what's inside {{ }} and figures it out.
Why Sometimes Quotes Are Used: Quotes can be helpful if you're dealing with spaces or special characters that might confuse YAML or if you want to make sure YAML understands exactly what you mean. For example, when you have a whole sentence or a complex condition, putting it in quotes can avoid misunderstandings.
Simple Rule: If your condition is straightforward and just involves checking a variable (like when: my_variable is defined), you usually don't need quotes. But if your condition gets more complicated or includes special characters, adding quotes (when: "{{ my_variable }}" == "some value") can be a good idea to keep things clear.

So, in easy terms: Quotes aren't always necessary, but they can be useful for clarity and to make sure your playbook works the way you expect, especially in more complex scenarios.

Join the discussion
You must log in to join this conversation.