企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
[TOC] # 1.服务器配置及架构说明 ``` 192.168.116.135 haproxy 192.168.116.134 mysql 192.168.116.133 apache php 192.168.116.134 apache php ``` ``` [root@linux-node01 roles]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) [root@linux-node01 roles]# uname -a Linux linux-node01 3.10.0-862.11.6.el7.x86_64 #1 SMP Tue Aug 14 21:49:04 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux ``` # 2.ansible目录结构 ``` ![](images/94bc5e5af68f339be3434e456042a8d.png ├── group_vars │   ├── all │   ├── haproxy │   └── mysql ├── hosts ├── roles │   ├── apache │   │   ├── tasks │   │   │   └── ── main.yml │   │   │   └── templates │   │   └── ── index.php.j2 .j2 │   ├── base │   │   ├── files │   │   │   ├── ── CentOS-Base.repo │   │   │   │   └── ── epel.repo │   │   │   └── tasks │   │   └── ── main.yml │   │   ├── haproxy │   │   ├── handlers │   │   │   └── ── main.yml │   │   │   ├── tasks │   │   │   └── ── main.yml │   │   │   └── templates │   │   └── ── haproxy.cfg.j2 .j2 │   └── mysql │   ├── defaults │   │   └── ── main.yml │   │   ├── handlers │   │   └── ── main.yml │   │   ├── tasks │   │   ├── ── main.yml │   │   │   └── ── main.yml.bak │   │   └── templates │   ├── ── my.cnf.j2f.j2 │   └── ── my.cnf.j2.bak ├── ├── ── site.retry └── └── ── site.yml ``` # 3.入口文件及变量信息 ``` cat site.yml --- - name: init base en for all hosts hosts: all roles: - base - name: install mysql hosts: mysql gather_facts: False roles: - mysql - name: install apache hosts: apache roles: - apache - name: install haproxy hosts: haproxy roles: - haproxy ``` ## hosts ``` [apache] 192.168.116.133 192.168.116.134 [mysql] 192.168.116.134 [haproxy] 192.168.116.135 ``` ## group_vars ``` all --- ansible_ssh_pass: 123456 cat haproxy --- mode: http balance: roundrobin cat mysql --- mysql_port: 3306 user: ansible password: ansible database: ansible ``` ## apache ``` cat tasks/main.yml --- - name: Install Apache and PHP yum: name={{ item }} state=present with_items: - httpd - php - php-mysql - libsemanage-python - libselinux-python - name: copy index.php.j2 template: src=index.php.j2 dest=/var/www/html/index.php - name: http service state service: name=httpd state=started enabled=yes cat templates/index.php.j2 <?php hostname {{ ansible_default_ipv4.address }} ?> ``` ## base ``` cat base/tasks/main.yml --- - copy: src=CentOS-Base.repo dest=/etc/yum.repos.d/CentOS-Base.repo owner=root group=root mode=644 - copy: src=epel.repo dest=/etc/yum.repos.d/epel.repo owner=root group=root mode=644 ls base/files/ CentOS-Base.repo epel.repo ``` ## haproxy ``` cat haproxy/tasks/main.yml --- - name: install haproxy yum: name={{ item }} state=present with_items: - haproxy - name: copy haproxy.cf template: src=haproxy.cfg.j2 dest=/etc/haproxy/haproxy.cfg owner=root group=root mode=644 notify: - restart haproxy - name: start haproxy service: name=haproxy state=started enabled=yes cat haproxy/templates/haproxy.cfg.j2 #--------------------------------------------------------------------- # Example configuration for a possible web application. See the # full configuration options online. # # http://haproxy.1wt.eu/download/1.4/doc/configuration.txt # #--------------------------------------------------------------------- #--------------------------------------------------------------------- # Global settings #--------------------------------------------------------------------- global log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon # turn on stats unix socket stats socket /var/lib/haproxy/stats #--------------------------------------------------------------------- # common defaults that all the 'listen' and 'backend' sections will # use if not designated in their block #--------------------------------------------------------------------- defaults mode {{ mode }} log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 #--------------------------------------------------------------------- # main frontend which proxys to the backends #--------------------------------------------------------------------- frontend ansible bind {{ ansible_default_ipv4.address }}:80 mode {{ mode }} acl url_static path_beg -i /static /images /javascript /stylesheets acl url_static path_end -i .jpg .gif .png .css .js use_backend static if url_static default_backend static #--------------------------------------------------------------------- # static backend for serving up images, stylesheets and such #--------------------------------------------------------------------- backend static balance {{ balance }} {% for host in groups['apache'] %} server {{ hostvars[host].ansible_hostname }} {{ hostvars[host].ansible_default_ipv4.address }}:80 check inter 3000 rise 3 fall 2 {% endfor %} cat haproxy/handlers/main.yml --- - name: start haproxy service: name=haproxy state=started enabled=yes ``` ## mysql ``` cat mysql/tasks/main.yml --- - name: install mysql yum: name={{ item }} state=installed with_items: - mariadb-server - MySQL-python - name: create mysql configuration file template: src=my.cnf.j2 dest=/etc/my.cnf notify: - restart mysql - name: start mariadb service service: name=mariadb state=restarted - name: create mysql database mysql_db: name={{ database }} state=present - name: create mysql user mysql_user: name={{ user }} password={{ password }} priv='*.*:ALL' state=present cat mysql/handlers/main.yml --- - name: restart mysql service: name=mariadb state=restarted cat mysql/templates/my.cnf.j2 [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql symbolic-links=0 port={{ mysql_port }} [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mariadb/mysqld.pid ``` # 执行ansible-playbook ``` ansible-playbook -i hosts site.yml ```