使用Ansible2.0部署JDK8

最近需要在多台新服务器部署Java环境,想到要重复同样的事这么多次就头疼,后来突然想到运维神器Ansible可以自动化部署,并且仅依赖ssh,只要在一台服务器上安装Ansible就可以,所以就想用Ansible来解决,因为毕竟偏运维所以也不想深究什么原理架构之类的,还是以解决实际问题为主,写死路径,丑陋什么的都无所谓。

所以没跟着官网文档做,直接在网上找了一篇入门教程就干了起来。
根据网上的教程添加个hosts文件用于管理服务器ip与提取公共变量。

hosts
1
2
3
4
5
6
[local]
192.168.100.129
[dw]
192.168.100.130
192.168.100.131
192.168.100.132

然后开始写yml格式playbook。

main.yml
1
2
3
4
5
6
7
8
9
10
11
12
- name: mkdir
shell: mkdir -p /root/java/
- name: copy jdk to remote host
copy: src=jdk-8u161-linux-x64.tar.gz dest=/root/java/
- name: unzip jdk
shell: tar -zxf /root/java/jdk-8u161-linux-x64.tar.gz -C /root/java/
- name: set jdk_env copy use template
template: src=java_home.sh.j2 dest=/root/java/set_jdk.sh
- name: execute script to set jdkenv
shell: sh /root/java/set_jdk.sh
- name: source bash_profile
shell: source /root/.bash_profile

  • 在root新建文件夹。
  • 拷贝jdk到新建的文件夹。
  • 解压jdk。
  • 拷贝设置环境变量的脚本到新建的文件夹下。
  • 执行设置环境变量的脚本。
  • 使刚才设置的环境变量生效。

看起来没什么问题,于是直接执行。

1
ansible-playbook playbook/roles/java/tasks/main.yml

直接报错:

1
2
3
4
5
6
7
The error appears to have been in '/etc/ansible/playbook/roles/java/tasks/main.yml': line 1, column 1, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- name: mkdir
^ here

看错误是解析yml失败了,猜测可能是空格或tab的原因导致,查看了下yml官方要求是用一个空格,但脚本里也都是空格没有tab。

没办法最后只能翻官方文档,看了官方的例子发现playbook的脚本格式与网上的教程都不一样,所以当时想会不会是因为用的Ansible2.0版本格式规则全变了,又不向下兼容导致。
所以写了个新建文件的小例子测试下。果然,2.0的格式与低版本的不一样,并且不向下兼容老本的playbook。

完整正确的Ansible2.0部署JDK的例子是:

  • 1.约定优于配置的情况下,官方建议的playbook、文件等存放的目录结构应该是:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    /etc/ansible/
    ├── hosts
    └── playbook
    └── roles
    └── java
    └── tasks
    ├── files
    │   └── jdk-8u161-linux-x64.tar.gz
    ├── main.yml
    └── templates
    └── java_home.sh.j2
  • 2.playbook文件格式:

    main.yml
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    - hosts: dw
    tasks:
    - name: mkdir
    shell: mkdir -p /root/java/
    - name: copy jdk to remote host
    copy: src=jdk-8u161-linux-x64.tar.gz dest=/root/java/
    - name: unzip jdk
    shell: tar -zxf /root/java/jdk-8u161-linux-x64.tar.gz -C /root/java/
    - name: set jdk_env copy use template
    template: src=java_home.sh.j2 dest=/root/java/set_jdk.sh
    - name: execute script to set jdkenv
    shell: sh /root/java/set_jdk.sh
    - name: source bash_profile
    shell: source /root/.bash_profile

增加了hosts,用于指定部署的服务器组,并且原本独立的命令移动到tasks下。

  • 3.设置环境变量的脚本:

    java_home.sh.j2
    1
    2
    3
    4
    5
    #!/bin/bash
    echo 'export JAVA_HOME=/root/java/jdk1.8.0_161' >> /root/.bash_profile
    echo 'export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH' >> /root/.bash_profile
    echo 'export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar' >> /root/.bash_profile
    source ~/.bash_profile
  • 最后执行命令:

    1
    ansible-playbook playbook/roles/java/tasks/main.yml

控制台输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

PLAY [dw] ***********************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************************
ok: [192.168.100.131]
ok: [192.168.100.132]
ok: [192.168.100.130]

TASK [mkdir] ********************************************************************************************************************************************************************************************************
[WARNING]: Consider using the file module with state=directory rather than running mkdir. If you need to use command because file 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.100.130]
changed: [192.168.100.132]
changed: [192.168.100.131]

TASK [copy jdk to remote host] **************************************************************************************************************************************************************************************
changed: [192.168.100.132]
changed: [192.168.100.130]
changed: [192.168.100.131]

TASK [unzip 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.100.130]
changed: [192.168.100.131]
changed: [192.168.100.132]

TASK [set jdk_env copy use template] ********************************************************************************************************************************************************************************
changed: [192.168.100.130]
changed: [192.168.100.131]
changed: [192.168.100.132]

TASK [execute script to set jdkenv] *********************************************************************************************************************************************************************************
changed: [192.168.100.130]
changed: [192.168.100.131]
changed: [192.168.100.132]

TASK [source bash_profile] ******************************************************************************************************************************************************************************************
changed: [192.168.100.130]
changed: [192.168.100.131]
changed: [192.168.100.132]

PLAY RECAP **********************************************************************************************************************************************************************************************************
192.168.100.130 : ok=7 changed=6 unreachable=0 failed=0
192.168.100.131 : ok=7 changed=6 unreachable=0 failed=0
192.168.100.132 : ok=7 changed=6 unreachable=0 failed=0

执行成功,部署全部正确完成。