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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
| [root@m01 webs]#cat tasks/main.yml ---
- name: Install Zabbix 6.0 repository shell: | rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/8/x86_64/zabbix-release-6.0-4.el8.noarch.rpm dnf clean all args: executable: /bin/bash register: repo_install
- name: Creata base repository yum_repository: name: nginx description: nginx-stable baseurl: http://nginx.org/packages/centos/7/$basearch/ gpgcheck: no enabled: yes
- name: Configu JDk rpm copy: src: "{{ item.src }}" dest: "{{ item.dest }}" loop: - { src: "{{ path }}jdk-8u181-linux-x64.rpm",dest: /root/jdk-8u181-linux-x64.rpm }
- name: Install Nginx PHP JDK Server yum: name: - nginx - php - php-bcmath - php-cli - php-common - php-devel - php-embedded - php-fpm - php-gd - php-intl - php-mbstring - php-mysqlnd - php-opcache - php-pdo - php-process - php-xml - php-json - /root/jdk-8u181-linux-x64.rpm state: present disable_gpg_check: yes
- name: Copy RPM files to target copy: src: "{{ item.src }}" dest: "{{ item.dest }}" loop: - { src: "{{ path1 }}zabbix-agent2-6.0.43-release2.el8.x86_64.rpm",dest: /tmp/zabbix-agent2-6.0.43-release2.el8.x86_64.rpm } - { src: "{{ path1 }}zabbix-agent2-plugin-mongodb-6.0.43-release2.el8.x86_64.rpm",dest: /tmp/zabbix-agent2-plugin-mongodb-6.0.43-release2.el8.x86_64.rpm } - { src: "{{ path1 }}zabbix-agent2-plugin-postgresql-6.0.43-release2.el8.x86_64.rpm",dest: /tmp/zabbix-agent2-plugin-postgresql-6.0.43-release2.el8.x86_64.rpm } - name: Install with yum localinstall shell: | yum localinstall -y /tmp/zabbix-agent2-*.rpm args: executable: /bin/bash
- name: Change directory ownership to www user file: path: /var/lib/php state: directory owner: www group: www recurse: yes
- name: Configu JDk.rpm nginx_conf.d copy: src: "{{ item.src }}" dest: "{{ item.dest }}" loop: - { src: "{{ path }}www.conf",dest: /etc/php-fpm.d/www.conf } - { src: "{{ path }}conf.d",dest: /etc/nginx } - { src: "{{ path }}backup.sh",dest: /root } notify: Restart Nginx PHP Tomcat Server
- name: Cron backup cron: name: backup minute: "0" hour: "2" job: "/root/backup.sh > /dev/null 2>&1" user: root state: present
- name: Create soft file: path: /soft state: directory
- name: unpackage Tomcat code unarchive: src: "{{ item.src }}" dest: "{{ item.dest }}" creates: "{{ item.creates }}" loop: - { src: "{{ path }}apache-tomcat-9.0.113.tar.gz", dest: /soft/, creates: /soft/apache-tomcat-9.0.113 } - { src: "{{ path }}code.tar.gz", dest: /, creates: /code } - { src: "{{ path }}redis-5.3.7.tgz", dest: /root, creates: /root/redis-5.3.7 }
- name: Create ln -s file: src: /soft/apache-tomcat-9.0.113 dest: /soft/tomcat state: link
- name: Creata Nginx PHP server template: src: "{{ item.src }}" dest: "{{ item.dest }}" mode: "{{ item.mode }}" loop: - { src: nginx.conf,dest: /etc/nginx/nginx.conf,mode: '0644' } - { src: tomcat.service,dest: /usr/lib/systemd/system/tomcat.service,mode: '0644' } - { src: server.xml,dest: /soft/tomcat/conf/server.xml,mode: '0600' } - { src: www.pwd,dest: /etc/www.pwd,mode: '0600' } - { src: php.ini,dest: /etc/php.ini,mode: '0644' } notify: Restart Nginx PHP Tomcat Server
- name: Compile and install Redis extensions shell: | cd /root/redis-5.3.7 && \ phpize && \ ./configure && \ make && \ make install
- name: 测试PHP配置 command: php-fpm -t register: php_test notify: Restart Nginx PHP Tomcat Server
- name: mount wp zh zrlog mount: src: "{{ item.src}}" path: "{{ item.path }}" fstype: nfs state: mounted loop: - { src: "172.16.1.31:/data/wp",path: /code/wp/wordpress/wp-content/uploads } - { src: "172.16.1.31:/data/zh",path: /code/zh/uploads/question } - { src: "172.16.1.31:/data/zrlog",path: /code/zrlog/ROOT/attached } notify: Restart Nginx PHP Tomcat Server
- name: Start Nginx PHP Tomcat Server systemd: name: "{{ item.name }}" state: started enabled: yes loop: - { name: nginx } - { name: php-fpm } - { name: tomcat }
- name: restart tomcat systemd: name: tomcat state: restarted
- name: Copy sh to web01 copy: src: "{{ item.src }}" dest: "{{ item.dest }}" mode: "{{ item.mode }}" loop: - { src: "{{ path }}setup_dual_gateway_persistent.sh",dest: /root/setup_dual_gateway_persistent.sh,mode: '0755'} - { src: "{{ path1 }}zabbix_agent2.conf1",dest: /etc/zabbix/zabbix_agent2.conf,mode: '0644' } when: inventory_hostname == '10.0.0.7' or ansible_host == '172.16.1.7' - name: Copy sh to web02 copy: src: "{{ item.src }}" dest: "{{ item.dest }}" mode: "{{ item.mode }}" loop: - { src: "{{ path }}setup_dual_gateway_persistent.sh2",dest: /root/setup_dual_gateway_persistent.sh,mode: '0755' } - { src: "{{ path1 }}zabbix_agent2.conf2",dest: /etc/zabbix/zabbix_agent2.conf,mode: '0644' } when: inventory_hostname == '10.0.0.8' or ansible_host == '172.16.1.8'
- name: Start zabbix-agent2 Server systemd: name: zabbix-agent2 state: started enabled: yes
- name: 执行配置脚本 shell: /root/setup_dual_gateway_persistent.sh register: result changed_when: "'配置完成' in result.stdout"
[root@m01 webs]#cat handlers/main.yml ---
- name: Restart Nginx PHP Tomcat Server systemd: name: "{{ item.name }}" state: restarted loop: - { name: 'nginx' } - { name: 'php-fpm' } - { name: 'tomcat' } when: > (item.name == 'nginx' and (ng_re is defined and ng_re.rc == 0)) or (item.name == 'php-fpm' and (php_re is defined and php_re.rc == 0)) or (item.name == 'tomcat' and (tomcat_config_check is defined and tomcat_config_check.rc == 0)
[root@m01 webs]#cat vars/main.yml ---
path: /ansible/roles/webs/templates/ path1: /ansible/roles/webs/files/
[root@m01 webs]#ll files/ total 13056 -rw-r--r-- 1 root root 6160420 Jan 7 16:33 zabbix-agent2-6.0.43-release2.el8.x86_64.rpm -rw-r--r-- 1 root root 14708 Jan 7 15:26 zabbix_agent2.conf1 -rw-r--r-- 1 root root 14708 Jan 7 15:26 zabbix_agent2.conf2 -rw-r--r-- 1 root root 3947240 Jan 7 16:33 zabbix-agent2-plugin-mongodb-6.0.43-release2.el8.x86_64.rpm -rw-r--r-- 1 root root 3222208 Jan 7 16:33 zabbix-agent2-plugin-postgresql-6.0.43-release2.el8.x86_64.rpm
[root@m01 webs]#ll templates/ total 253460 -rw-r--r-- 1 root root 13049663 Dec 3 03:56 apache-tomcat-9.0.113.tar.gz -rw-r--r-- 1 root root 76364324 Dec 25 17:45 code.tar.gz drwxr-xr-x 2 root root 180 Dec 18 16:15 conf.d -rw-r--r-- 1 root root 1229 Dec 25 20:53 conf.d.tar.gz -rw-r--r-- 1 root root 170023183 Sep 27 2024 jdk-8u181-linux-x64.rpm -rw-r--r-- 1 root root 1336 Dec 25 19:00 nginx.conf -rw-r--r-- 1 root root 62269 Dec 25 19:22 php.ini -rw------- 1 root root 2614 Dec 25 19:12 server.xml -rw-r--r-- 1 root root 320 Dec 25 17:47 tomcat.service -rw-r--r-- 1 root root 19404 Dec 25 17:52 www.conf [root@m01 webs]#cat templates/nginx.conf
user www; worker_processes auto; worker_cpu_affinity auto;
error_log /var/log/nginx/error.log notice; pid /run/nginx.pid;
events { use epoll; worker_connections 65535; }
http { include /etc/nginx/mime.types; default_type application/octet-stream; charset utf-8,gbk; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
server_tokens off; client_max_body_size 20m; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65;
gzip on; gzip_disable "MSIE [1-6]\."; gzip_http_version 1.1; gzip_comp_level 2; gzip_buffers 16 8k; gzip_min_length 1024; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/jpeg;
include /etc/nginx/conf.d/*.conf; }
[root@m01 webs]#cat templates/server.xml <?xml version="1.0" encoding="UTF-8"?> <Server port="8005" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <Listener className="org.apache.catalina.core.AprLifecycleListener" /> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources>
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" maxParameterCount="1000" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm>
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host>
<Host name="diy.oldboy.com" appBase="/code/diy/" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="diy.oldboy.com" suffix=".log" pattern="%h %l %u %t "%r" %s %b" /> </Host>
<Host name="www.zrlog.com" appBase="/code/zrlog/" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="zrlog" suffix=".log" pattern="%h %l %u %t "%r" %s %b" /> </Host> <Host name="session.oldboy.com" appBase="/session/" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="session" suffix=".log" pattern="%h %l %u %t "%r" %s %b" /> </Host>
</Engine> </Service> </Server>
root@m01 webs]#cat templates/tomcat.service [Unit] Description=Apache Tomcat Server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/soft/tomcat/bin/startup.sh ExecStop=/soft/tomcat/bin/shutdown.sh ExecRestart=/soft/tomcat/bin/shutdown.sh && sleep2 && /soft/tomcat/bin/startup.sh [Install] WantedBy=multi-user.target
|