@@ -552,7 +552,11 @@ public int run() throws Exception {
552552 }
553553
554554 if (sshUsePassword ) {
555- String extraVarsPassword = "ansible_password: " + sshPass ;
555+ // Escape special characters in the password to prevent YAML parsing errors
556+ String escapedPassword = escapePasswordForYaml (sshPass );
557+
558+ // Wrap in quotes to preserve special characters
559+ String extraVarsPassword = "ansible_password: \" " + escapedPassword + "\" " ;
556560 String finalextraVarsPassword = extraVarsPassword ;
557561
558562 if (useAnsibleVault ){
@@ -571,7 +575,11 @@ public int run() throws Exception {
571575 procArgs .add ("--become" );
572576
573577 if (becomePassword != null && !becomePassword .isEmpty ()) {
574- String extraVarsPassword = "ansible_become_password: " + becomePassword ;
578+ // Escape special characters in the password to prevent YAML parsing errors
579+ String escapedPassword = escapePasswordForYaml (becomePassword );
580+
581+ // Wrap in quotes to preserve special characters
582+ String extraVarsPassword = "ansible_become_password: \" " + escapedPassword + "\" " ;
575583 String finalextraVarsPassword = extraVarsPassword ;
576584
577585 if (useAnsibleVault ) {
@@ -981,6 +989,22 @@ String escapeYamlValue(String value) {
981989 return value ;
982990 }
983991
992+ /**
993+ * Escapes a password value for safe use in YAML.
994+ * Always wraps the password in quotes and escapes special characters to prevent YAML parsing errors.
995+ *
996+ * @param password The password to escape
997+ * @return Escaped and quoted password safe for YAML
998+ */
999+ String escapePasswordForYaml (String password ) {
1000+ // Escape special characters in the password to prevent YAML parsing errors
1001+ return password
1002+ .replace ("\\ " , "\\ \\ " )
1003+ .replace ("\" " , "\\ \" " )
1004+ .replace ("\n " , "\\ n" )
1005+ .replace ("\r " , "\\ r" );
1006+ }
1007+
9841008 /**
9851009 * Validates that a string is in proper Ansible vault format.
9861010 * Expected format: "!vault |\n" followed by vault-encrypted content
0 commit comments