Skip to content

Instantly share code, notes, and snippets.

@sivel
Last active October 12, 2023 15:09
Show Gist options
  • Save sivel/376abb3ae5476cfe168361d5ae556f61 to your computer and use it in GitHub Desktop.
Save sivel/376abb3ae5476cfe168361d5ae556f61 to your computer and use it in GitHub Desktop.

Multiple registers per task and data manipulation

.. versionadded:: fallible

Note

This feature has not been included in a released version of ansible-core yet, and is a tech preview as part of fallible.

This feature allows a playbook author to register multiple variables on a task, manipulate the data before registered to that variable, and provides implicit register names scoped to a single task. Individual loop results cannot be manipulated, this feature only applies to the final task result.

- command: "echo two"
  register:
    result2: ansible_result
    stdout2: ansible_result.stdout

In the above example result2: ansible_result produces the same result as if using register: result2 has historically provided. The stdout2: ansible_result.stdout performs jinja2 evaluation on the result represented by ansible_result and accesses the stdout attribute or key.

This is roughly equivalent to performing these 2 tasks:

- command: "echo two"
  register: result2

- set_fact:
    stdout2: '{{ result2.stdout }}'

In addition to this functionality, this feature offers 2 implicit register variable names scoped to the task for use in when, changed_when, failed_when, and until.

- command: "echo two"
  changed_when: ansible_result.stdout_lines|first == 'not two'

- command: "echo {{ item }}"
  loop:
    - one
    - two
    - three
  changed_when: ansible_loop_result.stdout_lines|first == 'three'

The ansible_result variable contains the full result of the task, whereas ansible_loop_result is the result of individual loop iterations. Register expressions cannot reference ansible_loop_result.

Note

add_host and group_by do not support register projections when used in a loop. The traditional register: result syntax must be used for these actions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment