@@ -290,6 +290,7 @@ public static AnsibleRunner buildAnsibleRunner(AnsibleRunnerContextBuilder conte
290
290
File tempVaultFile ;
291
291
File tempSshVarsFile ;
292
292
File tempBecameVarsFile ;
293
+ File vaultPromptFile ;
293
294
294
295
public void deleteTempDirectory (Path tempDirectory ) throws IOException {
295
296
Files .walkFileTree (tempDirectory , new SimpleFileVisitor <Path >() {
@@ -337,7 +338,6 @@ public int run() throws Exception {
337
338
338
339
if (ansibleVault ==null ){
339
340
tempInternalVaultFile = AnsibleVault .createVaultScriptAuth ("ansible-script-vault" );
340
-
341
341
ansibleVault = AnsibleVault .builder ()
342
342
.baseDirectory (baseDirectory )
343
343
.masterPassword (AnsibleUtil .randomString ())
@@ -387,12 +387,7 @@ public int run() throws Exception {
387
387
388
388
useAnsibleVault = ansibleVault .checkAnsibleVault ();
389
389
390
- if (useAnsibleVault ) {
391
- tempInternalVaultFile = ansibleVault .getVaultPasswordScriptFile ();
392
-
393
- procArgs .add ("--vault-id" );
394
- procArgs .add ("internal-encrypt@" + tempInternalVaultFile .getAbsolutePath ());
395
- }else {
390
+ if (!useAnsibleVault ) {
396
391
System .err .println ("WARN: ansible-vault is not installed, extra-vars will not be encrypted." );
397
392
}
398
393
}
@@ -431,12 +426,6 @@ public int run() throws Exception {
431
426
procArgs .add ("--extra-vars" + "=" + "@" + tempVarsFile .getAbsolutePath ());
432
427
}
433
428
434
- if (vaultPass != null && !vaultPass .isEmpty ()) {
435
- tempVaultFile = ansibleVault .getVaultPasswordScriptFile ();
436
- procArgs .add ("--vault-id" );
437
- procArgs .add (tempVaultFile .getAbsolutePath ());
438
- }
439
-
440
429
if (sshPrivateKey != null && !sshPrivateKey .isEmpty ()) {
441
430
String privateKeyData = sshPrivateKey .replaceAll ("\r \n " , "\n " );
442
431
tempPkFile = AnsibleUtil .createTemporaryFile ("id_rsa" , privateKeyData );
@@ -506,16 +495,14 @@ public int run() throws Exception {
506
495
procArgs .addAll (tokenizeCommand (extraParams ));
507
496
}
508
497
509
- if (debug ) {
510
- System .out .println (" procArgs: " + procArgs );
511
- }
512
-
513
498
if (processExecutorBuilder ==null ){
514
499
processExecutorBuilder = ProcessExecutor .builder ();
515
500
}
516
501
517
- //set main process command
518
- processExecutorBuilder .procArgs (procArgs );
502
+ if (debug ) {
503
+ System .out .println (" procArgs: " + procArgs );
504
+ processExecutorBuilder .debug (true );
505
+ }
519
506
520
507
if (baseDirectory != null ) {
521
508
processExecutorBuilder .baseDirectory (baseDirectory .toFile ());
@@ -540,20 +527,51 @@ public int run() throws Exception {
540
527
processEnvironment .put ("SSH_AUTH_SOCK" , this .sshAgent .getSocketPath ());
541
528
}
542
529
543
- processExecutorBuilder .environmentVariables (processEnvironment );
544
-
545
530
//set STDIN variables
546
- List <String > stdinVariables = new ArrayList <>();
531
+ List <VaultPrompt > stdinVariables = new ArrayList <>();
532
+
533
+ if (useAnsibleVault || vaultPass != null ){
534
+ vaultPromptFile = File .createTempFile ("vault-prompt" , ".log" );
535
+ }
547
536
548
537
if (useAnsibleVault ) {
549
- stdinVariables .add (ansibleVault .getMasterPassword () + "\n " );
538
+ VaultPrompt vaultPrompt = VaultPrompt .builder ()
539
+ .vaultId ("internal-encrypt" )
540
+ .vaultPassword (ansibleVault .getMasterPassword () + "\n " )
541
+ .build ();
542
+
543
+ stdinVariables .add (vaultPrompt );
544
+ processEnvironment .put ("LOG_PATH" , vaultPromptFile .getAbsolutePath ());
545
+
546
+ tempInternalVaultFile = ansibleVault .getVaultPasswordScriptFile ();
547
+
548
+ procArgs .add ("--vault-id" );
549
+ procArgs .add ("internal-encrypt@" + tempInternalVaultFile .getAbsolutePath ());
550
550
}
551
551
552
552
if (vaultPass != null && !vaultPass .isEmpty ()) {
553
- stdinVariables .add (vaultPass + "\n " );
553
+ VaultPrompt vaultPrompt = VaultPrompt .builder ()
554
+ .vaultId ("None" )
555
+ .vaultPassword (vaultPass + "\n " )
556
+ .build ();
557
+
558
+ stdinVariables .add (vaultPrompt );
559
+ processEnvironment .putIfAbsent ("LOG_PATH" , vaultPromptFile .getAbsolutePath ());
560
+
561
+ tempVaultFile = ansibleVault .getVaultPasswordScriptFile ();
562
+ procArgs .add ("--vault-id" );
563
+ procArgs .add (tempVaultFile .getAbsolutePath ());
554
564
}
555
565
566
+ //set main process command
567
+ processExecutorBuilder .procArgs (procArgs );
556
568
processExecutorBuilder .stdinVariables (stdinVariables );
569
+ processExecutorBuilder .environmentVariables (processEnvironment );
570
+
571
+ //set vault prompt file
572
+ if (vaultPromptFile !=null ){
573
+ processExecutorBuilder .promptStdinLogFile (vaultPromptFile );
574
+ }
557
575
558
576
proc = processExecutorBuilder .build ().run ();
559
577
@@ -627,6 +645,10 @@ public int run() throws Exception {
627
645
tempInternalVaultFile .deleteOnExit ();
628
646
}
629
647
648
+ if (vaultPromptFile != null && !vaultPromptFile .delete ()){
649
+ vaultPromptFile .deleteOnExit ();
650
+ }
651
+
630
652
if (usingTempDirectory && !retainTempDirectory ) {
631
653
deleteTempDirectory (baseDirectory );
632
654
}
@@ -668,9 +690,13 @@ public boolean registerKeySshAgent(String keyPath) throws Exception {
668
690
env .put ("SSH_ASKPASS" , tempPassVarsFile .getAbsolutePath ());
669
691
}
670
692
671
- List <String > stdinVariables = new ArrayList <>();
693
+ List <VaultPrompt > stdinVariables = new ArrayList <>();
672
694
if (sshPassphrase != null && !sshPassphrase .isEmpty ()) {
673
- stdinVariables .add (sshPassphrase + "\n " );
695
+ VaultPrompt sshPassPrompt = VaultPrompt .builder ()
696
+ .vaultPassword (sshPassphrase + "\n " )
697
+ .build ();
698
+
699
+ stdinVariables .add (sshPassPrompt );
674
700
}
675
701
676
702
ProcessExecutor processExecutor = ProcessExecutor .builder ()
0 commit comments