Reputation: 115
I have been trying to run anisble-playbooks using Java (runtime().exec() and ProcessBuilder), and in both instances I see that the extra variables that I wish to pass using the commandline never get executed or so it seems.
ProcessBuilder builder = new ProcessBuilder("ansible-playbook", "/root/playbooks/script-ilo.yml", "-e", "'@/tmp/vars.yml'");
and
String[] ansible_run = {"ansible-playbook", "/root/playbooks/script-ilo.yml", "-e", "'@/tmp/vars.yml'"};
Process p = Runtime.getRuntime().exec(ansible_run,null);
I packed the code as a jar and executed in test system and in both cases, ansible runs the playbook and throws an error.
# java -jar /home/admin/test-script.jar
PLAY [esxi] ********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.50.100]
TASK [Set XML with new secrets] ************************************************
fatal: [192.168.50.100]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'change_user' is undefined\n\nThe error appears to have been in '/root/playbooks/script-ilo.yml': line 3, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: Set XML with new secrets\n ^ here\n"}
to retry, use: --limit @/root/playbooks/script-ilo.retry
PLAY RECAP *********************************************************************
192.168.50.100 : ok=1 changed=0 unreachable=0 failed=1
When I run the command ansible-playbook /root/playbooks/script-ilo.yml -e '@/tmp/vars.yml'
in the shell, it run perfect.
I need help with getting unblocked here. If there is a better way to do this, I am all ears.
Upvotes: 5
Views: 8824
Reputation: 806
I have faced a similar issue and solved it using the following
ProcessBuilder builder = new ProcessBuilder("ansible-playbook", "/etc/ansible/playbooks_vmware/diskadd1.yaml","-e","vm_name=web04 addSizeInGB=40 scsi=0 unit_number=1");
Upvotes: 3
Reputation: 33223
String[] ansible_run = {"ansible-playbook", "/root/playbooks/script-ilo.yml", "-e", "'@/tmp/vars.yml'"};
Don't put single quotes in that -e
value; the single quotes are only needed for your shell, but ProcessBuilder
doesn't go through your shell, so the arguments don't need to be escaped.
I was actually expecting ansible to whine when I fed it a blatantly bogus -e
, but it turns out that any such value is passed in to the hostvars
as _raw_params
, so in your case, it would have set a value like:
"hostvars": {
"192.168.50.100": {
"_raw_params": "'@/tmp/vars.yml'",
Upvotes: 4