salt自定义module - 两个文件的diff

本文介绍如何使用SaltStack的自定义模块conf_file.py来对比两份配置文件之间的差异,并展示了如何通过Salt命令行调用该模块进行具体的配置文件差异比对示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[root@saltui openstack-deploy]# cat /etc/salt/master  # 看下salt master的工作目录
auto_accept: True
worker_threads: 10
file_recv: True
file_roots:
   base:
     - /srv/openstack-deploy/salt/
   dev:
     - /srv/openstack-deploy/salt/dev
jinja_trim_blocks: True
jinja_lstrip_blocks: True
 
# conf_file模块放在_modules目录下,基于difflib库实现
[root@saltui openstack-deploy]# cat /srv/openstack-deploy/salt/_modules/conf_file.py
# -*- coding: utf-8 -*-
'''
Diff information about files
'''
 
import contextlib
import difflib
import os
import salt.utils
 
 
def _binary_replace(old, new):
    '''
    This function does NOT do any diffing, it just checks the old and new files
    to see if either is binary, and provides an appropriate string noting the
    difference between the two files. If neither file is binary, an empty
    string is returned.
 
    This function should only be run AFTER it has been determined that the
    files differ.
    '''
    old_isbin = not salt.utils.istextfile(old)
    new_isbin = not salt.utils.istextfile(new)
    if any((old_isbin, new_isbin)):
        if all((old_isbin, new_isbin)):
            return 'Replace binary file'
        elif old_isbin:
            return 'Replace binary file with text file'
        elif new_isbin:
            return 'Replace text file with binary file'
    return ''
 
 
def diff(
    src,
    dst,
    onlydiff=True):
    '''
    Return unified diff of file compared to file on master
 
    CLI Example:
 
    .. code-block:: bash
 
        salt '*' conf_file.diff /home/fred/.vimrc /home/users/fred/.vimrc
    '''
    src = os.path.expanduser(src)
 
    ret = ''
 
    if not os.path.exists(src):
        ret = 'File {0} does not exist on the minion'.format(src)
        return ret
 
    if not os.path.exists(dst):
        ret = 'File {0} does not exist on the minion'.format(dst)
        return ret
 
    with contextlib.nested(salt.utils.fopen(src, 'r'),
                           salt.utils.fopen(dst, 'r')) \
            as (s, d):
        slines = s.readlines()
        dlines = d.readlines()
    if ''.join(slines) != ''.join(dlines):
        bdiff = _binary_replace(src, dst)
        if bdiff:
            ret += bdiff
        else:
            diff = difflib.unified_diff(slines, dlines,
                                        src, dst)
            if onlydiff:
                changes = [l for l in diff if l.startswith('+') or l.startswith('-')
                           or l.startswith('@@')]
            else:
                changes = diff
            ret += ''.join(changes)
 
    return ret
# 下发模块
[root@saltui openstack-deploy]# salt 'node_172_16_214_18*' saltutil.sync_modules

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 默认只显示差异的部分
[root@saltui openstack-deploy]# salt 'node_172_16_214_18*' conf_file.diff /tmp/nova.conf /etc/nova/nova.conf
node_172_16_214_181:
    --- /tmp/nova.conf
    +++ /etc/nova/nova.conf
    @@ -1,9 +1,8 @@
    -#
    -verbose = False
    +verbose = True
    @@ -12,7 +11,7 @@
    -quota_cores = 200
    +quota_cores = 20
node_172_16_214_182:
    --- /tmp/nova.conf
    +++ /etc/nova/nova.conf
    @@ -31,7 +31,7 @@
    -max_io_ops_per_host = 100
    +max_io_ops_per_host = 10
node_172_16_214_183:
    --- /tmp/nova.conf
    +++ /etc/nova/nova.conf
    @@ -13,7 +13,7 @@
    -quota_floating_ips = 100
    +quota_floating_ips = 10

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 加上onlydiff=False,也显示共同部分
[root@saltui openstack-deploy]# salt 'node_172_16_214_18*' conf_file.diff /tmp/nova.conf /etc/nova/nova.conf onlydiff=False
node_172_16_214_181:
    --- /tmp/nova.conf
    +++ /etc/nova/nova.conf
    @@ -1,9 +1,8 @@
     [DEFAULT]
     # Logs / State
    -#
     debug = False
    -verbose = False
    +verbose = True
     fatal_deprecations = False
     log_dir = /var/log/nova
     state_path = /var/lib/nova
    @@ -12,7 +11,7 @@
     novncproxy_host = node_172_16_214_181
     # Quota
    -quota_cores = 200
    +quota_cores = 20
     quota_fixed_ips = -1
     quota_floating_ips = 10
     quota_injected_file_content_bytes = 10240
node_172_16_214_182:
    --- /tmp/nova.conf
    +++ /etc/nova/nova.conf
    @@ -31,7 +31,7 @@
     cpu_allocation_ratio = 2.0
     disk_allocation_ratio = 1.0
     max_instances_per_host = 50
    -max_io_ops_per_host = 100
    +max_io_ops_per_host = 10
     ram_allocation_ratio = 1.0
     ram_weight_multiplier = 5.0
     reserved_host_disk_mb = 2048
node_172_16_214_183:
    --- /tmp/nova.conf
    +++ /etc/nova/nova.conf
    @@ -13,7 +13,7 @@
     # Quota
     quota_cores = 20
     quota_fixed_ips = -1
    -quota_floating_ips = 100
    +quota_floating_ips = 10
     quota_injected_file_content_bytes = 10240
     quota_injected_file_path_length = 255
     quota_injected_files = 5

 

转载于:https://my.oschina.net/yiyuanxi/blog/1544636

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值