mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-06-29 11:40:22 -07:00
[PR #10087/b861850e backport][stable-10] Feat: Add callback plugin print_task (#10159)
Some checks are pending
EOL CI / EOL Sanity (Ⓐ2.15) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.15+py2.7) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.15+py3.10) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.15+py3.5) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.15+alpine3+py:azp/posix/1/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.15+alpine3+py:azp/posix/2/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.15+alpine3+py:azp/posix/3/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.15+fedora37+py:azp/posix/1/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.15+fedora37+py:azp/posix/2/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.15+fedora37+py:azp/posix/3/) (push) Waiting to run
nox / Run extra sanity tests (push) Waiting to run
Some checks are pending
EOL CI / EOL Sanity (Ⓐ2.15) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.15+py2.7) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.15+py3.10) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.15+py3.5) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.15+alpine3+py:azp/posix/1/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.15+alpine3+py:azp/posix/2/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.15+alpine3+py:azp/posix/3/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.15+fedora37+py:azp/posix/1/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.15+fedora37+py:azp/posix/2/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.15+fedora37+py:azp/posix/3/) (push) Waiting to run
nox / Run extra sanity tests (push) Waiting to run
Feat: Add callback plugin print_task (#10087)
* Feat: Add callback plugin print_task
Prints task snippet to job output.
* Fix for failing tests
* Fix some pep8 formatting issues
* Updating DOCUMENTATION variable with version_added
* Set correct CALLBACK_NAME and fix warning with gather_facts
* Fix formatting again
* Update plugins/callback/print_task.py
* Update plugins/callback/print_task.py
* Add entry to BOTMETA.yml
* Use CSafeLoader and fallback to SafeLoader
* Change output function to self._display.display()
* Adding tests for community.general.print_task
* Adding EXAMPLES
* Apply suggestions from code review
---------
(cherry picked from commit b861850e1a
)
Co-authored-by: Max Mitschke <demonpig@fastmail.com>
Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
edb0d5f6ca
commit
f7189a55c6
4 changed files with 204 additions and 0 deletions
2
.github/BOTMETA.yml
vendored
2
.github/BOTMETA.yml
vendored
|
@ -77,6 +77,8 @@ files:
|
||||||
$callbacks/opentelemetry.py:
|
$callbacks/opentelemetry.py:
|
||||||
keywords: opentelemetry observability
|
keywords: opentelemetry observability
|
||||||
maintainers: v1v
|
maintainers: v1v
|
||||||
|
$callbacks/print_task.py:
|
||||||
|
maintainers: demonpig
|
||||||
$callbacks/say.py:
|
$callbacks/say.py:
|
||||||
keywords: brew cask darwin homebrew macosx macports osx
|
keywords: brew cask darwin homebrew macosx macports osx
|
||||||
labels: macos say
|
labels: macos say
|
||||||
|
|
64
plugins/callback/print_task.py
Normal file
64
plugins/callback/print_task.py
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Copyright (c) 2025, Max Mitschke <maxmitschke@fastmail.com>
|
||||||
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
from __future__ import (absolute_import, division, print_function)
|
||||||
|
__metaclass__ = type
|
||||||
|
|
||||||
|
DOCUMENTATION = r'''
|
||||||
|
name: print_task
|
||||||
|
type: aggregate
|
||||||
|
short_description: Prints playbook task snippet to job output
|
||||||
|
description:
|
||||||
|
- This plugin prints the currently executing playbook task to the job output.
|
||||||
|
version_added: 10.7.0
|
||||||
|
requirements:
|
||||||
|
- enable in configuration
|
||||||
|
'''
|
||||||
|
|
||||||
|
EXAMPLES = r'''
|
||||||
|
ansible.cfg: >
|
||||||
|
# Enable plugin
|
||||||
|
[defaults]
|
||||||
|
callbacks_enabled=community.general.print_task
|
||||||
|
'''
|
||||||
|
|
||||||
|
from yaml import load, dump
|
||||||
|
|
||||||
|
try:
|
||||||
|
from yaml import CSafeDumper as SafeDumper
|
||||||
|
from yaml import CSafeLoader as SafeLoader
|
||||||
|
except ImportError:
|
||||||
|
from yaml import SafeDumper, SafeLoader
|
||||||
|
|
||||||
|
from ansible.plugins.callback import CallbackBase
|
||||||
|
|
||||||
|
|
||||||
|
class CallbackModule(CallbackBase):
|
||||||
|
"""
|
||||||
|
This callback module tells you how long your plays ran for.
|
||||||
|
"""
|
||||||
|
CALLBACK_VERSION = 2.0
|
||||||
|
CALLBACK_TYPE = 'aggregate'
|
||||||
|
CALLBACK_NAME = 'community.general.print_task'
|
||||||
|
|
||||||
|
CALLBACK_NEEDS_ENABLED = True
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super(CallbackModule, self).__init__()
|
||||||
|
self._printed_message = False
|
||||||
|
|
||||||
|
def _print_task(self, task):
|
||||||
|
if hasattr(task, '_ds'):
|
||||||
|
task_snippet = load(str([task._ds.copy()]), Loader=SafeLoader)
|
||||||
|
task_yaml = dump(task_snippet, sort_keys=False, Dumper=SafeDumper)
|
||||||
|
self._display.display(f"\n{task_yaml}\n")
|
||||||
|
self._printed_message = True
|
||||||
|
|
||||||
|
def v2_playbook_on_task_start(self, task, is_conditional):
|
||||||
|
self._printed_message = False
|
||||||
|
|
||||||
|
def v2_runner_on_start(self, host, task):
|
||||||
|
if not self._printed_message:
|
||||||
|
self._print_task(task)
|
6
tests/integration/targets/callback_print_task/aliases
Normal file
6
tests/integration/targets/callback_print_task/aliases
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
# Copyright (c) Ansible Project
|
||||||
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
azp/posix/3
|
||||||
|
needs/target/callback
|
132
tests/integration/targets/callback_print_task/tasks/main.yml
Normal file
132
tests/integration/targets/callback_print_task/tasks/main.yml
Normal file
|
@ -0,0 +1,132 @@
|
||||||
|
---
|
||||||
|
####################################################################
|
||||||
|
# WARNING: These are designed specifically for Ansible tests #
|
||||||
|
# and should not be used as examples of how to write Ansible roles #
|
||||||
|
####################################################################
|
||||||
|
|
||||||
|
# Copyright (c) Ansible Project
|
||||||
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
|
include_role:
|
||||||
|
name: callback
|
||||||
|
vars:
|
||||||
|
tests:
|
||||||
|
- name: community.general.print_task is not enabled
|
||||||
|
environment:
|
||||||
|
ANSIBLE_NOCOLOR: 'true'
|
||||||
|
ANSIBLE_FORCE_COLOR: 'false'
|
||||||
|
playbook: |
|
||||||
|
- hosts: testhost
|
||||||
|
gather_facts: false
|
||||||
|
tasks:
|
||||||
|
- name: Sample task
|
||||||
|
debug:
|
||||||
|
msg: This is a test
|
||||||
|
expected_output: [
|
||||||
|
"",
|
||||||
|
"PLAY [testhost] ****************************************************************",
|
||||||
|
"",
|
||||||
|
"TASK [Sample task] *************************************************************",
|
||||||
|
"ok: [testhost] => {",
|
||||||
|
" \"msg\": \"This is a test\"",
|
||||||
|
"}",
|
||||||
|
"",
|
||||||
|
"PLAY RECAP *********************************************************************",
|
||||||
|
"testhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 "
|
||||||
|
]
|
||||||
|
|
||||||
|
- name: community.general.print_task is enabled
|
||||||
|
environment:
|
||||||
|
ANSIBLE_NOCOLOR: 'true'
|
||||||
|
ANSIBLE_FORCE_COLOR: 'false'
|
||||||
|
ANSIBLE_CALLBACKS_ENABLED: 'community.general.print_task'
|
||||||
|
playbook: |
|
||||||
|
- hosts: testhost
|
||||||
|
gather_facts: false
|
||||||
|
tasks:
|
||||||
|
- name: Sample task
|
||||||
|
debug:
|
||||||
|
msg: This is a test
|
||||||
|
expected_output: [
|
||||||
|
"",
|
||||||
|
"PLAY [testhost] ****************************************************************",
|
||||||
|
"",
|
||||||
|
"TASK [Sample task] *************************************************************",
|
||||||
|
"",
|
||||||
|
"- name: Sample task",
|
||||||
|
" debug:",
|
||||||
|
" msg: This is a test",
|
||||||
|
"",
|
||||||
|
"ok: [testhost] => {",
|
||||||
|
" \"msg\": \"This is a test\"",
|
||||||
|
"}",
|
||||||
|
"",
|
||||||
|
"PLAY RECAP *********************************************************************",
|
||||||
|
"testhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 "
|
||||||
|
]
|
||||||
|
|
||||||
|
- name: Print with msg parameter on the same line
|
||||||
|
environment:
|
||||||
|
ANSIBLE_NOCOLOR: 'true'
|
||||||
|
ANSIBLE_FORCE_COLOR: 'false'
|
||||||
|
ANSIBLE_CALLBACKS_ENABLED: 'community.general.print_task'
|
||||||
|
playbook: |
|
||||||
|
- hosts: testhost
|
||||||
|
gather_facts: false
|
||||||
|
tasks:
|
||||||
|
- name: Sample task
|
||||||
|
debug: msg="This is a test"
|
||||||
|
expected_output: [
|
||||||
|
"",
|
||||||
|
"PLAY [testhost] ****************************************************************",
|
||||||
|
"",
|
||||||
|
"TASK [Sample task] *************************************************************",
|
||||||
|
"",
|
||||||
|
"- name: Sample task",
|
||||||
|
" debug: msg=\"This is a test\"",
|
||||||
|
"",
|
||||||
|
"ok: [testhost] => {",
|
||||||
|
" \"msg\": \"This is a test\"",
|
||||||
|
"}",
|
||||||
|
"",
|
||||||
|
"PLAY RECAP *********************************************************************",
|
||||||
|
"testhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 "
|
||||||
|
]
|
||||||
|
|
||||||
|
- name: Task with additional parameters
|
||||||
|
environment:
|
||||||
|
ANSIBLE_NOCOLOR: 'true'
|
||||||
|
ANSIBLE_FORCE_COLOR: 'false'
|
||||||
|
ANSIBLE_CALLBACKS_ENABLED: 'community.general.print_task'
|
||||||
|
playbook: |
|
||||||
|
- hosts: testhost
|
||||||
|
gather_facts: false
|
||||||
|
tasks:
|
||||||
|
- name: Sample task
|
||||||
|
when: True
|
||||||
|
vars:
|
||||||
|
test_var: "Hello World"
|
||||||
|
debug:
|
||||||
|
var: test_var
|
||||||
|
expected_output: [
|
||||||
|
"",
|
||||||
|
"PLAY [testhost] ****************************************************************",
|
||||||
|
"",
|
||||||
|
"TASK [Sample task] *************************************************************",
|
||||||
|
"",
|
||||||
|
"- name: Sample task",
|
||||||
|
" when: true",
|
||||||
|
" vars:",
|
||||||
|
" test_var: Hello World",
|
||||||
|
" debug:",
|
||||||
|
" var: test_var",
|
||||||
|
"",
|
||||||
|
"ok: [testhost] => {",
|
||||||
|
" \"test_var\": \"Hello World\"",
|
||||||
|
"}",
|
||||||
|
"",
|
||||||
|
"PLAY RECAP *********************************************************************",
|
||||||
|
"testhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 "
|
||||||
|
]
|
Loading…
Add table
Add a link
Reference in a new issue