Skip to content

Feat: Add callback plugin print_task #10087

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

Merged

Conversation

demonpig
Copy link
Contributor

@demonpig demonpig commented May 1, 2025

Prints task snippet to job output.

SUMMARY

I figured this might be useful for debugging purposes when executing playbooks. All this callback plugin is doing, is printing out the task snippet as it would show within the playbook. See example below.

Playbook:

---

- name: Test
  hosts: all
  gather_facts: false

  tasks:
    - name: TEST | HEllo
      ansible.builtin.shell: echo {{ item }}
      when: false
      async: 10
      poll: 1
      loop:
        - 1
        - 2
        - 3
    # - name: Command 2
      # async: 1
      # ansible.builtin.shell: |
      #   echo hello
      #   exit 1

    - ansible.builtin.shell: echo hello

    - ansible.builtin.ping:

Command:

ansible-playbook -i inventory playbook.yml -v

Output:

PLAY [Test] ********************************************************************

TASK [TEST | HEllo] ************************************************************

- name: TEST | HEllo
  ansible.builtin.shell: echo {{ item }}
  when: false
  async: 10
  poll: 1
  loop:
  - 1
  - 2
  - 3

skipping: [localhost] => (item=1)  => {"ansible_loop_var": "item", "changed": false, "false_condition": false, "item": 1, "skip_reason": "Conditional result was False"}
skipping: [localhost] => (item=2)  => {"ansible_loop_var": "item", "changed": false, "false_condition": false, "item": 2, "skip_reason": "Conditional result was False"}
skipping: [localhost] => (item=3)  => {"ansible_loop_var": "item", "changed": false, "false_condition": false, "item": 3, "skip_reason": "Conditional result was False"}
skipping: [localhost] => {"changed": false, "msg": "All items skipped"}

TASK [ansible.builtin.shell] ***************************************************

- ansible.builtin.shell: echo hello

changed: [localhost] => {"changed": true, "cmd": "echo hello", "delta": "0:00:00.005068", "end": "2025-04-30 23:14:05.140808", "msg": "", "rc": 0, "start": "2025-04-30 23:14:05.135740", "stderr": "", "stderr_lines": [], "stdout": "hello", "stdout_lines": ["hello"]}

TASK [ansible.builtin.ping] ****************************************************

- ansible.builtin.ping: None

ok: [localhost] => {"changed": false, "ping": "pong"}

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME
  • community.general.print_task
ADDITIONAL INFORMATION

@ansibullbot ansibullbot added callback callback plugin feature This issue/PR relates to a feature request new_contributor Help guide this first time contributor plugins plugin (any type) labels May 1, 2025
@ansibullbot

This comment was marked as outdated.

@ansibullbot ansibullbot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR label May 1, 2025
@felixfontein felixfontein added check-before-release PR will be looked at again shortly before release and merged if possible. backport-10 Automatically create a backport for the stable-10 branch labels May 2, 2025
@ansibullbot

This comment was marked as outdated.

@demonpig
Copy link
Contributor Author

demonpig commented May 2, 2025

I'm not entirely sure how to work around the three tests that are failing. When I checked the outputs, they test playbooks are failing with the following. This is something that this callback plugin uses.

