ansible-role安装nginx,keepalived,tomcat

Architecture

 

roles Directory Structure

[root @ localhost the Roles] # Tree 
. 
├── keepalived 
│ ├── Files 
│ ├── handlers 
│ │ └── main.yml 
│ ├── Tasks 
│ │ └── main.yml 
│ ├── Templates 
│ │ ├── backup_keepalived.conf.j2 # keepalived master configuration file 
│ │ └── master_keepalived.conf.j2 # keepalived from the configuration file 
│ └── VARS 
│ └── main.yml 
├── nginx 
│ ├── default 
└── main.yml │ │ 
│ ├── files 
│ │ ├──-1.16.0.tar.gz #nginx nginx source package 
│ │ └── nginx.service #systemctl takeover nginx service file 
│ ├── handlers 
└── main.yml │ │ 
│ ├── Meta 
│ │ └── main.yml 
│ ├── Tasks
└── main.yml │ │ 
│ ├── Templates 
│ │ └── nginx.conf.ji2 #nginx Profile 
│ └── VARS 
│ └── main.yml 
├── site.yml 
└── Tomcat 
    ├ default - the 
    │ └── main.yml 
    ├── Files 
    │ ├── the Apache-Tomcat-8.5.37.tar.gz #tomcat binary package 
    │ └── jdk-8u144-linux- x64.gz #jdk binary packages 
    handlers ├── 
    │ └── main.yml 
    ├── Meta 
    │ └── main.yml 
    ├── Tasks 
    │ └── main.yml 
    ├── Templates 
    │ ├── java.sh.j2 # initial java environment variable file 
    │ ├── setclasspath.sh.j2 #tomcat set environment variables java script
    │ └── tomcat.service.j2 #systemctl take over the tomcat service file 
    └── VARS 
        └── main.yml

  

nginx roles

  • Tasks / main.yml
# Install gcc Environment 
- name: the install gcc the env 
  yum: name = {{Item}} State = Present 
  with_items: 
  - gcc 
  - Patch 
  - libffi-devel 
  - Python-devel 
  - zlib-devel 
  - bzip2-devel 
  - OpenSSL-devel 
  - the ncurses -devel 
  - SQLite-devel 
  - the readline-devel 
  - TK-devel 
  - gdbm-devel 
  - the libpcap-devel 
  - an xz-devel 
  - OpenSSL 
  - OpenSSL-devel 

# copies nginx source package to the target machine 
- name: UnArchive nginx.tar.gz 
  UnArchive : nginx-1.16.0.tar.gz src = dest = / tmp / 
  Tags: 
  - Jieya 

# compiler nginx 
- name: bianyi nginx
  shell: cd /tmp/nginx-1.16.0 && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module && make && make install
  tags:
  - bianyi

#复制systemctl接管nginx的服务文件
- name: copy nginx.service
  copy: src=nginx.service dest=/usr/lib/systemd/system/nginx.service

#重载systemd
- name: systemctl daemon-reload  
  shell: /usr/bin/systemctl daemon-reload

#复制nginx配置文件
- name: copy nginx.conf
  template: src=nginx.conf.ji2 dest=/usr/local/nginx/conf/nginx.conf
  notify: 
  - reload-nginx
  tags:
  - reload nginx

#启动nginx
- name: start nginx
  service: name=nginx state=started enabled=true  
  • handlers / main.yml
- name: reload-nginx
  shell: name=nginx state=reloaded
  • templates / main.yml: This configuration file needs more of their business correspondence change, do not copy the following configuration file, mainly to replace the contents inside
