@@ -75,29 +75,59 @@ def get(path, value=''):
7575 return ret
7676
7777
78- def setvalue (path , value ):
78+ def setvalue (* args ):
7979 '''
8080 Set a value for a specific augeas path
8181
8282 CLI Example::
8383
8484 salt '*' augeas.setvalue /files/etc/hosts/1/canonical localhost
85+
86+ salt '*' augeas.setvalue /files/etc/hosts/01/ipaddr 192.168.1.1 \
87+ /files/etc/hosts/01/canonical hostname
88+
89+ salt '*' augeas.setvalue prefix=/files/etc/sudoers/ \
90+ "/spec[user = '%wheel']/user" "%wheel" \
91+ "/spec[user = '%wheel']/host_group/host" 'ALL' \
92+ "/spec[user = '%wheel']/host_group/command[1]" 'ALL' \
93+ "/spec[user = '%wheel']/host_group/command[1]/tag" 'PASSWD' \
94+ "/spec[user = '%wheel']/host_group/command[2]" '/usr/bin/apt-get' \
95+ "/spec[user = '%wheel']/host_group/command[2]/tag" NOPASSWD
8596 '''
8697
98+
8799 from augeas import Augeas
88100 aug = Augeas ()
89101
90102 ret = {'retval' : False }
91103
104+ prefix = None
105+
106+
107+ tuples = filter (lambda x : not x .startswith ('prefix=' ), args )
108+ prefix = filter (lambda x : x .startswith ('prefix=' ), args )
109+ if prefix :
110+ prefix = prefix [0 ].split ('=' , 1 )[1 ]
111+
112+ if len (tuples ) % 2 != 0 :
113+ return ret # ensure we have multiple of twos
114+
115+ tuple_iter = iter (tuples )
116+
117+ for path , value in zip (tuple_iter , tuple_iter ):
118+ target_path = path
119+ if prefix :
120+ target_path = "{0}/{1}" .format (prefix .rstrip ('/' ), path .lstrip ('/' ))
121+ try :
122+ aug .set (target_path , str (value ))
123+ except ValueError as err :
124+ ret ['error' ] = "Multiple values: " + str (err )
125+
92126 try :
93- aug .set (path , unicode (value ))
94127 aug .save ()
95128 ret ['retval' ] = True
96- except ValueError as err :
97- ret ['error' ] = "Multiple values: " + str (err )
98129 except IOError as err :
99130 ret ['error' ] = str (err )
100-
101131 return ret
102132
103133
0 commit comments