ansible-community/community-topics

Add a mean to quit a subtask without ending a role

Popolon opened this issue · 1 comments

Summary

In several case, I need to loop on some dictionaries, and made various things by loop depending on this dictionaries. A subtask is then called to manage this, but there is currently no mean to simply end this subtask as a return 0 would be made in any language. This imply to duplicate iteration, by make first a loop to gather the informations on hosts, then depending on the results make a second loop. a - meta: end_subtask, -meta: return_successful or something like that would greatly simplify the work. The goal is to have finer granularity than end_host on roles.

Additional Information

As a simple example among several cases I encounter, I've several version of php to build on a host in //. we will take here php-8.1 and php-8.2. defined in a dictionary in the hosts/myhost.yml variables. On the host, I would like to compile the last 8.1.x and php-8.2.x available, if they are not already installed. Again this is an example, not depending on available package in the distribution, etc... used in several cases.

I iterate on this dictionary, in main.yml the following way (I simplified the example, generally more parameters are given as compilation/configuration options):

- name: Use role in loop when pear is defined
  include_tasks: php.yml
  vars:
    phpversion: "{{ outer_item.value.phpversion }}"
  with_dict: "{{ phpver }}"
  loop_control:
    loop_var: outer_item
  when: phpver is defined

Then in the php.yml, I check if get the last available version of the branch in a role specific dictionary, and then test, if this specfic X.Y.Z version of php is installed (I need several X.Y version in parallel). I would like as there are -meta: end_batch, end_play or end_host, to stop everything, something, that like in most language, just return from subtask with ok, and continue to next iteration, on the same host:

- name: test presence of already existing destination directory
  ansible.builtin.stat:
    path: "/path/php-{{ php_v }}"
  register: php_dir

- block:
  - name: "end php.yml because {{ php_dir }} already exists"
    debug:
      msg: "{{ php_v }} release is already installed, remove it if you want to to install it, jump to next version"
  - meta: end_subtask
  when: php_dir.stat.exists

Hi, this issue tracker is not meant for questions. Please use the Ansible forum for that: https://forum.ansible.com/ Thanks.