worker_processes  {{ ansible_processor_vcpus }};
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream tomcat {
        server {{ TOMCAT_SERVER1 }};
        server {{ TOMCAT_SERVER2 }};
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
            proxy_pass http://tomcat;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
  • VARs / main.yml
TOMCAT_SERVER1: 192.168.2.5:8080
TOMCAT_SERVER2: 192.168.2.6:8080

 

Tomcat roles

  • Tasks / main.yml 
# Jdk copy to the target host 
- name: cp jdk1.8 
  UnArchive: src = jdk-8u144-Linux-x64.gz dest = / usr 

# Create link 
- name: jdk Setup 
  File: src = / usr / jdk1.8.0_144 path the JAVA_HOME} {{} = = Link State 

# configure java script environment variables 
- name: JDK the env 
  Template: dest = java.sh.j2 the src = / etc / the profile.d / java.sh 

# loading environment variables 
- name: source java 
  shell: Source /etc/profile.d/java.sh 

# tomcat copy to the target host 
- name: cp tomcat 
  UnArchive: src = 8.5.37.tar.gz the Apache-tomcat-dest = / usr / 

# create link 
- name: Setup tomcat 
  File: the src = / usr / Apache tomcat-8.5.37-path = {{}} CATALINA_HOME Link State = 

# configure tomcat environment variables 
- name: tomcat env
  Template: setclasspath.sh.j2 dest = src =}} {{CATALINA_HOME / bin / setclasspath.sh 
  
# systemd file replication service to take over the tomcat 
- name: cp tomcat.service 
  Template: tomcat.service.j2 src = dest = / usr /lib/systemd/system/tomcat.service 

# overloaded systemd 
- name: daemonreload 
  shell: / usr / bin / systemctl daemonreload 

# start Tomcat 
- name: start Tomcat 
  Service: Tomcat State name = = = started Enabled to true
  • templates/java.sh.j2
export JAVA_HOME={{ JAVA_HOME }}
export PATH=$JAVA_HOME/bin:$PATH
  • /templates/setclasspath.sh.j2
#! / bin / SH 
Export the JAVA_HOME the JAVA_HOME = {{}} 
# The Licensed to the Apache Software Foundation (the ASF) an under One or More 
# ..... 
# ..... 
# only need to configure java_home, other default
  • templates/tomcat.service.j2
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart={{ CATALINA_HOME }}/bin/startup.sh
ExecStop={{ CATALINA_HOME }}/bin/shutdown.sh
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  • VARs / main.yml
JAVA_HOME: /usr/java
CATALINA_HOME: /usr/tomcat

  

 Keepalived roles

  • Tasks / main.yml
#安装keepalived
- name: install keepalived
  yum: name=keepalived state=present

#复制主配置文件
- name: copy master_conf
  template: src=master_keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf
  when:  ansible_default_ipv4.address  == "192.168.2.3"
  notify:
  - restart keepalived

#复制从配置文件
- name: copy backup_conf
  template: src=backup_keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf
  when:  ansible_default_ipv4.address  == "192.168.2.4"
  notify:
  - restart keepalived

#重启keepalived
- name: start keepalived
  service: name=keepalived state=started
  • templates/master_keepalived.conf.j2
! Configuration File for keepalived
 
global_defs {
   #notification_email {
   #  [email protected]
   #  [email protected]
   #  [email protected]
   #}
   #notification_email_from [email protected]
   #smtp_server 192.168.200.1
   #smtp_connect_timeout 30
   router_id {{ master_router_id }}
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
 
vrrp_instance VI_1 {
    state MASTER
    interface {{ ansible_default_ipv4.alias }}
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        {{ virtual_ipaddress }}
    }
}
  • templates/backup_keepalived.conf.j2
! Configuration File for keepalived
 
global_defs {
   #notification_email {
   #  [email protected]
   #  [email protected]
   #  [email protected]
   #}
   #notification_email_from [email protected]
   #smtp_server 192.168.200.1
   #smtp_connect_timeout 30
   router_id {{ backup_router_id }}
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
 
vrrp_instance VI_1 {
    state BACKUP
    interface {{ ansible_default_ipv4.alias }}
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        {{ virtual_ipaddress }}
    }
}
  • VARs / main.yml
virtual_ipaddress: 192.168.2.88

master_router_id: MA
master_priority: 100
master_ipaddress: 192.168.2.3
master_state: MASTER

backup_router_id: BA
backup_priority: 99
backup_ipaddress: 192.168.2.4
backup_state: BACKUP
  • handlers / main.yml
- name: restart keepalived
  service: name=keepalived state=restarted

  

Guess you like

Origin www.cnblogs.com/forlive/p/12591761.html