DevOps / Ansible Interview questions
What is the difference between copy and template modules?
| copy | template |
| Transfers a file as-is, byte for byte. | Renders a Jinja2 template file before transferring it. |
| Good for static files that never change per host. | Good for config files that need per-host or per-environment values inserted. |
| No templating syntax processed in the source file. | Supports variables, conditionals, and loops via Jinja2 syntax. |
- name: Static file, unchanged everywhere ansible.builtin.copy: src: files/motd dest: /etc/motd - name: Config that varies per host ansible.builtin.template: src: templates/app.conf.j2 dest: /etc/app/app.conf
Use copy for content that's identical across every target, like a license file or a static binary. Use template whenever the destination file needs to embed values that differ by host or environment, such as a database hostname, port number, or feature flag, since Jinja2 templating ({{ variable }}, {% if %}, {% for %}) inside a .j2 file is only processed by the template module, not copy.
More Related questions...