yum install gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools vim iotop bc zip unzip zlib-devel lrzsz tree screen lsof tcpdump wget ntpdate

    make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 USE_LUA=1 LUA_INC=/usr/local/src/lua-5.3.5/src/ LUA_LIB=/usr/local/src/lua-5.3.5/src

    make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 USE_LUA=1 LUA_INC=/usr/local/src/lua-5.3.5/src/ LUA_LIB=/usr/local/src/lua-5.3.5/src/ PREFIX=/usr/local/haproxy

    192.168.37.51 ansible
    52 vs1(n+k+l)
    53
    54 rs1(httpd)
    55
    56mariadb

    [root@an ~]# yum install ansible

    [root@an ~]# ssh-keygen -t rsa

    [root@an ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.37.52
    [root@an ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.37.53
    [root@an ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.37.54
    [root@an ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.37.55
    [root@an ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.37.56

    [root@an ~]# ssh 192.168.37.55
    [root@an ~]# vim /etc/ansible/hosts

    [web]
    192.168.37.54
    192.168.37.55
    [nginx]
    192.168.37.52 state=MASTER priority=100
    192.168.37.53 state=BACKUP priority=90 keepalived的变量
    [mysql]
    192.168.37.56

    [root@an ~]# ansible all -a date

    [root@an ~]# mkdir -pv /etc/ansible/roles/{mysql,web,nginx}/{files,tasks,templates,vars,handlers,meta}

    [root@an tasks]# cd /etc/ansible/roles/web/tasks/
    [root@an tasks]# vim main.yml

    • name: install web pakgs
      yum: name={{ item }}
      with_items:
      • httpd
      • php
      • php-mysql
    • name: config web
      copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
      notify: restart the service # 注意这里要与handlers里定义的name相同
    • name: copy wordpress
      synchronize: src=wordpress dest=/var/www/html/wordpress/
    • name: restart the service
      service: name=httpd state=started

    [root@an handlers]# cd /etc/ansible/roles/web/handlers/
    [root@an handlers]# vim main.yml

    • name: restart the service #就这
      service: name=httpd state=restarted
      [root@an handlers]# cd /etc/ansible/roles/web/files/
      [root@an files]# tar xf wordpress-5.0-zh_CN.tar.gz
      [root@an files]# cp /etc/httpd/conf/httpd.conf .
      [root@an files]# cd wordpress
      [root@an wordpress]# cp wp-config-sample.php wp-config.php
      [root@an wordpress]# vim wp-config.php

    define(‘DB_NAME’, ‘wp’);

    /* MySQL数据库用户名 /
    define(‘DB_USER’, ‘wpuser’);

    /* MySQL数据库密码 /
    define(‘DB_PASSWORD’, ‘centos’);

    /* MySQL主机 /
    define(‘DB_HOST’, ‘192.168.37.66’);

    [root@an wordpress]# cd /etc/ansible
    [root@an ansible]# vim web.yml

    [root@an ansible]# ansible-playbook -C /etc/ansible/web.yml

    [root@an web]# yum install -y rsync

    [root@an ~]# vim /etc/ansible/roles/nginx/tasks/main.yml

    • name: install package
      yum: name={{ item }}
      with_items:
      • nginx
      • keepalived
    • name: config keepalived
      template: src=keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf
      notify: restart keepalived
    • name: config nginx
      template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
      notify: restart nginx
    • name: start service
      service: name={{ item }} state=started enabled=true
      with_items:
      • keepalived
      • nginx

    [root@an ~]# vim /etc/ansible/roles/nginx/handlers/main.yml

    • name: restart keepalived
      service: name=keepalived state=restarted
    • name: restart nginx
      service: name=nginx state=restarted
      [root@an ~]# vim /etc/ansible/roles/nginx/templates/keepalived.conf.j2

    global_defs {
    notification_email {
    acassen@firewall.loc
    }
    notification_email_from Alexandre.Cassen@firewall.loc
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id {{ansible_hostname}} #自带变量,通过ansible 主机IP -m setup 查询
    vrrp_mcast_group4 224.0.0.43
    }

    vrrp_instance VI_1 {
    state {{ state }} #已通过hosts文件定义变量
    interface ens33 #网卡名
    virtual_router_id 51
    priority {{ priority }}
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass lovelinux #设置密码
    }
    virtual_ipaddress {
    172.18.43.88 #虚拟IP
    }
    }

    vim /etc/ansible/roles/nginx/templates/nginx.conf.j2
    upstream web { #新增段
    server 172.18.43.61;
    server 172.18.43.62;
    }
    server {
    listen 80 defaultserver;
    listen [::]:80 default_server;
    server_name
    ;
    root /usr/share/nginx/html;

    1. # Load configuration files for the default server block.
    2. include /etc/nginx/default.d/*.conf;
    3. location / { #新增段
    4. proxy_pass
    5. }
    6. }

    [root@an ~]# vim /etc/ansible/nginx.yml

    [root@an ~]# ansible-playbook -C /etc/ansible/nginx.yml

    [root@an ~]# vim /etc/ansible/roles/mysql/tasks/main.yml

    • name: install mariadb
      yum: name=mariadb-server
    • name: copy sql file
      copy: src=mysql.sql dest=/tmp/mysql.sql
    • name: start mysql service
      service: name=mariadb state=started
    • name: config mysql
      shell: “mysql < /tmp/mysql.sql”
      [root@an ~]# vim /etc/ansible/roles/mysql/files/mysql.sql
      CREATE DATABASE wp;
      GRANT ALL ON wp.* TO ‘wpuser’@’%’ IDENTIFIED BY ‘lovelinux’;

    [root@an ~]# vim /etc/ansible/mysql.yml

    • hosts: mysql
      remote_user: root
      roles:
      • mysql

    [root@an ~]# ansible-playbook -C /etc/ansible/mysql.yml
    [root@an ~]# cd /etc/ansible/
    [root@an ansible]# tree -L 4

    [root@an ansible]# ansible-playbook web.yml

    [root@an ansible]# ansible-playbook nginx.yml

    [root@an ansible]#ansible-playbook mysql.yml