07:15 [ERROR]: Task failed: Module failed: Cannot detect the required Python library cryptography (>= 3.4)
07:15 Origin: /root/ansible_collections/community/general/tests/output/.tmp/integration/java_cert-di_su1xq-ÅÑŚÌβŁÈ/tests/integration/targets/java_cert/tasks/state_change.yml:10:3
07:15 
07:15  8 #
07:15  9
07:15 10 - name: Create private keys
07:15      ^ column 3
07:15 
07:15 failed: [testhost] (item=/tmp/ansible.b0rnqgr_.test/key.pem) => {
07:15     "ansible_loop_var": "item",
07:15     "changed": false,
07:15     "invocation": {
07:15         "module_args": {
07:15             "attributes": null,
07:15             "backup": false,
07:15             "cipher": "auto",
07:15             "curve": null,
07:15             "force": false,
07:15             "format": "auto_ignore",
07:15             "format_mismatch": "regenerate",
07:15             "group": null,
07:15             "mode": "u=rw,go=",
07:15             "owner": null,
07:15             "passphrase": null,
07:15     },
07:15     "item": "/tmp/ansible.b0rnqgr_.test/key.pem",
07:15     "msg": "Cannot detect the required Python library cryptography (>= 3.4)"
07:15 }

@felixfontein
Copy link
Collaborator

The Debian Bullseye tests failures are unrelated to this PR, please simply ignore them for now.

@ansibullbot ansibullbot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR label May 2, 2025
@ansibullbot

This comment was marked as outdated.

@ansibullbot ansibullbot added ci_verified Push fixes to PR branch to re-run CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR and removed ci_verified Push fixes to PR branch to re-run CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels May 3, 2025
Copy link
Collaborator

@felixfontein felixfontein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution! If you haven't done so, please take a look at https://github.com/ansible-collections/community.general/blob/main/CONTRIBUTING.md#creating-new-modules-or-plugins.

@demonpig
Copy link
Contributor Author

demonpig commented May 5, 2025

I'll work on some tests later tonight.

'''


import yaml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would advise against using 'plain' yaml, i would try using the CSafeLoader first (speed of C lib), fallback to SafeLoader (slower python) and avoid other loaders. See core's use of yaml lib when in doubt.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just updated the code to do this.

@ansibullbot ansibullbot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR integration tests/integration tests tests labels May 6, 2025
@demonpig demonpig force-pushed the feature-callback_print_task branch from ffccfb3 to e615072 Compare May 9, 2025 02:13
@ansibullbot ansibullbot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR label May 9, 2025
@russoz
Copy link
Collaborator

russoz commented May 10, 2025

Hi @demonpig pardon me for being a bit of a contrarian here, but how is this better than ansible-playbook -vvv? Or -vvvv? It already shows the module arguments. Personally, in my plabooks, I would not want to be changing the callback configs for a debug, just increasing the verbosity would do the trick for me. YMMV

Copy link
Collaborator

@felixfontein felixfontein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also use the C dumper here if available (and use the safe dumper instead of the default one).

Also, what do you think about @russoz comment?

@felixfontein
Copy link
Collaborator

Hi @demonpig pardon me for being a bit of a contrarian here, but how is this better than ansible-playbook -vvv? Or -vvvv?

It shows the arguments after being passed through the module, but you don't really see the raw task information (which also happens to be untemplated).

@demonpig
Copy link
Contributor Author

@russoz I understand your points. I view this as an addition to -vvvv where you can get a print out of the task and the arguments passed to it. Part of the reason behind this was a need to debug playbooks within AWX / Ansible Automation Platform without having to look at the source code.

@felixfontein
Copy link
Collaborator

If nobody objects, I'll merge this for 10.7.0.

@felixfontein felixfontein removed the check-before-release PR will be looked at again shortly before release and merged if possible. label May 18, 2025
@felixfontein felixfontein merged commit b861850 into ansible-collections:main May 18, 2025
135 checks passed
Copy link

patchback bot commented May 18, 2025

Backport to stable-10: 💚 backport PR created

✅ Backport PR branch: patchback/backports/stable-10/b861850e1a9495a1227a15b894b531409b4384e5/pr-10087

Backported as #10159

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

patchback bot pushed a commit that referenced this pull request May 18, 2025
* 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

Co-authored-by: Felix Fontein <[email protected]>

* Update plugins/callback/print_task.py

Co-authored-by: Felix Fontein <[email protected]>

* 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

Co-authored-by: Felix Fontein <[email protected]>

---------

Co-authored-by: Felix Fontein <[email protected]>
(cherry picked from commit b861850)
@felixfontein
Copy link
Collaborator

@demonpig thanks for your contribution!
@Akasurde @bcoca @russoz thanks for reviewing and commenting!

felixfontein added a commit that referenced this pull request May 19, 2025
…int_task (#10159)

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 b861850)

Co-authored-by: Max Mitschke <[email protected]>
Co-authored-by: Felix Fontein <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-10 Automatically create a backport for the stable-10 branch callback callback plugin feature This issue/PR relates to a feature request integration tests/integration new_contributor Help guide this first time contributor plugins plugin (any type) tests tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants