Skip to content

Commit 1fa16df

Browse files
committed
group_vars and properties in nodeexecutor
1 parent de654c6 commit 1fa16df

4 files changed

Lines changed: 46 additions & 10 deletions

File tree

src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleRunner.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public static AnsibleRunner buildAnsibleRunner(AnsibleRunnerContextBuilder conte
128128
}
129129

130130
Boolean generateInventoryNodeAuth = contextBuilder.generateInventoryNodesAuth();
131-
if(generateInventoryNodeAuth){
131+
if(generateInventoryNodeAuth != null && generateInventoryNodeAuth){
132132
Map<String, Map<String, String>> nodesAuth = contextBuilder.getNodesAuthenticationMap();
133133
if (nodesAuth != null && !nodesAuth.isEmpty()) {
134134
ansibleRunnerBuilder.addNodeAuthToInventory(true);
@@ -300,11 +300,13 @@ public static AnsibleRunner buildAnsibleRunner(AnsibleRunnerContextBuilder conte
300300
File tempSshVarsFile ;
301301
File tempBecameVarsFile ;
302302
File vaultPromptFile;
303-
File tempNodeAuthFile = null;
303+
File tempNodeAuthFile;
304+
File groupVarsDir;
304305

305306
String customTmpDirPath;
306307

307-
Boolean addNodeAuthToInventory;
308+
@Builder.Default
309+
Boolean addNodeAuthToInventory = false;
308310
Map<String, Map<String, String>> nodesAuthentication;
309311

310312
public void deleteTempDirectory(Path tempDirectory) throws IOException {
@@ -410,7 +412,7 @@ public int run() throws Exception {
410412
procArgs.add("-i");
411413
procArgs.add(inventory);
412414

413-
if(addNodeAuthToInventory) {
415+
if(addNodeAuthToInventory != null && addNodeAuthToInventory && nodesAuthentication != null && !nodesAuthentication.isEmpty()) {
414416
Map<String, String> hostUsers = new LinkedHashMap<>();
415417
Map<String, String> hostPasswords = new LinkedHashMap<>();
416418
nodesAuthentication.forEach((nodeName, authValues) -> {
@@ -437,7 +439,30 @@ public int run() throws Exception {
437439
yamlData.put("host_users", hostUsers);
438440
try {
439441
String yamlContent = mapperYaml.writeValueAsString(yamlData);
440-
tempNodeAuthFile = AnsibleUtil.createTemporaryFile("", "all.yaml", yamlContent, customTmpDirPath);
442+
443+
// Create group_vars directory structure
444+
File inventoryFile = new File(inventory);
445+
File inventoryParentDir = inventoryFile.getParentFile();
446+
447+
if (inventoryParentDir != null) {
448+
groupVarsDir = new File(inventoryParentDir, "group_vars");
449+
450+
if (!groupVarsDir.exists()) {
451+
if (!groupVarsDir.mkdirs()) {
452+
throw new RuntimeException("Failed to create group_vars directory at: " + groupVarsDir.getAbsolutePath());
453+
}
454+
}
455+
456+
// Create all.yaml in group_vars directory
457+
tempNodeAuthFile = new File(groupVarsDir, "all.yaml");
458+
java.nio.file.Files.writeString(tempNodeAuthFile.toPath(), yamlContent);
459+
tempNodeAuthFile.deleteOnExit();
460+
groupVarsDir.deleteOnExit();
461+
} else {
462+
// Fallback to temp file if inventory has no parent directory
463+
tempNodeAuthFile = AnsibleUtil.createTemporaryFile("group_vars", "all.yaml", yamlContent, customTmpDirPath);
464+
}
465+
441466
} catch (IOException e) {
442467
throw new RuntimeException("Failed to write all.yaml for node auth", e);
443468
}
@@ -712,6 +737,12 @@ public int run() throws Exception {
712737
tempNodeAuthFile.deleteOnExit();
713738
}
714739

740+
if (groupVarsDir != null && groupVarsDir.exists()) {
741+
if (!groupVarsDir.delete()) {
742+
groupVarsDir.deleteOnExit();
743+
}
744+
}
745+
715746
if (usingTempDirectory && !retainTempDirectory) {
716747
deleteTempDirectory(baseDirectory);
717748
}

src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsibleNodeExecutor.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class AnsibleNodeExecutor implements NodeExecutor, AnsibleDescribable, Pr
3636
builder.property(WINDOWS_EXECUTABLE_PROP);
3737
builder.property(CONFIG_FILE_PATH);
3838
builder.property(GENERATE_INVENTORY_PROP);
39+
builder.property(GENERATE_INVENTORY_NODES_AUTH);
3940
builder.property(SSH_AUTH_TYPE_PROP);
4041
builder.property(SSH_USER_PROP);
4142
builder.property(SSH_PASSWORD_STORAGE_PROP);
@@ -63,6 +64,8 @@ public class AnsibleNodeExecutor implements NodeExecutor, AnsibleDescribable, Pr
6364
builder.frameworkMapping(ANSIBLE_CONFIG_FILE_PATH,FWK_PROP_PREFIX + ANSIBLE_CONFIG_FILE_PATH);
6465
builder.mapping(ANSIBLE_GENERATE_INVENTORY,PROJ_PROP_PREFIX + ANSIBLE_GENERATE_INVENTORY);
6566
builder.frameworkMapping(ANSIBLE_GENERATE_INVENTORY,FWK_PROP_PREFIX + ANSIBLE_GENERATE_INVENTORY);
67+
builder.mapping(ANSIBLE_GENERATE_INVENTORY_NODES_AUTH,PROJ_PROP_PREFIX + ANSIBLE_GENERATE_INVENTORY_NODES_AUTH);
68+
builder.frameworkMapping(ANSIBLE_GENERATE_INVENTORY_NODES_AUTH,FWK_PROP_PREFIX + ANSIBLE_GENERATE_INVENTORY_NODES_AUTH);
6669
builder.mapping(ANSIBLE_SSH_AUTH_TYPE,PROJ_PROP_PREFIX + ANSIBLE_SSH_AUTH_TYPE);
6770
builder.frameworkMapping(ANSIBLE_SSH_AUTH_TYPE,FWK_PROP_PREFIX + ANSIBLE_SSH_AUTH_TYPE);
6871
builder.mapping(ANSIBLE_SSH_USER,PROJ_PROP_PREFIX + ANSIBLE_SSH_USER);
@@ -197,7 +200,13 @@ public List<String> listSecretsPath(ExecutionContext context, INodeEntry node) {
197200
jobConf.put(AnsibleDescribable.ANSIBLE_LIMIT,node.getNodename());
198201
AnsibleRunnerContextBuilder builder = new AnsibleRunnerContextBuilder(node, context, context.getFramework(), jobConf);
199202

200-
return AnsibleUtil.getSecretsPath(builder);
203+
List<String> secretPaths = AnsibleUtil.getSecretsPath(builder);
204+
List<String> secretPathsNodes = builder.getListNodesKeyPath();
205+
206+
if(secretPathsNodes != null && !secretPathsNodes.isEmpty()){
207+
secretPaths.addAll(secretPathsNodes);
208+
}
209+
return secretPaths;
201210
}
202211
}
203212

src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsiblePlaybookInlineWorkflowStep.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ public class AnsiblePlaybookInlineWorkflowStep implements StepPlugin, AnsibleDes
4343
builder.property(PLAYBOOK_INLINE_PROP);
4444
builder.property(EXTRA_VARS_PROP);
4545
builder.property(CONFIG_ENCRYPT_EXTRA_VARS);
46-
builder.property(GENERATE_INVENTORY_PROP);
47-
builder.property(GENERATE_INVENTORY_NODES_AUTH);
4846
builder.property(INVENTORY_INLINE_PROP);
4947
builder.property(VAULT_KEY_FILE_PROP);
5048
builder.property(VAULT_KEY_STORAGE_PROP);

src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsiblePlaybookWorkflowStep.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ public class AnsiblePlaybookWorkflowStep implements StepPlugin, AnsibleDescribab
4343
builder.property(EXTRA_VARS_PROP);
4444
builder.property(CONFIG_ENCRYPT_EXTRA_VARS);
4545
builder.property(INVENTORY_INLINE_PROP);
46-
builder.property(GENERATE_INVENTORY_PROP);
47-
builder.property(GENERATE_INVENTORY_NODES_AUTH);
4846
builder.property(VAULT_KEY_FILE_PROP);
4947
builder.property(VAULT_KEY_STORAGE_PROP);
5048
builder.property(EXTRA_ATTRS_PROP);

0 commit comments

Comments
 (0)