Skip to content

Conversation

@rpluem-vf
Copy link
Contributor

Enrich the data for config reports with unified diffs in case the Ansible tasks provides diff data. This is part of a series of PR's to enable the Ansible diff mode on Foreman.

@evgeni
Copy link
Member

evgeni commented Jan 20, 2025

Why would you alter the data here instead of passing before/after verbatim to Foreman and let the UI render it in whichever way it likes?

@rpluem-vf
Copy link
Contributor Author

Why would you alter the data here instead of passing before/after verbatim to Foreman and let the UI render it in whichever way it likes?

This saves space. If you have a 100 KB file and change only one line you have to save 200 KB in the report (100 KB for the file before the change, 100 KB for the file after the change). If you have this for thousands of hosts and a number of larger configuration files this sums up.

@codecov-commenter
Copy link

codecov-commenter commented Jan 20, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 22.22222% with 7 lines in your changes missing coverage. Please review.

Please upload report for BASE (develop@5f5ba20). Learn more about missing BASE report.

Files with missing lines Patch % Lines
plugins/callback/foreman.py 22.22% 7 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             develop    #1816   +/-   ##
==========================================
  Coverage           ?   35.36%           
==========================================
  Files              ?       94           
  Lines              ?     4604           
  Branches           ?     1134           
==========================================
  Hits               ?     1628           
  Misses             ?     2973           
  Partials           ?        3           
Flag Coverage Δ
sanity 35.36% <22.22%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@rpluem-vf
Copy link
Contributor Author

Why would you alter the data here instead of passing before/after verbatim to Foreman and let the UI render it in whichever way it likes?

This saves space. If you have a 100 KB file and change only one line you have to save 200 KB in the report (100 KB for the file before the change, 100 KB for the file after the change). If you have this for thousands of hosts and a number of larger configuration files this sums up.

UI wise this uses what is used for the Puppet data to render which only delivers the unified diffs. Plus difflib is part of core Python whereas in Ruby a 3rd party GEM (https://rubygems.org/gems/diff-lcs/) would be required to generate the needed diff. But you are correct that providing the unified diff leaves less render options for the UI. I guess it is a kind of preference which way to go. Just let men know.

@evgeni
Copy link
Member

evgeni commented Jan 20, 2025

The Ansible callback here is just "the messenger", we (well, you ;) ) can adapt to whatever foreman/foreman_ansible (I don't recall which one carries the report parsing these days) expects. If it's easier (and cheaper) to follow the Puppet path, so be it.

BTW, I always thought that Ansible already provided a unified diff in the diff attribute. But my memory might be missguided (I know we only do before/after in our own modules, and the normal Ansible cli shows a unified diff fine, but I have no idea who actually generates it)

@rpluem-vf
Copy link
Contributor Author

The Ansible callback here is just "the messenger", we (well, you ;) ) can adapt to whatever foreman/foreman_ansible (I don't recall which one carries the report parsing these days) expects. If it's easier (and cheaper) to follow the Puppet path, so be it.

BTW, I always thought that Ansible already provided a unified diff in the diff attribute. But my memory might be missguided (I know we only do before/after in our own modules, and the normal Ansible cli shows a unified diff fine, but I have no idea who actually generates it)

I was surprised as well. I will have a look at my code again because there seems to be a function already for callbacks: and it is used here and here.

@rpluem-vf
Copy link
Contributor Author

I have two further PR's that are unrelated to this new feature, but fix issues that cause tests to fail:

#1819
#1820

@evgeni Can you have a look?

Once they are in I would rebase this PR. Furthermore I rewrote the code to use the function I mentioned above to generate the diff instead of my own. It is more generic and covers cases that I did not consider so far.

@rpluem-vf
Copy link
Contributor Author

@evgeni
Copy link
Member

evgeni commented Jan 29, 2025

I have merged the test cleanup PRs, thanks for that!

The diff here looks quite reasonable, but I guess we should first get movement on the receiving end (foreman_ansible) for proceeding further?

@rpluem-vf
Copy link
Contributor Author

I have merged the test cleanup PRs, thanks for that!

As promised I just pushed a rebased commit with tests that makes use of the existing function

The diff here looks quite reasonable, but I guess we should first get movement on the receiving end (foreman_ansible) for proceeding further?

I think there is no hard dependency. If the updated callback runs against an older Foreman there are just no diffs in the data coming from Ansible. And even if there are as I added a new key report_diff the existing reporting UI would just ignore it.

@rpluem-vf
Copy link
Contributor Author

I see that there are failures currently. I will take care of them and then you can have a look. Stay tuned.

@rpluem-vf rpluem-vf force-pushed the diff_mode branch 5 times, most recently from 2cd1b97 to c6d428d Compare January 29, 2025 11:54
@rpluem-vf
Copy link
Contributor Author

@evgeni : Now all is green :-)

@rpluem-vf
Copy link
Contributor Author

Any update?

@evgeni
Copy link
Member

evgeni commented Mar 13, 2025

I think the diff looks fine, but I'd prefer theforeman/smart_proxy_ansible#98 and theforeman/foreman_ansible#749 being merged first.

@rpluem-vf
Copy link
Contributor Author

I think the diff looks fine, but I'd prefer theforeman/smart_proxy_ansible#98 and theforeman/foreman_ansible#749 being merged first.

Understood. Any idea how we can push these to get merged?

@rpluem-vf
Copy link
Contributor Author

The last push was just a rebase that solved a conflict.

In case that a 'diff' key with 'before' and 'after' data is available
create a unified diff from 'before' and 'after' and store it below the
'report_diff' key as a string for later consumption in config reports
on the Foreman server.
Remove the 'diff' key afterwards to save space as in case of a file the
content is probably big and it is stored twice (before and after).

* plugins/callback/foreman.py:
  Implement feature.

* tests/callback/three_hosts.yml:
  Add tasks to generate diffs when running in diff mode.

* tests/conftest.py:
  Allow to run the playbook in diff mode.

* tests/test_callback.py:
  - Run the playbook for the foreman report type in diff mode
  - Ignore further items when comparing the json data that depend on the
    local environment
  - Remove additional strings that are different in each run
  - Transform the json string into json data to improve data handling

* tests/fixtures/callback/dir_store/*:
  Adjust fixtures to new tests and changes in format.

Signed-off-by: Ruediger Pluem <[email protected]>
@rpluem-vf
Copy link
Contributor Author

rpluem-vf commented Dec 17, 2025

The last push was just a rebase that solved a conflict in tests/test_callback.py.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants