The role of the automated operation and maintenance tool Ansible-playbook (5)

1. Role introduction

Ansible has introduced new features since version 1.2 for hierarchical and structural organization of playbooks. Roles can automatically load variable files, tasks, and handlers according to the hierarchical structure. To use roles, you only need to use the include directive in the playbook. Simply put, roles are a mechanism by which variables, files, tasks, templates, and processors are placed in separate directories, and they can be included conveniently. Roles are generally used in scenarios where services are built based on the host structure, but they can also be used in scenarios such as building daemons.

Ansible's roles directory structure

project/   项目名称,有以下子目录
	tasks/  定义task、role的基本元素,至少包含一个名为main.yaml的文件;其他需要的文件在此通过include进行包含
	files/  存放由copy或者script模块调用的文件
	vars/ 不常用,定义变量;至少包含一个名为main.yaml的文件;其他需要的文件在此通过include进行包含
	default/ 不常用,设定默认变量时使用此目录中的main.yaml
	templates/  template模块查找所需要的模板问文件的目录
	handlers/
	meta/  不常用,定义当前角色的特殊设定以及依赖关系

2. Case

Deploy nginx on one of the servers, deploy tomcat on the two servers, and use nginx as a reverse proxy to implement polling access to services.
Insert picture description here

2.1 The total entry file of the role

cat install.yml

- hosts: 10.99.200.110
  remote_user: root
  gather_facts: false
  roles:
   - nginx

- hosts: test
  remote_user: root
  gather_facts: false
  roles:
   - tomcat

2.2 nginx deployment

Insert picture description here

step1: define variables

cat vars / main.yml

# defile nginx deploy host ip
nginx_ip: '10.99.200.110'
nginx_port: 10086
nginx_core: 4
nginx_user: 'root'

tomcat_server: 'TMS'
tomcat_server1: '10.99.200.110'
tomcat_server2: '10.99.200.111'
tomcat_port1: 8080
tomcat_port2: 8080
step2: deploy nginx

cat main.yml

- name: unpress nginx
  unarchive: src=install_nginx-1.16.1.tar.gz dest=/tmp/
  tags:
  - unpress

- name: install nginx
  shell: cd /tmp/install_nginx-1.16.1 && bash install.sh

- name: replace nginx conf
  template: src=nginx.conf.ji2 dest=/usr/local/nginx/conf/nginx.conf
  notify:
  - reload-nginx
  tags:
  - reload nginx

- name: start nginx
  shell: /etc/init.d/nginx -s reload
step3: nginx configuration file template

cat templates/nginx.conf.ji2

user {
    
    {
    
     nginx_user }};
worker_processes  {
    
    {
    
     nginx_core }};

events {
    
    
    worker_connections  51200;
    accept_mutex on;
    multi_accept on;
    use epoll;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;
    client_max_body_size 500M;
    client_header_buffer_size 4k;
    server_tokens off;
    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  /usr/local/nginx/logs/access.log  main;
    send_timeout 60;
    tcp_nodelay     on;
    underscores_in_headers on;
    keepalive_timeout  120;
    gzip  on;
    gzip_min_length  10k;
    gzip_buffers     16 64k;
    gzip_http_version 1.1;
    gzip_comp_level 3;
    gzip_types       text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php application/javascript application/json;
    gzip_disable "MSIE [1-6]\.";
    gzip_vary on;

    proxy_temp_path /usr/local/nginx/ngx_cache 1 2;
    proxy_cache_path /usr/local/nginx/ngx_cache/temp levels=1:2 keys_zone=content:1024m inactive=2d max_size=10G;
    
    upstream {
    
    {
    
     tomcat_server }} {
    
    
      server {
    
    {
    
     tomcat_server1 }}:{
    
    {
    
     tomcat_port1 }}; 
      server {
    
    {
    
     tomcat_server2 }}:{
    
    {
    
     tomcat_port2 }}; 
    }
    server {
    
    
        listen       {
    
    {
    
     nginx_port }};
        server_name  {
    
    {
    
     nginx_ip }};
        location / {
    
    
            root   html;
            index  index.html index.htm;
            proxy_pass http://{
    
    {
    
     tomcat_server }};
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
    
    
            root   html;
        }
    }
include      /usr/local/nginx/conf/conf.d/*.conf;
}
step4: nginx trigger operation

cat handlers/main.yml

- name: reload-nginx
  shell: /etc/init.d/nginx -s reload

2.3 tomcat deployment

Insert picture description here

step1: Define the tomcat installation parameters

cat vars / main.yml

# defile tomcat deploy args
tomcat_path: "/u01/isi/application"
tomcat_version: "apache-tomcat-9.0.33"
step2: deploy tomcat

cat tasks/main.yml

# install tomcat
- name: unpress pacakges
  unarchive: src={
    
    {
    
     tomcat_version }}.tar.gz dest={
    
    {
    
     tomcat_path }}

- name: copy scripts for tomcat 
  copy: src=start.sh dest={
    
    {
    
     tomcat_path }}/{
    
    {
    
     tomcat_version }} mode=0755

- name: copy scripts for tomcat
  copy: src=stop.sh dest={
    
    {
    
     tomcat_path }}/{
    
    {
    
     tomcat_version }} mode=0755

- name: start tomcat
  shell: cd {
    
    {
    
     tomcat_path }}/{
    
    {
    
     tomcat_version }} && source /etc/profile && ./start.sh
step3: write tomcat startup script

cat files/start.sh

#!/bin/bash
cd /u01/isi/application/apache-tomcat-9.0.33
nohup ./bin/startup.sh &

cat files/stop.sh

#!/bin/bash
ps -ef | grep apache-tomcat-9.0.33 | grep -v grep | awk '{print $2}' | xargs kill -9

2.4 Detect and execute role file

ansible-playbook --check install.ymlInsert picture description here
ansible-playbook install.yml
Insert picture description here

2.5 Browser verification

Front-end browser verification Insert picture description here
Back-end log verification
Insert picture description here

3. File acquisition

链接:https://pan.baidu.com/s/1sHN790EMUISV1P9Okqwa_A 
提取码:juey 

It's all here, scan it!
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44729138/article/details/114943658