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

is there away to control ansible stdout output? #1389

Open
braindevices opened this issue Aug 14, 2024 · 1 comment
Open

is there away to control ansible stdout output? #1389

braindevices opened this issue Aug 14, 2024 · 1 comment
Labels
needs_triage New item that needs to be triaged

Comments

@braindevices
Copy link

we normally use the yaml or the diy callback for stdout, this make the large failure message far more readable on the fly.
The json mode put the output the dict on single line, which is hard to read when the dict contains multiline strings

for example:

fatal: [localhost]: FAILED! => {"changed": false, "cmd": "set -e\nset -x\necho hello\nexit 1\n", "delta": "0:00:00.002386", "end": "2024-08-14 22:34:33.969828", "msg": "non-zero return code", "rc": 1, "start": "2024-08-14 22:34:33.967442", "stderr": "+ echo hello\n+ exit 1", "stderr_lines": ["+ echo hello", "+ exit 1"], "stdout": "hello", "stdout_lines": ["hello"]}

however, according to this #212, it prevent us to change the stdout_callback

I wonder is there a way to tune the stdout in the way we wanted with ansible-runner?

I try to use event_handler but it won't sync with the playbook screen output

def handle_event(event):
    if event['event'] == "runner_on_failed" and 'event_data' in event:
        event_data = event['event_data']
        keys = ["task_path", 'res']
        data = {k:event_data[k] for k in keys if k in event_data}
        if data:
            print(yaml.safe_dump(data, default_flow_style=False))

The print out can happen any time not synced with the normal output

@github-actions github-actions bot added the needs_triage New item that needs to be triaged label Aug 14, 2024
@braindevices
Copy link
Author

braindevices commented Aug 14, 2024

I found a very ugly way to achieve this but I do not know if this is stable or just a side effect:

def my_stdout(stdout: str, res: dict):
    remove_large_res = re.compile(r"( => )\{.*\}")
    neg_keys = [
        'stdout_lines', 'stderr_lines',
        'start', 'end', 'delta',
        'invocation',
        # '_ansible_no_log',
        # "_ansible_item_label"
    ]
    data = {k:v for k, v in res.items() if k not in neg_keys and not k.startswith('_')}
    _dump = r'\1\n' + yaml.safe_dump(data)
    return remove_large_res.sub(_dump, stdout)


def handle_event(event):
    # Format event as YAML
    if event['event'].endswith("_failed") and 'event_data' in event:
        event_data = event['event_data']
        host = event_data["host"]
        # remove stderrlines and stderrouts
        if 'res' in event_data:
            keys_to_remove = ['stdout_lines', 'stderr_lines']
            for k in keys_to_remove:
                if k in event_data['res']:
                    event_data['res'].pop(k)
            
            event['stdout'] =my_stdout(event['stdout'], event_data['res'])
    return True

this depends on the fact that handle_event() can actually modify the event, is this a stable feature?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs_triage New item that needs to be triaged
Projects
None yet
Development

No branches or pull requests

1 participant