Saturday, 6 December 2025

Ansible 11. Variables - different ways to refer a variable.

 All three ways you wrote are valid in Ansible, but there are subtle differences in syntax and formatting:


1️⃣ Single-line without quotes

msg: Successfully connected to {{ db_name_to_check }} on {{ db_hostname }}
  • Works fine for simple single-line messages.

  • No quotes needed if there are no special characters.

  • Best for short messages.


2️⃣ Single-line with quotes

msg: "Successfully connected to {{ db_name_to_check }} on {{ db_hostname }}"
  • Also works the same as above.

  • Quotes allow using special characters (like :) or spaces at the start/end safely.

  • Good practice if your message might include special YAML characters.


3️⃣ Multi-line / folded style (>)

msg: > Successfully connected to {{ db_name_to_check }} on {{ db_hostname }}.
  • > allows multi-line YAML strings.

  • Line breaks in YAML become spaces in the final string.

  • Useful if your message spans multiple lines.

Example:

msg: > Successfully connected to {{ db_name_to_check }} on {{ db_hostname }}. Database size: {{ postgres_status.databases[db_name_to_check].size }}
  • This produces a single line in output:
    "Successfully connected to postgres on oel02db.mydb.com. Database size: 12345"


Summary / Recommendation

StyleUse case
No quotesSimple, short messages
QuotesMessages with special characters or punctuation
> foldedLong messages, multiple lines, better readability

No comments:

Post a Comment

Building a Safer PostgreSQL CI/CD Pipeline with GitHub Actions: Dev → PR Review → Test Promotion

In my previous post, we explored a simple push-to-main deployment strategy . While functional, that model is not considered an industry best...