ansible 部署jdk
阅读原文时间:2023年07月16日阅读:3

playbook 剧本如下

[root@sz_fy_virt_encrypt_33_239 x]# cat jdk.yml

  • hosts: web
    remote_user: opsadmin
    become: yes
    become_method: sudo
    become_user: root
    tasks:

    • name: copy jdk to remote hosts
      copy: src=jdk-8u45-linux-x64.tar.gz dest=/tmp/

    • name: tar jdk
      shell: tar xf /tmp/jdk-8u45-linux-x64.tar.gz -C /usr/local/

    • name: link
      file: src=/usr/local/jdk1..0_45 dest=/usr/local/jdk state=link

    • name: java_env
      shell: /bin/echo {{ item }} >> /etc/profile && source /etc/profile
      with_items:

      • export JAVA_HOME=/usr/local/jdk
      • export JRE_HOME=/usr/local/jdk/jre
      • export CLASSPATH=.:\$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:\$JRE_HOME/lib:\$CLASSPATH
      • export PATH=\$JAVA_HOME/bin:\$PATH

/etc/ansible/hosts

192.168.11.172 ansible_user=opsadmin ansible_sudo_pass="x" ansible_ssh_port=12600
192.168.11.173 ansible_user=opsadmin ansible_sudo_pass="x" ansible_ssh_port=12600
192.168.11.174 ansible_user=opsadmin ansible_sudo_pass="x" ansible_ssh_port=12600

ansible-playbook --syntax-check jdk.yml

ansible-playbook -C jdk.yml  #tar 命令  用-C 检测参数 看来是模拟不了

正式执行结果如下

[opsadmin@sz x]$ ansible-playbook jdk.yml

PLAY [web] *******************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************
ok: [192.168.11.173]
ok: [192.168.11.174]
ok: [192.168.11.172]

TASK [copy jdk to remote hosts] **********************************************************************************************************************************************************************************************************************************************
changed: [192.168.11.174]
changed: [192.168.11.172]
changed: [192.168.11.173]

TASK [tar jdk] ***************************************************************************************************************************************************************************************************************************************************************
[WARNING]: Consider using the unarchive module rather than running tar. If you need to use command because unarchive is insufficient you can add warn=False to this command task or set command_warnings=False in ansible.cfg to get rid of this message.

changed: [192.168.11.174]
changed: [192.168.11.173]
changed: [192.168.11.172]

TASK [link] ******************************************************************************************************************************************************************************************************************************************************************
changed: [192.168.11.172]
changed: [192.168.11.174]
changed: [192.168.11.173]

TASK [java_env] **************************************************************************************************************************************************************************************************************************************************************
changed: [192.168.11.172] => (item=export JAVA_HOME=/usr/local/jdk)
changed: [192.168.11.173] => (item=export JAVA_HOME=/usr/local/jdk)
changed: [192.168.11.174] => (item=export JAVA_HOME=/usr/local/jdk)
changed: [192.168.11.172] => (item=export JRE_HOME=/usr/local/jdk/jre)
changed: [192.168.11.173] => (item=export JRE_HOME=/usr/local/jdk/jre)
changed: [192.168.11.174] => (item=export JRE_HOME=/usr/local/jdk/jre)
changed: [192.168.11.172] => (item=export CLASSPATH=.:\$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:\$JRE_HOME/lib:\$CLASSPATH)
changed: [192.168.11.174] => (item=export CLASSPATH=.:\$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:\$JRE_HOME/lib:\$CLASSPATH)
changed: [192.168.11.173] => (item=export CLASSPATH=.:\$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:\$JRE_HOME/lib:\$CLASSPATH)
changed: [192.168.11.172] => (item=export PATH=\$JAVA_HOME/bin:\$PATH)
changed: [192.168.11.174] => (item=export PATH=\$JAVA_HOME/bin:\$PATH)
changed: [192.168.11.173] => (item=export PATH=\$JAVA_HOME/bin:\$PATH)

PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
192.168.11.172 : ok= changed= unreachable= failed=
192.168.11.173 : ok= changed= unreachable= failed=
192.168.11.174 : ok= changed= unreachable= failed=

检查结果