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

A system administrator is writing an Ansible playbook to deploy a web application. The application requires a specific configuration file, `/etc/myapp/config.yaml`, to be present on all web servers. The content of this file varies slightly based on the environment (e.g., development, staging, production) but shares a common structure. Which Ansible module is best suited for deploying this configuration file with environment-specific variables?

  1. AThe `copy` module
  2. BThe `synchronize` module
  3. CThe `file` module
  4. DThe `template` module
Show answer & explanation

Correct answer: D. The `template` module

The Ansible `template` module uses Jinja2 templating to generate a file on the target host. This is ideal for configuration files where the content is mostly static but contains variables that need to be dynamically populated based on the environment, host, or other factors defined in Ansible inventory or playbooks.

Why the other options are wrong

  • A. The `copy` module copies files as-is, without variable substitution, making it unsuitable for dynamic content.
  • B. The `synchronize` module is for efficient synchronization of files/directories between hosts, similar to `rsync`, not for templating.
  • C. The `file` module manages file attributes (permissions, ownership) or creates empty files/directories, not file content.

Ansible `template` Module

The Ansible `template` module uses Jinja2 templating to render files on target hosts. It allows dynamic content generation in configuration files based on variables defined in Ansible.

  • Uses Jinja2 syntax for variables, loops, and conditionals.
  • Source template files typically have a `.j2` extension.
  • Ideal for deploying configuration files that require environment-specific values.

Memory trick: Templates transform, copies just conform.

More Automation, Orchestration and Scripting questions