实验总共三台机器 一台做为控制台 另外两台作为web使用
该项目的目录结构如下:
[root@TrackerServer servers]# tree . ├── ansible.cfg -> ./conf/ansible.cfg ├── conf │ ├── ansible.cfg │ └── hosts ├── group_vars │ └── all-servers ├── pkg.yml ├── roles │ ├── centos │ │ ├── files │ │ ├── handlers │ │ │ └── main.yml │ │ ├── tasks │ │ │ ├── main.yml │ │ │ ├── service.yml │ │ │ └── tools.yml │ │ └── templates │ │ ├── iptables-save │ │ └── sshd_config │ └── tomcat │ ├── files │ │ ├── apache-tomcat-7.0.63.tar.gz │ │ ├── install_native.sh │ │ ├── jdk-7u79-linux-x64.rpm │ │ ├── profile │ │ ├── tomcat │ │ └── tomcat-native-1.1.33-src.tar.gz │ ├── handlers │ │ └── main.yml │ ├── tasks │ │ ├── main.yml │ │ ├── tomcat-set.yml │ │ └── tomcat.yml │ └── templates │ ├── catalina.sh │ ├── iptables-save │ ├── server.xml │ └── tomcat-users.xml ├── ssh_keys │ ├── id_rsa │ └── id_rsa.pub └── website.yml
pkg.yml用来安装centos系统所需要的工具以及库内容如下:
[root@TrackerServer servers]# cat pkg.yml --- - hosts: test-servers gather_facts: False remote_user: root roles: - centos
website.yml用来批量安装tomcat的,内容如下:
[root@TrackerServer servers]# cat website.yml --- - hosts: test-servers gather_facts: False remote_user: root roles: - tomcat
我建立两个角色 一个用来更新centos的库以及相关的工具,一个用来安装tomcat
先来看更新centos的安装包,配置如下
[root@TrackerServer servers]# cat roles/centos/tasks/main.yml --- - include: service.yml - include: tools.yml [root@TrackerServer servers]# cat roles/centos/tasks/tools.yml --- - name: install environ tags: tools yum: name={{item}} state=latest with_items: - apr-devel - openssl-devel - make - gcc - gcc-c++ - tree - autoconf - automake - zlib-devel - python - perl - glibc - unzip - gzip - crontabs
然后执行安装或者更新的指令:
[root@TrackerServer servers]# ansible-playbook pkg.yml --tags "tools" PLAY [test-servers] *********************************************************** TASK: [centos | install environ] ********************************************** ok: [test-server2] => (item=apr-devel,openssl-devel,make,gcc,gcc-c++,tree,autoconf,automake,zlib-devel,python,perl,glibc,unzip,gzip,crontabs) ok: [test-server1] => (item=apr-devel,openssl-devel,make,gcc,gcc-c++,tree,autoconf,automake,zlib-devel,python,perl,glibc,unzip,gzip,crontabs) PLAY RECAP ******************************************************************** test-server1 : ok=1 changed=0 unreachable=0 failed=0 test-server2 : ok=1 changed=0 unreachable=0 failed=0
这就完了,也可通过ansible命令来进行操作 非常简单
再来看omcat 安装 管理等相关的配置
[root@TrackerServer servers]# ll roles/tomcat/files/ total 143884 -rw-r--r-- 1 root root 8850470 Feb 24 09:28 apache-tomcat-7.0.63.tar.gz -rwxr-xr-x 1 root root 218 Feb 24 15:42 install_native.sh -rw-r--r-- 1 root root 138082565 Feb 24 09:27 jdk-7u79-linux-x64.rpm -rw-r--r-- 1 root root 1910 Feb 24 15:42 profile -rwxr-xr-x 1 root root 1389 Feb 24 16:00 tomcat -rw-r--r-- 1 root root 388787 Feb 24 09:27 tomcat-native-1.1.33-src.tar.gz
这是我要安装tomcat所需要的源码包,安装tomcat-native脚本 tomcat的启动脚本,以及系统环境配置文件等
1,install_native.sh 是用来安装tomcat-native的内容如下:
[root@TrackerServer servers]# cat roles/tomcat/files/install_native.sh #!/bin/bash make distclean ./configure --with-apr=/usr/bin/apr-1-config \ --with-java-home=/usr/java/jdk1.7.0_79 \ --with-ssl=yes \ --prefix=/usr/local/tomcat make && make install [root@TrackerServer servers]#
2,tomcat是启动脚本 内容如下:
#!/bin/bash # # chkconfig: 345 99 28 # description: Starts/Stops Apache Tomcat export CATALINA_HOME=/usr/local/tomcat export CATALINA_BASE=/usr/local/tomcat TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}" tomcat_pid() { echo `ps -fe | grep $CATALINA_BASE | grep -v grep | tr -s " "|cut -d" " -f2` } start() { pid=$(tomcat_pid) if [ -n "$pid" ];then echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m" else echo -e "\e[00;32mStarting tomcat\e[00m" sh $CATALINA_HOME/bin/startup.sh status fi return 0 } status(){ pid=$(tomcat_pid) if [ -n "$pid" ]; then echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m" else echo -e "\e[00;31mTomcat is not running\e[00m" fi } stop() { pid=$(tomcat_pid) if [ -n "$pid" ];then echo -e "\e[00;31mStoping Tomcat\e[00m" sh $CATALINA_HOME/bin/shutdown.sh else echo -e "\e[00;31mTomcat is not running\e[00m" fi return 0 } case $1 in start) start ;; stop) stop ;; restart) stop sleep 5s start ;; status) status ;; *) echo -e $TOMCAT_USAGE ;; esac exit 0
3,profile(路径/etc/profile)是用来设置native生成后的库路径 (去tomcat官网看吧)
然后看下任务目录中的配置:
1,安装jdk tomcat 以及tomcat-native的配置都存储在该文件里
[root@TrackerServer servers]# cat roles/tomcat/tasks/main.yml
---
- include: tomcat.yml
- include: tomcat-set.yml
[root@TrackerServer servers]# cat roles/tomcat/tasks/tomcat.yml
---
- name: upload jdk1.7
copy: src=jdk-7u79-linux-x64.rpm dest={{dest_dir}} owner=root group=root mode=644
tags: tomcat
- name: be sure jdk1.7 installed
yum: pkg=/usr/local/src/jdk-7u79-linux-x64.rpm state=installed
tags: tomcat
- name: upload or sync tomcat-native
copy: src=tomcat-native-1.1.33-src.tar.gz dest={{dest_dir}}
tags: tomcat
- name: upload or sync tomcat1.7
copy: src=apache-tomcat-7.0.63.tar.gz dest={{dest_dir}}
tags: tomcat
- name: Extract tomcat1.7
shell: chdir={{dest_dir}} tar zxvf apache-tomcat-7.0.63.tar.gz -C /usr/local
tags: tomcat
- name: Extract tomcat-native
command: chdir={{dest_dir}} tar zxvf tomcat-native-1.1.33-src.tar.gz
tags: tomcat
- name: install directory
shell: chdir=/usr/local rm -rf tomcat && mv apache-tomcat-7.0.63 tomcat
tags: tomcat
- name: install tomcat-native scripts
copy: src=install_native.sh dest={{dest_dir}}/tomcat-native-1.1.33-src/jni/native mode=655
tags: tomcat
- name: install tomcat-natvie
shell: chdir={{dest_dir}}/tomcat-native-1.1.33-src/jni/native ./install_native.sh >> /dev/null
tags: tomcat
- name: isntall tomcat started script
copy: src=tomcat dest=/etc/init.d/ mode=0755
tags: tomcat
- name: add tomcat script to service
service: name=tomcat enabled=yes
tags: tomcat
2,tomcat的设置配置等都在改文件里如下:
[root@TrackerServer servers]# cat roles/tomcat/tasks/tomcat-set.yml
---
- name: config path environ
copy: src=profile dest=/etc/ mode=0644
tags: setpath
- name: command profile
shell: sh /etc/profile
tags: setpath
- name: config tomcat start scripts
copy: src=tomcat dest=/etc/init.d/ mode=0755
notify: restart tomcat
tags: tomcat-conf
- name: config tomcat started srcirpt environ
template: src=catalina.sh dest=/usr/local/tomcat/bin/
notify: restart tomcat
tags: tomcat-conf
- name: config tomcat server.xml
template: src=server.xml dest=/usr/local/tomcat/conf/
notify: restart tomcat
tags: tomcat-conf
- name: config tomcat-user.xml
template: src=tomcat-users.xml dest=/usr/local/tomcat/conf
notify: restart tomcat
tags: tomcat-conf
- name: wait for tomcat to start
wait_for: port={{http_port}} delay=15
tags: tomcat-conf
3,模板文件内容如下:
[root@TrackerServer servers]# ll roles/tomcat/templates/ total 36 -rwxr-xr-x 1 root root 20933 Feb 24 13:55 catalina.sh -rw-r--r-- 1 root root 0 Feb 19 09:04 iptables-save -rw-r--r-- 1 root root 6556 Feb 24 16:02 server.xml -rw-r--r-- 1 root root 1384 Feb 24 14:16 tomcat-users.xml
4,通报的任务如下:
[root@TrackerServer servers]# cat roles/tomcat/handlers/main.yml --- - name: restart tomcat service: name=tomcat sleep=6 state=restarted
全局变量在group_vars中设定
[root@TrackerServer servers]# cat group_vars/all-servers #配置service.xml http_port: 80 https_port: 443 #配置启动脚本参数 java_opts: '"-server -Xms1024m -Xmx1024m -XX:PermSize=512m -XX:MaxPermSize=512m"' #配置tomcat-user.xml admin_username: root admin_password: root12300. #配置sshd_config sshd_auth: 'no' sshd_dns: 'no' sshd_port: 22 #所有源码包和rpm包的存放路径 dest_dir: '/usr/local/src'
hosts主机信息:
[root@TrackerServer servers]# cat conf/hosts [test-servers] test-server1 ansible_ssh_host=192.168.0.128 test-server2 ansible_ssh_host=192.168.0.129 [web-servers] guanwang_serveransible_ssh_host=192.168.0.45 iecp_web_serveransible_ssh_host=192.168.0.198 imss_serveransible_ssh_host=192.168.0.224 osms_serveransible_ssh_host=192.168.0.226 olsm_serveransible_ssh_host=192.168.0.228 imss_wap_serveransible_ssh_host=192.168.0.193 wap_serveransible_ssh_host=192.168.0.164 boss_serveransible_ssh_host=192.168.0.215 img_ngx_Serveransible_ssh_host=192.168.0.238 [all-servers:children] test-servers web-servers
ok
1 现在开始安装:
root@TrackerServer servers]# ansible-playbook website.yml --tags "tomcat" PLAY [test-servers] *********************************************************** TASK: [tomcat | upload jdk1.7] ************************************************ changed: [test-server1] changed: [test-server2] TASK: [tomcat | be sure jdk1.7 installed] ************************************* ok: [test-server2] ok: [test-server1] TASK: [tomcat | upload or sync tomcat-native] ********************************* changed: [test-server1] changed: [test-server2] TASK: [tomcat | upload or sync tomcat1.7] ************************************* changed: [test-server2] changed: [test-server1] TASK: [tomcat | Extract tomcat1.7] ******************************************** changed: [test-server1] changed: [test-server2] TASK: [tomcat | Extract tomcat-native] **************************************** changed: [test-server2] changed: [test-server1] TASK: [tomcat | install directory] ******************************************** changed: [test-server2] changed: [test-server1] TASK: [tomcat | install tomcat-native scripts] ******************************** changed: [test-server2] changed: [test-server1] TASK: [tomcat | install tomcat-natvie] **************************************** changed: [test-server1] changed: [test-server2] TASK: [tomcat | isntall tomcat started script] ******************************** ok: [test-server1] ok: [test-server2] TASK: [tomcat | add tomcat script to service] ********************************* ok: [test-server2] ok: [test-server1] PLAY RECAP ******************************************************************** test-server1 : ok=11 changed=8 unreachable=0 failed=0 test-server2 : ok=11 changed=8 unreachable=0 failed=0
2,设置tomcat的环境变量:
[root@TrackerServer servers]# ansible-playbook website.yml --tags "setpath" PLAY [test-servers] *********************************************************** TASK: [tomcat | config path environ] ****************************************** ok: [test-server2] ok: [test-server1] TASK: [tomcat | command profile] ********************************************** changed: [test-server2] changed: [test-server1] PLAY RECAP ******************************************************************** test-server1 : ok=2 changed=1 unreachable=0 failed=0 test-server2 : ok=2 changed=1 unreachable=0 failed=0
3 管理,配置启动tomcat
[root@TrackerServer servers]# ansible-playbook website.yml --tags "tomcat-conf" PLAY [test-servers] *********************************************************** TASK: [tomcat | config tomcat start scripts] ********************************** ok: [test-server2] ok: [test-server1] TASK: [tomcat | config tomcat started srcirpt environ] ************************ changed: [test-server2] changed: [test-server1] TASK: [tomcat | config tomcat server.xml] ************************************* changed: [test-server2] changed: [test-server1] TASK: [tomcat | config tomcat-user.xml] *************************************** changed: [test-server2] changed: [test-server1] TASK: [tomcat | wait for tomcat to start] ************************************* ok: [test-server1] ok: [test-server2] NOTIFIED: [tomcat | restart tomcat] ******************************************* changed: [test-server2] changed: [test-server1] PLAY RECAP ******************************************************************** test-server1 : ok=6 changed=4 unreachable=0 failed=0 test-server2 : ok=6 changed=4 unreachable=0 failed=0
通过ansible查看tomcat是否启动:
[root@TrackerServer servers]# ansible test-servers -a 'service tomcat status' test-server2 | success | rc=0 >> [00;32mTomcat is running with pid: 4768[00m test-server1 | success | rc=0 >> [00;32mTomcat is running with pid: 4929[00m
完毕!!