cancel
Showing results for 
Search instead for 
Did you mean: 
  • 3,405 Views

Interpretation of single- double quoted value

Jump to solution

Hi ,

I started my RH294 training, and I am a little bit confused about the interpretation of single- double quoted value in Ansible.

When should I use : 

  • "{{ my_var }}"
  • {{ my_var }}
  • '{{my_var }}'

What is the difference between :

  • hosts: 'datacenter*'
  • hosts: "datacenter*"

How ansible interprets each case.

Thank you so much.

2 Solutions

Accepted Solutions
Chetan_Tiwary_
Community Manager
Community Manager
  • 3,375 Views

Hello @Hamdi_Gabsi !

Thanks for reaching out!

Refer it here : https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html#yaml-syntax 

Chetan_Tiwary__0-1692025099958.png

https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html 

  1. "{{ my_var }}" --> case of variable interpolation or Dynamic Injection. Use it when you want to use the actual value of the variable during execution in a string or a command.
  2.  '{{ my_var }}' --> case of literal string or Static Injection. No variable interpolation ( means no variable substitution ) is done here. Use it when you want the origial placeholder text rather than it's value. 
  3. {{ my_var }} --> same as the first one but generally used for larger strings.

**************************************************************

Regarding second part : 

hosts: 'datacenter*' --> * is interpreted as literal * --> host pattern matching for any host that starts with datacenter.


hosts: "datacenter*" --> wildcard expansion is same as the previous one with ' ' --> which will match the host pattern that starts with the word "datacenter".

 

View solution in original post

Travis
Moderator
Moderator
  • 1,100 Views

@MARAmorim -

One more thing to understand here is that you can also do it without any quotes. It will use the * as a wildcard to get anything and pattern match on groups or single hostname. The cool thing about the RHLS and self-paced lab environment is that as @Chetan_Tiwary_ demoed above, you can change up the playbooks a little and experiment and actually see what happens.

The other thing to keep in mind, Ansible is YAML files and YAML is nothing but lists of lists and dictionaries (KEY: VALUE) pairs. What we teach you with the "best practices" for playbook writing is to not have things in "folded" form but to use the expanded form in a playbook. The HOSTS piece of the playbook is the only time we make an exception as we generally see that in folded form on the line instead of the list format with the "-".

Travis Michette, RHCA XIII
https://rhtapps.redhat.com/verify?certId=111-134-086
SENIOR TECHNICAL INSTRUCTOR / CERTIFIED INSTRUCTOR AND EXAMINER
Red Hat Certification + Training

View solution in original post

7 Replies
Chetan_Tiwary_
Community Manager
Community Manager
  • 3,376 Views

Hello @Hamdi_Gabsi !

Thanks for reaching out!

Refer it here : https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html#yaml-syntax 

Chetan_Tiwary__0-1692025099958.png

https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html 

  1. "{{ my_var }}" --> case of variable interpolation or Dynamic Injection. Use it when you want to use the actual value of the variable during execution in a string or a command.
  2.  '{{ my_var }}' --> case of literal string or Static Injection. No variable interpolation ( means no variable substitution ) is done here. Use it when you want the origial placeholder text rather than it's value. 
  3. {{ my_var }} --> same as the first one but generally used for larger strings.

**************************************************************

Regarding second part : 

hosts: 'datacenter*' --> * is interpreted as literal * --> host pattern matching for any host that starts with datacenter.


hosts: "datacenter*" --> wildcard expansion is same as the previous one with ' ' --> which will match the host pattern that starts with the word "datacenter".

 

MARAmorim
Mission Specialist
Mission Specialist
  • 1,153 Views

Hello Chetan,

 

sorry for picking up this old post, but I was not sure about the last part. Perhaps you can explain me better?

"

Regarding second part : 

hosts: 'datacenter*' --> * is interpreted as literal * --> host pattern matching for any host that starts with datacenter.


hosts: "datacenter*" --> wildcard expansion is same as the previous one with ' ' --> which will match the host pattern that starts with the word "datacenter".

 

If I understand correctly if wildcard is double quotes then it can be interpreted by the shell and expanded to all strings in the execution environment that start with datacenter, imagining you don t have any, then only datacenter string will be passed to ansible

 

While in single quote the asterisk is preserverd in ansible and used for host pattern matching...

 

IS this correct?

 

Thank you very much

 

Miguel

Chetan_Tiwary_
Community Manager
Community Manager
  • 1,102 Views

@MARAmorim I have corrected the 2nd part, it seems both the patterns work in similar way in host pattern matching , I verified this in ch06s02 RH294 course :

Chetan_Tiwary__0-1724705606486.png

Chetan_Tiwary__1-1724705622929.png

Thanks for your understanding.

Travis
Moderator
Moderator
  • 1,101 Views

@MARAmorim -

One more thing to understand here is that you can also do it without any quotes. It will use the * as a wildcard to get anything and pattern match on groups or single hostname. The cool thing about the RHLS and self-paced lab environment is that as @Chetan_Tiwary_ demoed above, you can change up the playbooks a little and experiment and actually see what happens.

The other thing to keep in mind, Ansible is YAML files and YAML is nothing but lists of lists and dictionaries (KEY: VALUE) pairs. What we teach you with the "best practices" for playbook writing is to not have things in "folded" form but to use the expanded form in a playbook. The HOSTS piece of the playbook is the only time we make an exception as we generally see that in folded form on the line instead of the list format with the "-".

Travis Michette, RHCA XIII
https://rhtapps.redhat.com/verify?certId=111-134-086
SENIOR TECHNICAL INSTRUCTOR / CERTIFIED INSTRUCTOR AND EXAMINER
Red Hat Certification + Training
MARAmorim
Mission Specialist
Mission Specialist
  • 1,035 Views

@Travis  and @Chetan_Tiwary_ 

Thank you guys!!

 

Yeah I wasn t finding any difference between single, double and unquoted host patterns...

 

 

Cheers

 

Miguel

0 Kudos
  • 3,369 Views

Thank you so much  @Chetan_Tiwary_  for these details.

I think it is clear now.

I appreciate your response.

Chetan_Tiwary_
Community Manager
Community Manager
  • 3,358 Views

Glad it helped @Hamdi_Gabsi ! All the best !

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