Task

May 8, 2026 ยท View on GitHub

Tasks are divided into single-layer and multi-layer types:

  • Single-layer task: Contains module-related fields, does not contain block. A task can only use one module.
  • Multi-layer task: Does not contain module fields, contains block (with optional rescue, always).

Tasks execute on each host of the play separately (unless run_once: true).

File Definition

- include_tasks: other/task.yaml
  tags: ["always"]
  when: true
  run_once: false
  ignore_errors: false
  vars: { a: b }

- name: Block Name
  tags: ["always"]
  when: true
  run_once: false
  ignore_errors: false
  vars: { a: b }
  block:
    - name: Task Name
      [module]
  rescue:
    - name: Task Name
      [module]
  always:
    - name: Task Name
      [module]

- name: Task Name
  tags: ["always"]
  when: true
  loop: [""]
  [module]
FieldDescription
include_tasksReference other task files.
nameTask name, optional.
tagsTags, optional. Only applies to this task, does not inherit play/role tags.
whenExecution condition, optional. Can be a string or array, using template syntax, evaluated separately for each host.
failed_whenFailure condition, optional. Considered failed when met, supports template syntax.
run_onceWhether to execute only once, optional, default false. Executes on the first host.
ignore_errorsWhether to ignore failures, optional, default false.
varsVariables for this task, optional, YAML format.
loopExecute module in a loop, passing current value as item each iteration. Can be a string or array, using template syntax.
retriesNumber of retries on failure, optional.
registerWrite execution result to variable for subsequent tasks. Contains sub-fields like stderr, stdout.
register_typeParse format for register: string (default), json, yaml.
blockTask list. Required when no module is defined, executes in normal flow.
rescueTask list. Executes when any sibling task in block fails.
alwaysTask list. Executes after block (and rescue if present) regardless of success or failure.
moduleSpecific operation, corresponding to registered modules. Required when not using block.

Registered Modules

ModuleDescription
add_hostvarsInject variables into specified hosts
assertConditional assertion
commandExecute commands
copyCopy files/directories to target hosts
debugPrint variables
fetchFetch files from remote hosts to local
gen_certValidate or generate certificates
imagePull/push/copy images
include_varsLoad variables from YAML files
prometheusQuery Prometheus metrics
resultWrite to playbook status detail
set_factSet variables on the current host
setupGather host information
templateRender templates and copy to target hosts