简体   繁体   English

从 Java 运行 ansible-playbook

[英]running ansible-playbook from java

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.我一直在尝试使用 Java(runtime().exec() 和 ProcessBuilder)运行 anisble-playbooks,在这两种情况下,我都看到我希望使用命令行传递的额外变量永远不会被执行,或者看起来如此。

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.我将代码打包为 jar 并在测试系统中执行,在这两种情况下,ansible 都会运行剧本并引发错误。

# 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.当我在 shell 中运行命令ansible-playbook /root/playbooks/script-ilo.yml -e '@/tmp/vars.yml'时,它运行完美。

I need help with getting unblocked here.我需要帮助来解锁这里。 If there is a better way to do this, I am all ears.如果有更好的方法来做到这一点,我全神贯注。

String[] ansible_run = {"ansible-playbook", "/root/playbooks/script-ilo.yml", "-e", "'@/tmp/vars.yml'"};

Don't put single quotes in that -e value;不要在该-e值中放置单引号; 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.单引号仅在您的外壳程序中需要,但ProcessBuilder不会通过您的外壳程序,因此不需要对参数进行转义。

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:当我给它一个公然伪造的-e时,我实际上希望 ansible 发出呜呜声,但事实证明,任何此类值都作为_raw_params传递给hostvars ,因此在您的情况下,它会设置一个如下值:

"hostvars": {
    "192.168.50.100": {
        "_raw_params": "'@/tmp/vars.yml'",

我遇到了类似的问题并使用以下方法解决了它

ProcessBuilder builder = new ProcessBuilder("ansible-playbook", "/etc/ansible/playbooks_vmware/diskadd1.yaml","-e","vm_name=web04 addSizeInGB=40 scsi=0 unit_number=1");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM