Interview Prep/Ansible/Variables, Facts and Templates

Different servers need different values but the template has dozens of `if` blocks. How do you simplify?

IntermediateScenarioVariables, Facts and Templates
Answer

Situation read → Template complexity is a smell.

What I'd do →

  1. Push decisions into variables (group_vars), not template branching.
  2. Use a dictionary lookup by OS or role instead of if/elif chains.
  3. Split into several templates if the files genuinely differ.
pkg: "{{ {'Debian':'apache2','RedHat':'httpd'}[ansible_facts['os_family']] }}"

Why → Data-driven config is easier to review and test.

Interview tip → Templates should format data, not decide policy.

All Ansible questions Roadmap: Ansible

More on Variables, Facts and Templates