25
25
import com .rundeck .plugins .ansible .ansible .AnsibleRunner ;
26
26
import com .rundeck .plugins .ansible .ansible .InventoryList ;
27
27
import com .rundeck .plugins .ansible .util .VaultPrompt ;
28
+ import lombok .Setter ;
28
29
import org .rundeck .app .spi .Services ;
29
30
import org .rundeck .storage .api .PathUtil ;
30
31
import org .rundeck .storage .api .StorageException ;
31
32
import org .yaml .snakeyaml .LoaderOptions ;
32
33
import org .yaml .snakeyaml .Yaml ;
33
34
import org .yaml .snakeyaml .constructor .SafeConstructor ;
35
+ import org .yaml .snakeyaml .error .YAMLException ;
34
36
35
37
import java .io .BufferedReader ;
36
38
import java .io .ByteArrayOutputStream ;
45
47
import java .nio .file .SimpleFileVisitor ;
46
48
import java .nio .file .attribute .BasicFileAttributes ;
47
49
import java .util .ArrayList ;
50
+ import java .util .Collections ;
48
51
import java .util .HashMap ;
49
52
import java .util .HashSet ;
50
53
import java .util .List ;
@@ -60,8 +63,10 @@ public class AnsibleResourceModelSource implements ResourceModelSource, ProxyRun
60
63
public static final String HOST_TPL_J2 = "host-tpl.j2" ;
61
64
public static final String GATHER_HOSTS_YML = "gather-hosts.yml" ;
62
65
66
+ @ Setter
63
67
private Framework framework ;
64
68
69
+ @ Setter
65
70
private Services services ;
66
71
67
72
private String project ;
@@ -72,6 +77,8 @@ public class AnsibleResourceModelSource implements ResourceModelSource, ProxyRun
72
77
73
78
private String inventory ;
74
79
private boolean gatherFacts ;
80
+ @ Setter
81
+ private Integer yamlDataSize ;
75
82
private boolean ignoreErrors = false ;
76
83
private String limit ;
77
84
private String ignoreTagPrefix ;
@@ -118,17 +125,14 @@ public class AnsibleResourceModelSource implements ResourceModelSource, ProxyRun
118
125
119
126
protected boolean encryptExtraVars = false ;
120
127
128
+ @ Setter
121
129
private AnsibleInventoryList .AnsibleInventoryListBuilder ansibleInventoryListBuilder = null ;
122
130
123
131
public AnsibleResourceModelSource (final Framework framework ) {
124
132
this .framework = framework ;
125
133
}
126
134
127
- public void setAnsibleInventoryListBuilder (AnsibleInventoryList .AnsibleInventoryListBuilder builder ) {
128
- this .ansibleInventoryListBuilder = builder ;
129
- }
130
-
131
- private static String resolveProperty (
135
+ private static String resolveProperty (
132
136
final String attribute ,
133
137
final String defaultValue ,
134
138
final Properties configuration ,
@@ -142,18 +146,32 @@ private static String resolveProperty(
142
146
}
143
147
}
144
148
149
+ private static Integer resolveIntProperty (
150
+ final String attribute ,
151
+ final Integer defaultValue ,
152
+ final Properties configuration ,
153
+ final Map <String , Map <String , String >> dataContext ) throws ConfigurationException {
154
+ final String strValue = resolveProperty (attribute , null , configuration , dataContext );
155
+ if (null != strValue ) {
156
+ try {
157
+ return Integer .parseInt (strValue );
158
+ } catch (NumberFormatException e ) {
159
+ throw new ConfigurationException ("Can't parse attribute :" + attribute +
160
+ ", value: " + strValue +
161
+ " Expected Integer. : " + e .getMessage (), e );
162
+ }
163
+ }
164
+ return defaultValue ;
165
+ }
166
+
145
167
private static Boolean skipVar (final String hostVar , final List <String > varList ) {
146
168
for (final String specialVarString : varList ) {
147
169
if (hostVar .startsWith (specialVarString )) return true ;
148
170
}
149
171
return false ;
150
172
}
151
173
152
- public void setServices (Services services ) {
153
- this .services = services ;
154
- }
155
-
156
- public void configure (Properties configuration ) throws ConfigurationException {
174
+ public void configure (Properties configuration ) throws ConfigurationException {
157
175
158
176
project = configuration .getProperty ("project" );
159
177
configDataContext = new HashMap <String , Map <String , String >>();
@@ -167,6 +185,8 @@ public void configure(Properties configuration) throws ConfigurationException {
167
185
gatherFacts = "true" .equals (resolveProperty (AnsibleDescribable .ANSIBLE_GATHER_FACTS ,null ,configuration ,executionDataContext ));
168
186
ignoreErrors = "true" .equals (resolveProperty (AnsibleDescribable .ANSIBLE_IGNORE_ERRORS ,null ,configuration ,executionDataContext ));
169
187
188
+ yamlDataSize = resolveIntProperty (AnsibleDescribable .ANSIBLE_YAML_DATA_SIZE ,10 , configuration , executionDataContext );
189
+
170
190
limit = (String ) resolveProperty (AnsibleDescribable .ANSIBLE_LIMIT ,null ,configuration ,executionDataContext );
171
191
ignoreTagPrefix = (String ) resolveProperty (AnsibleDescribable .ANSIBLE_IGNORE_TAGS ,null ,configuration ,executionDataContext );
172
192
@@ -670,14 +690,22 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx
670
690
*/
671
691
public void ansibleInventoryList (NodeSetImpl nodes , AnsibleRunner .AnsibleRunnerBuilder runnerBuilder ) throws ResourceModelSourceException {
672
692
693
+ int codePointLimit = yamlDataSize * 1024 * 1024 ;
694
+
673
695
LoaderOptions snakeOptions = new LoaderOptions ();
674
696
// max inventory file size allowed to 10mb
675
- snakeOptions .setCodePointLimit (10_485_760 );
697
+ snakeOptions .setCodePointLimit (codePointLimit );
676
698
Yaml yaml = new Yaml (new SafeConstructor (snakeOptions ));
677
699
678
700
String listResp = getNodesFromInventory (runnerBuilder );
679
701
680
- Map <String , Object > allInventory = yaml .load (listResp );
702
+ Map <String , Object > allInventory ;
703
+ try {
704
+ allInventory = yaml .load (listResp );
705
+ } catch (YAMLException e ) {
706
+ throw new ResourceModelSourceException ("Cannot load yaml data coming from Ansible: " + e .getMessage (), e );
707
+ }
708
+
681
709
Map <String , Object > all = InventoryList .getValue (allInventory , ALL );
682
710
Map <String , Object > children = InventoryList .getValue (all , CHILDREN );
683
711
0 commit comments