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

vyos_config: print compare command result in diff mode #42494

Merged
merged 1 commit into from
Jul 19, 2018

Conversation

hitsumabushi
Copy link
Contributor

SUMMARY
  • print diff with vyos_config
    • as diff, use output of compare command
  • example
    TASK [vyos : config example]  
    *******************************************************************************************************************************************************************
    [edit firewall name to_gslb_filter]
    +rule 7 {
    +    action drop
    +    destination {
    +        address 192.168.10.1
    +        port 23
    +    }
    +    protocol tcp
    +}
    
ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

vyos_config module

ANSIBLE VERSION

I checked current devel branch. : https://github.com/ansible/ansible/tree/d7c3d5501bdc7482a330fb7b29ad8dd7ed3683b6

$ ansible --version
ansible 2.7.0.dev0
  config file = /home/hitsu/work/src/github.sakura.codes/iot-pf/router-conf-ansible/ansible.cfg
  configured module search path = ['/home/hitsu/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']                                                                                                  
  ansible python module location = /home/hitsu/.pyenv/versions/3.6.2/envs/3.6_global/lib/python3.6/site-packages/ansible                                                                                          
  executable location = /home/hitsu/.pyenv/versions/global/bin/ansible
  python version = 3.6.2 (default, Nov 23 2017, 16:58:38) [GCC 7.2.0]

@ansibot
Copy link
Contributor

ansibot commented Jul 9, 2018

@ansibot ansibot added affects_2.7 This issue/PR affects Ansible v2.7 core_review In order to be merged, this PR must follow the core review workflow. feature This issue/PR relates to a feature request. module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. networking Network category new_contributor This PR is the first contribution by a new community member. python3 small_patch support:network This issue/PR relates to code supported by the Ansible Network Team. labels Jul 9, 2018
@@ -223,7 +223,7 @@ def run(module, result):
result['changed'] = True

if module._diff:
result['diff'] = diff
result['diff'] = {'prepared': diff}
Copy link
Member

Choose a reason for hiding this comment

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

Is there a specific reason for moving generated diff under prepared key?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ganeshrn Thank you for comment.

Just for printing diff.
I think printing diff need (before, after) or prepared keys.

def _get_diff(self, difflist):
if not isinstance(difflist, list):
difflist = [difflist]
ret = []
for diff in difflist:
try:
with warnings.catch_warnings():
warnings.simplefilter('ignore')
if 'dst_binary' in diff:
ret.append("diff skipped: destination file appears to be binary\n")
if 'src_binary' in diff:
ret.append("diff skipped: source file appears to be binary\n")
if 'dst_larger' in diff:
ret.append("diff skipped: destination file size is greater than %d\n" % diff['dst_larger'])
if 'src_larger' in diff:
ret.append("diff skipped: source file size is greater than %d\n" % diff['src_larger'])
if 'before' in diff and 'after' in diff:
# format complex structures into 'files'
for x in ['before', 'after']:
if isinstance(diff[x], MutableMapping):
diff[x] = json.dumps(diff[x], sort_keys=True, indent=4, separators=(',', ': ')) + '\n'
if 'before_header' in diff:
before_header = "before: %s" % diff['before_header']
else:
before_header = 'before'
if 'after_header' in diff:
after_header = "after: %s" % diff['after_header']
else:
after_header = 'after'
before_lines = to_text(diff['before']).splitlines(True)
after_lines = to_text(diff['after']).splitlines(True)
if before_lines and not before_lines[-1].endswith('\n'):
before_lines[-1] += '\n\\ No newline at end of file\n'
if after_lines and not after_lines[-1].endswith('\n'):
after_lines[-1] += '\n\\ No newline at end of file\n'
differ = difflib.unified_diff(before_lines,
after_lines,
fromfile=before_header,
tofile=after_header,
fromfiledate='',
tofiledate='',
n=C.DIFF_CONTEXT)
difflines = list(differ)
if len(difflines) >= 3 and sys.version_info[:2] == (2, 6):
# difflib in Python 2.6 adds trailing spaces after
# filenames in the -- before/++ after headers.
difflines[0] = difflines[0].replace(' \n', '\n')
difflines[1] = difflines[1].replace(' \n', '\n')
# it also treats empty files differently
difflines[2] = difflines[2].replace('-1,0', '-0,0').replace('+1,0', '+0,0')
has_diff = False
for line in difflines:
has_diff = True
if line.startswith('+'):
line = stringc(line, C.COLOR_DIFF_ADD)
elif line.startswith('-'):
line = stringc(line, C.COLOR_DIFF_REMOVE)
elif line.startswith('@@'):
line = stringc(line, C.COLOR_DIFF_LINES)
ret.append(line)
if has_diff:
ret.append('\n')
if 'prepared' in diff:
ret.append(to_text(diff['prepared']))
except UnicodeDecodeError:
ret.append(">> the files are different, but the diff library cannot compare unicode strings\n\n")
return u''.join(ret)

Is it wrong?

Copy link
Member

Choose a reason for hiding this comment

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

I get it now, so adding diff as part of prepared key results is rendering diff in a readable format in output logs.
The only concern I have here is this change might break backward compatibility for playbook that depend on diff key

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, that may be the problem.
Do you have any idea to keep backward compatibility?

Copy link
Contributor

Choose a reason for hiding this comment

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

Note that this line was only added recently (https://github.com/ansible/ansible/pull/41846/files) for Ansible 1.7 development branch, i.e. it has not been added to a release.
So your change should be safe.

Copy link
Contributor

Choose a reason for hiding this comment

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

In fact, @ganeshrn, you added it a couple of weeks ago ;)

Copy link
Member

@ganeshrn ganeshrn Jul 19, 2018

Choose a reason for hiding this comment

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

@felixfontein Good catch. Thanks for pointing out :)
@hitsumabushi diff is added as part of result for most network platforms, somehow is was missed in vyos_config for earlier release. I added diff in vyos_config during the recent cliconf refactor work. Looking at this PR I assumed that it was present since earlier releases.

@ansibot ansibot removed needs_triage Needs a first human triage before being processed. new_contributor This PR is the first contribution by a new community member. labels Jul 9, 2018
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Jul 18, 2018
@ganeshrn
Copy link
Member

@hitsumabushi Thank you!

@ganeshrn ganeshrn merged commit c43e51e into ansible:devel Jul 19, 2018
@ansible ansible locked and limited conversation to collaborators Jul 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.7 This issue/PR affects Ansible v2.7 core_review In order to be merged, this PR must follow the core review workflow. feature This issue/PR relates to a feature request. module This issue/PR relates to a module. networking Network category python3 small_patch stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. support:network This issue/PR relates to code supported by the Ansible Network Team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants