Ansible-playbook 安装windows环境 JDK

使用Ansible在windows环境下安装Java

使用到的模块

  • win_copy: 复制软件安装程序到windows主机
  • win_package: 安装软件包
  • win_path: 添加环境变量
  • win_reboot: 重启windows服务器
  • win_shell: 检查java安装情况
  • debug: 输出检查结果

编写playbook

[root@squid win_java_install]# cat win_install_java.yaml 
---
- hosts: windows
  tasks:
  - name: copy jdk software to remote host
    win_copy: src=jdk-7u2-windows-x64.exe  dest=c:\\tools\\

  - name: install java
    win_package:
      path: c:\\tools\\jdk-7u2-windows-x64.exe
      product_id: jdk-7u2
      arguments:
        - /s
        - /quiet
      state: present

  - name: Add JAVA_HOME Env
    win_path:
      name: JAVA_HOME
      elements: C:\Program Files\Java\jdk1.7.0_02
      scope: machine
      state: present

  - name: Add JAVA_BIN Env
    win_path:
      name: PATH
      elements: C:\Program Files\Java\jdk1.7.0_02\bin
      scope: machine
      state: present

  - name: Reboot Windows Server
    win_reboot:
      msg: "Restart the server 3 seconds later"
      post_reboot_delay: 3
      test_command: whoami

  - name: check jdk shell
    win_shell: "java -version"
    args:
      executable: cmd
    register: java_version

  - debug: msg="{{java_version.stderr_lines}}"

执行playbook

[root@squid win_java_install]# ansible-playbook win_install_java.yaml 

PLAY [windows] ***************************************************************************************************

TASK [copy jdk software to remote host] ***************************************************************************************************
ok: [10.241.0.4]

TASK [install java] ***************************************************************************************************
changed: [10.241.0.4]

TASK [Add JAVA_HOME Env] ***************************************************************************************************
changed: [10.241.0.4]

TASK [Add JAVA_BIN Env] ***************************************************************************************************
changed: [10.241.0.4]

TASK [Reboot Windows Server] ***************************************************************************************************
changed: [10.241.0.4]

TASK [check jdk shell] ***************************************************************************************************
changed: [10.241.0.4]

TASK [debug] ***************************************************************************************************
ok: [10.241.0.4] => {
    "msg": [
        "java version \"1.7.0_02\"", 
        "Java(TM) SE Runtime Environment (build 1.7.0_02-b13)", 
        "Java HotSpot(TM) 64-Bit Server VM (build 22.0-b10, mixed mode)"
    ]
}

PLAY RECAP ***************************************************************************************************
10.241.0.4                 : ok=7    changed=5    unreachable=0    failed=0
    原文作者:baiyongjie
    原文地址: https://www.jianshu.com/p/402159137f1d
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