From 5fb64b4abecc33aa34e3bea2f872872f99b1ba39 Mon Sep 17 00:00:00 2001 From: Ulrich Dangel Date: Tue, 19 Jun 2012 23:36:19 +0200 Subject: [PATCH] Allow multiple set commands in one setvalue command. This can be used to create more complex node structures at once, e.g: salt '*' augeas.setvalue prefix=/files/etc/sudoers \ "spec[user = '%wheel']/user" "%wheel" \ "spec[user = '%wheel']/host_group/host" ALL \ "spec[user = '%wheel']/host_group/command[1]" 'ALL' \ "spec[user = '%wheel']/host_group/command[1]/tag" 'PASSWD' \ "spec[user = '%wheel']/host_group/command[2]" '/usr/bin/apt-get' \ "spec[user = '%wheel']/host_group/command[2]/tag" NOPASSWD \ "spec[user = '%wheel']/host_group/command[3]" '/usr/bin/aptitude' will create following line in /etc/sudoers: %wheel ALL = PASSWD : ALL , NOPASSWD : /usr/bin/apt-get , /usr/bin/aptitude --- salt/modules/augeas_cfg.py | 40 +++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/salt/modules/augeas_cfg.py b/salt/modules/augeas_cfg.py index 0ec946671af1..784f284ed628 100644 --- a/salt/modules/augeas_cfg.py +++ b/salt/modules/augeas_cfg.py @@ -75,29 +75,59 @@ def get(path, value=''): return ret -def setvalue(path, value): +def setvalue(*args): ''' Set a value for a specific augeas path CLI Example:: salt '*' augeas.setvalue /files/etc/hosts/1/canonical localhost + + salt '*' augeas.setvalue /files/etc/hosts/01/ipaddr 192.168.1.1 \ + /files/etc/hosts/01/canonical hostname + + salt '*' augeas.setvalue prefix=/files/etc/sudoers/ \ + "/spec[user = '%wheel']/user" "%wheel" \ + "/spec[user = '%wheel']/host_group/host" 'ALL' \ + "/spec[user = '%wheel']/host_group/command[1]" 'ALL' \ + "/spec[user = '%wheel']/host_group/command[1]/tag" 'PASSWD' \ + "/spec[user = '%wheel']/host_group/command[2]" '/usr/bin/apt-get' \ + "/spec[user = '%wheel']/host_group/command[2]/tag" NOPASSWD ''' + from augeas import Augeas aug = Augeas() ret = {'retval': False} + prefix = None + + + tuples = filter(lambda x: not x.startswith('prefix='), args) + prefix = filter(lambda x: x.startswith('prefix='), args) + if prefix: + prefix = prefix[0].split('=', 1)[1] + + if len(tuples) % 2 != 0: + return ret # ensure we have multiple of twos + + tuple_iter = iter(tuples) + + for path, value in zip(tuple_iter, tuple_iter): + target_path = path + if prefix: + target_path = "{0}/{1}".format(prefix.rstrip('/'), path.lstrip('/')) + try: + aug.set(target_path, str(value)) + except ValueError as err: + ret['error'] = "Multiple values: " + str(err) + try: - aug.set(path, unicode(value)) aug.save() ret['retval'] = True - except ValueError as err: - ret['error'] = "Multiple values: " + str(err) except IOError as err: ret['error'] = str(err) - return ret