root@jenkins:~/ansible/java/jdk-install# cat jdk.yml 
- hosts: '{{ server }}'
  tasks:
  - name: "拷贝安装包到远程"
    copy: src=/root/ansible/java/jdk-install/package/openjdk-11.0.2_linux-x64_bin.tar.gz  dest=/usr/local/
  - name: "解压jdk安装包"
    shell: chdir=/usr/local tar zxf openjdk-11.0.2_linux-x64_bin.tar.gz
  - name: "重命名jdk"
    shell: mv /usr/local/jdk-11.0.2 /usr/local/jdk11
  - name: "添加环境变量到/etc/profile中"
    shell: /bin/echo {{ item }} >> /etc/profile
    with_items:
        - export JAVA_HOME=/usr/local/jdk11
        - export CLASSPATH=\$JAVA_HOME/lib/
        - export PATH=\$PATH:\$JAVA_HOME/bin
 
  - name: "加载配置文件"
    shell: source /etc/profile
    args:
          executable: /bin/bash

  - name: "创建一个超链接为java11"
    shell: ln -s /usr/local/jdk/bin/java /usr/bin/java
  - name: "测试jdk是否生效"
    shell: java -version

运行

在jenkins配置一个任务,写个脚本构建,这就完事了

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

import os


def deploy_package(ip, package):
    if package == 'jdk11':
        print(f'cd /root/ansible/java/jdk-install/ && ansible-playbook -s jdk.yml -e server={ip}')
        os.system(f'cd /root/ansible/java/jdk-install/ && ansible-playbook -s jdk.yml -e server={ip}')
    elif package == 'nginx':
        print(f'cd /root/ansible/roles && ansible-playbook -s nginx.yaml -e server={ip}')
        os.system(f'cd /root/ansible/roles && ansible-playbook -s nginx.yaml -e server={ip}')


if __name__ == '__main__':
    server_id = os.getenv('server_id')
    package_name = os.getenv('package_name')
    deploy_package(server_id, package_name)
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