cancel
Showing results for 
Search instead for 
Did you mean: 
MARAmorim
Mission Specialist
Mission Specialist
  • 219 Views

Can someone explain me when and how to use single. double quotes and unquoted in a playbook?

Hello all, 

this may be a little bit silly but I am still not able to understand when using the double quotes and single quotes and unquoted.

 

For example:

if I have a playbook with a task and refering to a inventory with a group orange and some ungrouped hosts, and I want to make sure that this task is only running on the orange group we could do something like (I know we could just select on hosts filed the pattern, but for the sake of understanding and being able to add more tasks and selectively run them, I use "all"):

...

hosts: all

tasks:

  - name: Retrieve only the hosts that belong to group orange

    ansible.builtin.debug:

       msg: "This is host {{ inventory_host}} and belongs to group ORANGE"

   when: inventory_hostname in groups['orange']

 

This seems to work but if I try it from another perspective using another condition:

when: orange in group_names

This won' t work, but:

when: " 'orange' in group_names"

will do the trick.

What exactly is happening here?

 

Thank you very much!

 

Miguel

 

 

 

1 Reply
Chetan_Tiwary_
Community Manager
Community Manager
  • 215 Views

@MARAmorim for starters refer this thread : https://learn.redhat.com/t5/RH294-Red-Hat-Linux-Automation/Interpretation-of-single-double-quoted-va...

So, this when: inventory_hostname in groups['orange']  works because ' ' is used for mentioning literal value and not variables / expressions.

Now, this  when: " 'orange' in group_names" also works because of the same reason as above-  'orange'  is here literal string which is present in the available group in the inventory.  " " is ensuring that 'orange' is treated as literal string.

This when: orange in group_names does not work because it will be treated as a variable and since it is not defined it wont work.

Chetan_Tiwary__0-1726586632477.png

 

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