ansible / proposals

Repository for sharing and tracking progress on enhancement proposals for Ansible.
Creative Commons Zero v1.0 Universal
92 stars 19 forks source link

Reversible include_tasks #201

Open TimEisler opened 2 years ago

TimEisler commented 2 years ago

Proposal: Reversible include_tasks

Author: Tim Eisler <@TimEisler>

Date: 2021-11-06

Motivation

To allow a task file to be shared by a block and a rescue but to run the tasks in reverse order in rescue to achieve an undo.

Problems

The number of YAML files to be maintained can be reduced.

Solution proposal

- hosts: all
  gather_facts: yes
  vars:
    pip_pkgname: 'bottle'
    path: '/usr/local/{{ pip_pkgname }}'
    marker_file_name: '{{ pip_pkgname }}_installed.txt'

  tasks:
    - block:
      - name: Block. Reversal is not expected
        include_tasks:
          file: ./shared_task_file.yml
      rescue:
      - name: Rescue. Reversal is expected
        include_tasks:
          file: ./shared_task_file.yml
          reverse: true
          apply:
            vars:
              - state: absent

Dependencies (optional)

Testing (optional)

Unit tests should be written to verify the reverse option is accepted. The existing integration tests for the includes target should be extended to check for reversed task order when reverse: True is used.

Documentation (optional)

Documentation for include_tasks should be revised to list rescue and provide examples; namely, the DOCUMENTATION and EXAMPLES docstrings in lib/ansible/modules/include_tasks.py

Anything else?

I have already implemented this feature in https://github.com/TimEisler/ansible/tree/feature/IncludeTaskReverse

I have documented this in a Gist at https://gist.github.com/TimEisler/c4c5e49d7c05a60c92527a60866c3abd

I have posted a notice about this to the ansible-devel google group on Nov 7, 2021.

Files created or modified by my feature per git diff --name-only devel feature/IncludeTaskReverse are:

lib/ansible/modules/include_tasks.py
lib/ansible/playbook/task_include.py
lib/ansible/plugins/callback/default.py
lib/ansible/plugins/strategy/__init__.py
test/integration/targets/includes/aliases
test/integration/targets/includes/always_includable_tasks.yml
test/integration/targets/includes/block_rescue_includable_tasks.yml
test/integration/targets/includes/include_task_reverse.yml
test/integration/targets/includes/runme.sh
test/integration/targets/includes/setup.yml
test/integration/targets/includes/test_include_task_reverse.py
test/units/playbook/test_helpers.py
test/units/playbook/test_included_file.py