CompTIA Linux+ (XK0-006)Automation, Orchestration and ScriptingEasy

A system administrator is developing an Ansible playbook for deploying a web application. The application requires a specific configuration file, `app.conf`, which needs to be generated dynamically based on host-specific variables (e.g., `server_port`, `database_url`). Which Ansible module is best suited for creating this configuration file from a template?

  1. A`ansible.builtin.file`
  2. B`ansible.builtin.template`
  3. C`ansible.builtin.lineinfile`
  4. D`ansible.builtin.copy`
Show answer & explanation

Correct answer: B. `ansible.builtin.template`

The `ansible.builtin.template` module is specifically designed for generating files from Jinja2 templates, allowing dynamic content based on Ansible variables. This perfectly matches the requirement for a configuration file generated from host-specific variables.

Why the other options are wrong

  • A. The `file` module is for managing file attributes (permissions, ownership) or creating/deleting empty files/directories, not for generating content.
  • C. The `lineinfile` module is used for modifying specific lines within an existing file, not for generating an entire file from a template.
  • D. The `copy` module transfers files as-is; it does not process templates or substitute variables.

Ansible `template` Module

An Ansible module that generates files from Jinja2 templates, enabling dynamic content based on Ansible variables, facts, and loops.

  • Uses Jinja2 templating engine.
  • Source template file is specified by `src` parameter.
  • Destination file on the target host is specified by `dest` parameter.
  • Idempotent: only updates the file if the rendered content changes.

Memory trick: Ansible has a 'template' for every file, a 'copy' for static, and 'lineinfile' for tweaks.

More Automation, Orchestration and Scripting questions