@@ -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 }
0 commit comments