Task names are optional, but extremely useful. In its output, Ansible shows you the name of each task it runs. Choosing names that describe what each task does and why improves readability. It also enables the usage of --start-at-task
.
command
instead of shell
ANS-E3005Use shell only when shell functionality is required. Shell should only be used when piping, redirecting or chaining commands (and Ansible would be preferred for some of those!)
command
instead of arguments to modules ANS-E3002Executing a command when there are arguments to modules is not recommended. Using command
module is a bad idea, since it’s not idempotent in nature, the developer is responsible for handling the idempotency of the task.
copy
and template
do not need to use relative path for src
. This removes the need for knowing the location of the root directory.
If a task has a when: result.changed
setting, it is effectively acting as a handler. Sometimes you want a task to run only when a change is made on a machine. For example, you may want to restart a service if a task updates the configuration of that service, but not if the configuration is unchanged. Ansible uses handlers to address this use case. Handlers are tasks that only run when notified. Each handler should have a globally unique name.