Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to register output of a task and make it available in templates context #1449

Open
iilyak opened this issue Dec 29, 2023 · 1 comment
Labels
state: needs triage Waiting to be triaged by a maintainer.

Comments

@iilyak
Copy link

iilyak commented Dec 29, 2023

rash-sh allows passing arbitrary values from one task to another one via register (see here)

tasks:
  sources:
    cmds:
      - sh: find . -type f -name '*.c'
  mytask:
    cmds:
      - task: sources
        register: sources_result
      - echo discovered files {{ .sources.output }}
      - echo stderr {{ .sources.extra.stderr }}
      - echo return code {{ .sources.extra.return_code }}

Starting from the above we can go further and allow multiple named returns

  sources:
    cmds:
      - sh: find . -type f -name '*.c'
         register: c_sources
      - sh: find . -type f -name '*.cpp'
         register: cpp_sources
      - sh: find . -type f -name '*.h'
         register: h_sources
    register:
         sources: # the result would be flattened (concatenation of lists) 
            - c_sources
            - cpp_sources
         headers: h_sources
      
  mytask:
    cmds:
      - task: sources
        register:
           inputs: sources
           headers: headers
           rc: _rc # assume we always register return code in `_rc` output
           sterr: _stderr # assume we always register return code in `_stderr` output
      - echo discovered files {{ .inputs }}
      - echo header files {{ .headers }}
      - echo stderr {{ .stderr }}
      - echo return code {{ .rc }}

This design has sufficient level of indirection to avoid any name collisions. Also it doesn't pollute the global namespace.

@task-bot task-bot added the state: needs triage Waiting to be triaged by a maintainer. label Dec 29, 2023
@thetanil
Copy link

thetanil commented Jan 1, 2024

register is also supported in Ansible. you use it not just for templates context, but for conditionals in the task itself using when: and probably others.

example from ansible docs

- hosts: web_servers

  tasks:

     - name: Run a shell command and register its output as a variable
       ansible.builtin.shell: /usr/bin/foo
       register: foo_result
       ignore_errors: true

     - name: Run a shell command using output of the previous task
       ansible.builtin.shell: /usr/bin/bar
       when: foo_result.rc == 5

The struct for vars created with register are kind of complex in a way which I'm not a huge fan of, but exit status (registered_var.rc) and stdout separate from stderr is very useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: needs triage Waiting to be triaged by a maintainer.
Projects
None yet
Development

No branches or pull requests

3 participants