You start by taking the string.
ansible-playbook delete.yml --extra-vars "host=5.955.595 user=root pass=anotherpw vm=myVm"
Then you escape all special characters. In this case, that’s just the 2 double-quotes.
ansible-playbook delete.yml --extra-vars \"host=5.955.595 user=root pass=anotherpw vm=myVm\"
Then you surround that with double-quotes, to make it a Java string literal.
"ansible-playbook delete.yml --extra-vars \"host=5.955.595 user=root pass=anotherpw vm=myVm\""
Then you remove hardcoded values and replace them with string concatenations with the variables having the dynamic values.
"ansible-playbook delete.yml --extra-vars \"host=" + ip + " user=" + usr + " pass=" + pw + " vm=" + vmName + "\""
Then you assign it to a variable.
String p = "ansible-playbook delete.yml --extra-vars \"host=" + ip + " user=" + usr + " pass=" + pw + " vm=" + vmName + "\"";
Then you wrap it over multiple lines, to make it more readable.
String p = "ansible-playbook delete.yml --extra-vars \"host=" + ip +
" user=" + usr +
" pass=" + pw +
" vm=" + vmName + "\"";
See how easy it is, if you just take it one step at a time?
1
solved Java Quoting string variables