Check Java version via Ansible playbook

蹲街弑〆低调 提交于 2019-12-10 22:19:38

问题


Following is my playbook:

---
- hosts: UAT
  gather_facts: false
  remote_user: xxxx
  become_method: sudo
  become: yes
  become_user: sudo_user
  tasks:
   - name: Fetch Java version
     command: java -version
     register: java_result
     ignore_errors: True
   - debug: "msg={{ java_result.stdout }}"
...

And I am getting the below error:

fatal: [ma-dsast-lapp10]: FAILED! => {
    "changed": false,
    "cmd": "java -version '2>&1' '|' grep version",
    "failed": true,
    "invocation": {
        "module_args": {
            "_raw_params": "java -version 2>&1 | grep version",
            "_uses_shell": false,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "warn": true
        }
    },
    "msg": "[Errno 2] No such file or directory",
    "rc": 2
}

Im getting : No such file or directory error

What could be the issue with this? Please help me on this.


回答1:


I ran your script on my machine and it did not work either. I modified it a little bit

---

- hosts: localhost
  gather_facts: False

  tasks:
   - name: Fetch Java version
     shell: java -version 2>&1 | grep version | awk '{print $3}' | sed 's/"//g'                                                                                                                                   
     changed_when: False
     register: java_result

   - debug:
       msg: "{{ java_result.stdout }}"

And I got

TASK [debug] *******************************************************************************************
ok: [localhost] => {
    "msg": "1.8.0_181"
}


来源:https://stackoverflow.com/questions/53740372/check-java-version-via-ansible-playbook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!