参数化构建过程----文本参数

 

 构建---执行shell

#!/bin/bash
####判断IP是否在hosts列表里
cat /etc/ansible/hosts | grep -v ^# | awk  '{print $1}' | grep ${host_ip}
if [ $? -eq 0 ]
   then
       echo -e "\033[34m此IP在hosts列表,可以执行soft安装\033[0m"
   else
       echo -e "\033[31m确定IP,先添加到hosts文件\033[0m"
       exit 1
fi
####安装包拷贝到目标服务器
case ${install_jdk} in
    Yes)
#        ansible ${host_ip} -m copy -a "src=${jdk_file} dest=/usr/local/src"
        ansible-playbook /etc/ansible/jdk_install.yml -e host_ip=${host_ip}
        echo -e "\033[34mjdk安装完成\033[0m"
        ;;
    No)
        echo -e "\033[31m略过jdk安装\033[0m"
        ;;
esac
case ${install_nginx} in
    Yes)
#        ansible ${host_ip} -m copy -a "src=${nginx_file} dest=/usr/local/src"
        ansible-playbook /etc/ansible/nginx_install.yml -e host_ip=${host_ip}
         echo -e "\033[34mjnginx安装完成\033[0m"
        ;;
    No)
        echo -e "\033[31m略过nginx安装\033[0m"
        ;;
esac
case ${install_tomcat} in
    Yes)
#        ansible ${host_ip} -m copy -a "src=${tomcat_file} dest=/usr/local/src"
        ansible-playbook /etc/ansible/tomcat_install.yml -e host_ip=${host_ip}
         echo -e "\033[34mtomcat安装完成\033[0m"
        ;;
    No)
        echo -e "\033[31m略过Tomcat安装\033[0m"
        ;;
esac
case ${install_filebeat} in
    Yes)
#        ansible ${host_ip} -m copy -a "src=${tomcat_file} dest=/usr/local/src"
        ansible-playbook /etc/ansible/filebeat_install.yml -e host_ip=${host_ip}
         echo -e "\033[34mfilebeat安装完成\033[0m"
        ;;
    No)
        echo -e "\033[31m略过filebea安装\033[0m"
        ;;
esac
    

一、到Jenkins服务器上,安装ansible配置ansible-roles

这里要把jdk、tomcat、nginx、filebeat安装包放在/usr/local/src下

[root@srv-ansible]#cd /etc/ansible

[root@srv-ansible]# vim filebeat_install.yml 

- hosts: "{{ host_ip }}"
  remote_user: root
  roles:
    - filebeat_install

[root@srv ansible]# vim jdk_install.yml 

- hosts: "{{  host_ip }}"
  remote_user: root
  roles:
    - jdk_install

[root@srv ansible]# vim tomcat_install.yml 

- hosts: "{{ host_ip }}"
  remote_user: root
  roles:
    - tomcat_install

[root@srv ansible]# vim tomcat_install.yml 

- hosts: "{{ host_ip }}"
  remote_user: root
  roles:
    - tomcat_install

二、cd roles创建ansible剧本

[root@srv roles]# cd filebeat_install/tasks/
[root@srv tasks]# ls
filebeat_install.yml  main.yml
[root@srv tasks]# cat filebeat_install.yml 
- name: 上传tomcat文件到目标服务器
  copy: src=/usr/local/src/filebeat-7.10.2-x86_64.rpm dest=/usr/local/src/
- name: 安装filebeat
  command: rpm -ivh /usr/local/src/filebeat-7.10.2-x86_64.rpm
#- name: 启动服务
#  command: systemctl restart filebeat
- name: 启动服务
  systemd:
    name: filebeat
    state: restarted
    enabled: yes
[root@srv tasks]# cd ../../jdk_install/tasks/
[root@srv tasks]# ls
jdk_install.yml  main.yml
[root@srv tasks]# cat jdk_install.yml 
- name: 上传JDK文件到目标服务器并解压
  unarchive: src=/usr/local/src/jdk-8u201-linux-x64.tar.gz dest=/usr/local/src/
- name: 拷贝jdk到/usr/local
  command: chdir=/usr/local/src/ cp -r jdk1.8.0_201 /usr/local/jdk_1.8
- name: 设置java环境变量
  lineinfile: path=/etc/profile line={{ item }}
  with_items:
    - "#java"
    - "export JAVA_HOME=/usr/local/jdk_1.8" 
    - "export JRE_HOME=/usr/local/jdk_1.8/jre"
    - "export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/bin:./"
    - "export PATH=$JAVA_HOME/BIN:$JRE_HOME/bin:$PATH"
[root@srv tasks]# cat main.yml 
- include: jdk_install.yml
[root@srv tomcat_install]# cd ../tomcat_install/tasks/
[root@srv tasks]# ls
main.yml  tomcat_install.yml
[root@srv tasks]# cat tomcat_install.yml 
- name: 上传tomcat文件到目标服务器
  unarchive: src=/usr/local/src/apache-tomcat-8.5.71.tar.gz dest=/usr/local/src/
- name: 拷贝tomcat文件到/usr/local
  command: chdir=/usr/local/src cp -r apache-tomcat-8.5.71 /usr/local/tomcat
[root@srv tasks]# cat main.yml 
- include: tomcat_install.yml 
[root@srv tasks]# cd ../../nginx_install/tasks/
[root@srv tasks]# ls
main.yml  nginx_conf.yml  nginx_install.yml
[root@srv tasks]# cat nginx_install.yml 
- name: 上传nginx文件到目标服务器并解压
  unarchive: src=/usr/local/src/openresty-1.19.3.2.tar.gz dest=/usr/local/src/
- name: 编译nginx到/usr/local/
  shell: "{{ item }}"
  with_items:
    - "./configure"
    - make
    - make install
  args:
    chdir: "/usr/local/src/openresty-1.15.8.1"
[root@srv tasks]# cat nginx_conf.yml 
#- name: 设置nginx的配置路径
#  lineinfile: path=/usr/local/nginx/conf/nginx.conf line={{ item }}
#  with_items:
#    - include conf.d/*.conf;
- name: 设置nginx的配置路径
  shell: "{{ item }}"
  with_items:
    - sed -i '117 i include conf.d/*.conf;' /usr/local/openresty/nginx/conf/nginx.conf
- name: 创建nginx congfig文件
  file: path=/usr/local/openresty/nginx/conf/conf.d state=directory
- name: 创建nginx 证书文件
  file: path=/usr/local/openresty/nginx/conf/cert state=directory
[root@srv tasks]# cat main.yml 
- include: nginx_install.yml
- include: nginx_conf.yml

Logo

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

更多推荐