Skip to content

Instantly share code, notes, and snippets.

@meganlkm
Created January 18, 2018 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meganlkm/d0c959aa7bcb332822a68c528bc434ae to your computer and use it in GitHub Desktop.
Save meganlkm/d0c959aa7bcb332822a68c528bc434ae to your computer and use it in GitHub Desktop.
"""
example sls:
merge_reactor_config:
module.run:
- name: conf_manip.merge
- origin: /etc/salt/master.d/reactor.conf
- source: /srv/salt/mystate/files/reactor.conf
"""
import filecmp
import os
import yaml
from collections import Mapping
from shutil import copyfile
def __update_list(src, base, changed=False):
for val in src:
if val not in base:
base.append(val)
changed = True
return base, changed
def merge(origin, source):
response = {}
if os.path.exists(origin):
if not filecmp.cmp(origin, source):
origin_data = yaml.load(open(origin))
source_data = yaml.load(open(source))
changed = False
for key, value in source_data.iteritems():
if value != origin_data.get(key):
if isinstance(value, list):
origin_data[key], changed = __update_list(value, origin_data[key], changed)
elif isinstance(value, Mapping):
origin_data[key].update(value)
changed = True
else:
origin_data[key] = value
changed = True
if changed:
stream = file(origin, 'w')
yaml.dump(origin_data, stream, default_flow_style=False)
response['updated'] = changed
else:
copyfile(source, origin)
response['created'] = os.path.exists(origin)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment