1.1 構(gòu)建目錄結(jié)構(gòu)
此操作是安裝nginx+mysql+tomcat+db的目錄結(jié)構(gòu),可以參考一下,不錯(cuò)~
mkdir -p /ansible/roles/{nginx,mysql,tomcat,db}/{defaults,files,handlers,meta,tasks,templates,vars}
- defaults 默認(rèn)尋找路徑
- tasks 存放playbooks路徑
- files 存放文件和腳本包,copy模塊文件搜索路徑
- templates 模版存放路徑
- handlers notify調(diào)用部分playbook存放路徑
- vars roles內(nèi)變量存放路徑
1.2 文件目錄結(jié)構(gòu)
[root@qtbackup ~]# tree /ansible/
/ansible/
├── playbook
│ └── playbook.yml
├── roles
│ ├── db
│ │ ├── defaults
│ │ ├── files
│ │ ├── handlers
│ │ ├── meta
│ │ ├── tasks
│ │ ├── templates
│ │ └── vars
│ ├── mysql
│ │ ├── defaults
│ │ ├── files
│ │ ├── handlers
│ │ ├── meta
│ │ ├── tasks
│ │ ├── templates
│ │ └── vars
│ ├── nginx
│ │ ├── defaults
│ │ ├── files
│ │ ├── handlers
│ │ ├── meta
│ │ ├── tasks
│ │ ├── templates
│ │ └── vars
│ └── tomcat
│ ├── defaults
│ ├── files
│ │ ├── apache-tomcat-8.0.29.tar.gz
│ │ ├── jdk-8u73-linux-x64.gz
│ │ ├── tomcat-initscript.sh
│ │ └── tomcat-users.xml
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ │ └── tomcat-users.xml
│ └── vars
├── ssh_key.sh ###自動(dòng)分發(fā)sshkey腳本 (下邊會(huì)完整貼出來)
├── web.retry
└── web.yml ####總調(diào)用文件
34 directories, 11 files
[root@qtbackup ~]#
1.3 在ansible目錄創(chuàng)建一個(gè)總調(diào)用文件
- hosts: bgo ####這個(gè)是hosts定義個(gè)主機(jī)組名字,如果是單臺(tái)主機(jī)可以直接寫地址
remote_user: root ####執(zhí)行用戶
roles: #####解釋配置文件目錄,因?yàn)槲覀冞@次裝的是tomcat所以注釋其他的
# - nginx
# - mysql
- tomcat
# - db
1.4 創(chuàng)建tomcat安裝playbook文件
vim /ansible/roles/tomcat/tasks/main.yml
#這個(gè)是用yum模塊安裝jdk,有需要的可以用
#- name: install java
# yum: name=java-1.8.0-openjdk.x86_64 state=present
===============================================================
#創(chuàng)建用戶
- name: group
group: name=tomcat
- name: user
user: name=tomcat group=tomcat home=/usr/tomcat
sudo: True
##############################這個(gè)源碼包安裝JDK#############################
#復(fù)制jdk到tmp目錄下
- name: copy jdk-8u73-linux-x64.gz
copy: src=jdk-8u73-linux-x64.gz dest=/tmp/jdk-8u73-linux-x64.gz
#解壓jdk包到/application
- name: Extract archive jdk
command: /bin/tar xf /tmp/jdk-8u73-linux-x64.gz -C /application
#改名
- name: java
shell: mv /application/jdk1.8.0_73 /application/java
#添加環(huán)境變量
- name: add /etc/profile
lineinfile: dest=/etc/profile regexp="^JAVA_HOME=" line="JAVA_HOME=/application/java/"
- name: add /etc/profile
lineinfile: dest=/etc/profile regexp="^CLASS_PATH=" line="CLASS_PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib"
- name: add /etc/profile
lineinfile: dest=/etc/profile regexp="^PATH=\$PATH:\$JAVA_HOME" line="PATH=$PATH:$JAVA_HOME/bin"
- name: add /etc/profile
lineinfile : dest=/etc/profile regexp="^export JAVA_HOME" line="export JAVA_HOME"
##########################安裝tomcat###########################################
- name: copy tomcat_tar_gz
copy: src=apache-tomcat-8.0.29.tar.gz dest=/tmp/apache-tomcat-8.0.29.tar.gz
#解壓tomcat到opt目錄
- name: Extract archive
command: /bin/tar xf /tmp/apache-tomcat-8.0.29.tar.gz -C /opt
#創(chuàng)建軟連接
- name: Symlink install directory
file: src=/opt/apache-tomcat-8.0.29/ dest=/application/tomcat state=link
#賦予目錄權(quán)限
- name: Change ownership of Tomcat installation
file: path=/application/tomcat/ owner=tomcat group=tomcat state=directory recurse=yes
#推送配置文件
- name: Configure Tomcat users
template: src=tomcat-users.xml dest=/application/tomcat/conf/
notify: restart tomcat
#安裝tomcat。init啟動(dòng)腳本
- name: Install Tomcat init script
copy: src=tomcat-initscript.sh dest=/etc/init.d/tomcat mode=0755
#開啟tomcat
- name: Start Tomcat
service: name=tomcat state=started enabled=yes
1.5 通過notify調(diào)用handlers
- name: restart tomcat
service: name=tomcat state=restarted
1.6 語法檢測(cè)===執(zhí)行安裝
cd /ansible
ansible-playbook web.yml --syntax-check #檢查語法
ansible-playbook web.yml #執(zhí)行
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。