Ansible Automation Ansible Modules & Task Execution 1 — Questions and Answers
Question 1: Which Ansible module is used to copy a file from the control node to a managed host?
- copy (Correct answer)
- transfer
- fetch
- send
Correct answer: copy
The copy module transfers files from the Ansible control node (or a defined src path) to a destination on the managed host.
Question 2: What does the 'fetch' module do in Ansible?
- Retrieves a file from a managed host to the control node (Correct answer)
- Downloads a file from a URL to the managed host
- Copies a file between two managed hosts
- Fetches variables from the inventory
Correct answer: Retrieves a file from a managed host to the control node
The fetch module is the reverse of copy — it pulls a file from the managed host back to the Ansible control node.
Question 3: Which module would you use to ensure a system package is installed on a host regardless of the package manager?
- package (Correct answer)
- yum
- apt
- dnf
Correct answer: package
The generic 'package' module uses the host's detected package manager automatically, making playbooks more portable across distributions.
Question 4: What is the purpose of the 'template' module?
- Render a Jinja2 template and write the result to a file on the managed host (Correct answer)
- Copy a static file to the managed host
- Generate an inventory from a template
- Create a role scaffold from a template
Correct answer: Render a Jinja2 template and write the result to a file on the managed host
The template module processes a Jinja2 (.j2) file using current variables and writes the rendered output to the specified destination path.
Question 5: Which module is used to run arbitrary shell commands that require shell features like pipes or redirects?
- shell (Correct answer)
- command
- raw
- execute
Correct answer: shell
The shell module runs commands through /bin/sh, enabling shell features like pipes, redirects, and environment variable expansion.
Question 6: What is the key difference between the 'command' and 'shell' modules?
- command does not invoke a shell; shell does, enabling pipes and redirects (Correct answer)
- shell runs locally; command runs remotely
- command uses Python; shell uses bash
- They are identical but shell is deprecated
Correct answer: command does not invoke a shell; shell does, enabling pipes and redirects
The command module executes a program directly without a shell, making it safer and more predictable, while shell passes the command to /bin/sh.
Which Ansible module is used to copy a file from the control node to a managed host?