通过Ansible手册检查Java版本

时间:2018-12-12 09:57:48

标签: java shell scripting ansible version

以下是我的剧本:

---
- 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 }}"
...

我收到以下错误:

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
}

我得到了:没有这样的文件或目录错误

这可能是什么问题? 请帮助我。

2 个答案:

答案 0 :(得分:0)

我在计算机上运行了您的脚本,但该脚本也不起作用。我做了一点修改

"request": {
		"type": "IntentRequest",
		"requestId": "amzn1.echo-api.request.9529849e-190d-4278-95a8-3702b3ee4d1c",
		"timestamp": "2018-12-12T10:05:14Z",
		"locale": "en-US",
		"intent": {
			"name": "NewAppointmentIntent",
			"confirmationStatus": "NONE",
			"slots": {
				"Status": {
					"name": "Status",
					"confirmationStatus": "NONE"
				},
				"Comment": {
					"name": "Comment",
					"confirmationStatus": "NONE"
				},
				"ReasonForVisit": {
					"name": "ReasonForVisit",
					"confirmationStatus": "NONE"
				},
				"Time": {
					"name": "Time",
					"confirmationStatus": "NONE"
				},
				"EmergencyType": {
					"name": "EmergencyType",
					"confirmationStatus": "NONE"
				},
				"PatientNumber": {
					"name": "PatientNumber",
					"confirmationStatus": "NONE"
				},
				"Day": {
					"name": "Day",
					"confirmationStatus": "NONE"
				}
			}
		},
		"dialogState": "STARTED"
	}

我知道了

---

- 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 }}"

答案 1 :(得分:-1)


  • 主机:全部 remote_user:root collect_facts:错误

    任务:

    • 名称:获取Java版本 外壳:java -version 2>&1 | grep构建| awk -F“ \ n”'{print $ 1}' 更改时间:错误 注册:java_result

    • 调试: 信息:“ {{java_result.stdout}}”