1、ansible-playbook实现MySQL的二进制部署
准备安装介质和配置文件
[root@CentOS84 data]# tree file
file
├── my.cnf
└── mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
[root@CentOS84 data]# cat file/my.cnf
[mysqld]
datadir=/data/mysql
log_bin=1
server_id=1
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sockansible-playbook安装脚本
root@CentOS84 data# cat mysql_install2.yaml
---
hostsdb
  remote_userroot
  gather_factsno
  vars
    version"mysql-5.7.35-linux-glibc2.12-x86_64"
    suffix"tar.gz"
    file"{{version}}.{{suffix}}"
    password"mysql123"
  tasks
name创建用户组
      groupname=mysql gid=306
name创建用户并加入到组
      username=mysql uid=306 group=mysql shell=/sbin/nologin system=yes create_home=no home=/data/mysql
name拷贝并解压文件
      unarchivesrc=/data/file/file dest=/usr/local/
name创建链接文件
      filesrc=/usr/local/version dest=/usr/local/mysql state=link owner=mysql group=mysql
name配置环境变量
      shell"echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh"
name环境变量生效
      shellsource /etc/profile.d/mysql.sh
name创建数据目录
      filename=/data/mysql state=directory owner=mysql group=mysql
name拷贝配置文件
      copysrc=/data/file/my.cnf dest=/etc/my.cnf
name初始化数据文件
      shellmysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
name拷贝启动脚本
      shell/bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
name启动mysql服务
      shellchkconfig --add mysqld && service mysqld start
name更改root口令  password 
      shellmysqladmin -uroot password  password 执行过程如下:
root@CentOS84 data# ansible-playbook mysql_install2.yaml
PLAY db *****************************************************************************************************************************
TASK 创建用户组 **************************************************************************************************************************
changed10.10.10.12
changed10.10.10.11
TASK 创建用户并加入到组 **********************************************************************************************************************
changed10.10.10.12
changed10.10.10.11
TASK 拷贝并解压文件 ************************************************************************************************************************
changed10.10.10.11
changed10.10.10.12
TASK 创建链接文件 *************************************************************************************************************************
changed10.10.10.11
changed10.10.10.12
TASK 配置环境变量 *************************************************************************************************************************
changed10.10.10.12
changed10.10.10.11
TASK 环境变量生效 *************************************************************************************************************************
changed10.10.10.12
changed10.10.10.11
TASK 创建数据目录 *************************************************************************************************************************
changed10.10.10.12
changed10.10.10.11
TASK 拷贝配置文件 *************************************************************************************************************************
changed10.10.10.12
changed10.10.10.11
TASK 初始化数据文件 ************************************************************************************************************************
changed10.10.10.12
changed10.10.10.11
TASK 拷贝启动脚本 *************************************************************************************************************************
changed10.10.10.12
changed10.10.10.11
TASK 启动mysql服务 **********************************************************************************************************************
changed10.10.10.12
changed10.10.10.11
TASK 更改root口令 mysql123 **************************************************************************************************************
changed10.10.10.12
changed10.10.10.11
PLAY RECAP ****************************************************************************************************************************
10.10.10.11                ok=12   changed=12   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
10.10.10.12                ok=12   changed=12   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0登录验证:
[root@CentOS84 data]# ssh 10.10.10.11
  Last login: Thu Mar 10 18:29:22 2022 from 10.10.10.10
  [root@CentOS8411 ~]# mysql -uroot -pmysql123
  mysql: [Warning] Using a password on the command line interface can be insecure.
  Welcome to the MySQL monitor.  Commands end with ; or \g.
  Your MySQL connection id is 3
  Server version: 5.7.35-log MySQL Community Server (GPL)
  
  Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  
  Oracle is a registered trademark of Oracle Corporation and/or its
  affiliates. Other names may be trademarks of their respective
  owners.
  
  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  
  mysql> select user,host,plugin,authentication_string  from mysql.user;
  +---------------+-----------+-----------------------+-------------------------------------------+
  | user          | host      | plugin                | authentication_string                     |
  +---------------+-----------+-----------------------+-------------------------------------------+
  | root          | localhost | mysql_native_password | *F861720E101148897B0F5239DB926E756B1C28B3 |
  | mysql.session | localhost | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
  | mysql.sys     | localhost | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
  +---------------+-----------+-----------------------+-------------------------------------------+
  3 rows in set (0.00 sec)ansible-playbook卸载脚本
root@CentOS84 data# cat mysql_uninstall.yaml
  ---
hostsdb
    remote_userroot
    gather_factsno
  
    vars
      version"mysql-5.7.35-linux-glibc2.12-x86_64"
  
  
    tasks
namestop mysql service
        shellservice mysqld stop
nameremove file
        filename= item  state=absent
        with_items
/usr/local/mysql
/usr/local/ version 
/etc/my.cnf
/etc/init.d/mysqld
/data/mysql
namedelete user
      username=mysql state=absent remove=yesroot@CentOS84 data# ansible-playbook mysql_uninstall.yaml
  
  PLAY db *****************************************************************************************************************************
  
  TASK stop mysql service *************************************************************************************************************
  [WARNING]Consider using the service module rather than running 'service'.  If you need to use command because service is
  insufficient you can add 'warnfalse' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
  changed10.10.10.11
  
  TASK remove file ********************************************************************************************************************
  changed10.10.10.11 => (item=/usr/local/mysql)
  changed10.10.10.11 => (item=/usr/local/mysql-5.7.35-linux-glibc2.12-x86_64)
  changed10.10.10.11 => (item=/etc/my.cnf)
  changed10.10.10.11 => (item=/etc/init.d/mysqld)
  changed10.10.10.11 => (item=/data/mysql)
  
  TASK delete user ********************************************************************************************************************
  changed10.10.10.11
  
  PLAY RECAP ****************************************************************************************************************************
  10.10.10.11                ok=3    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=02、Ansible playbook实现apache批量部署,并对不同主机提供以各自IP地址为内容的index.html
环境准备
##hosts文件:
[web]
10.10.10.12  #CentOS7
10.10.10.11  #CentOS8
10.10.10.13  #Ubuntu1804
##index.html 配置文件
[root@CentOS84 data]# cat templates/index.html.j2
IP={{ansible_all_ipv4_addresses[0]}}ansible-playbook运行安装脚本
root@CentOS84 data# cat install_apache.yaml
---
hostsweb
  tasks
nameinstall apache
      yumname=httpd
      whenansible_os_family == "RedHat"
nameinstall apache
      aptname=apache2
      whenansible_os_family == "Debian"
nametemplate config to index.html
      templatesrc=index.html.j2 dest=/var/www/html/index.html
namestart apache
      servicename=httpd state=started
      whenansible_os_family == "RedHat"
namecheck service
      shellsystemctl status httpd | cat
      whenansible_os_family == "RedHat"
      registercheck_services_RedHat
namecheck service
      shellsystemctl status apache2 | cat
      whenansible_os_family == "Debian"
      registercheck_services_Debian
debugvar=check_services_RedHat.stdout_lines
      whenansible_os_family == "RedHat"
debugvar=check_services_Debian.stdout_lines
      whenansible_os_family == "Debian"
namecheck port
      shellss -ntlp
      registercheck_port
debug
        msg"{{check_port.stdout_lines}}"ansible-playbook脚本执行过程
root@CentOS84 data# ansible-playbook install_apache.yaml
PLAY web ****************************************************************************************************************************
TASK Gathering Facts ****************************************************************************************************************
ok10.10.10.13
ok10.10.10.12
ok10.10.10.11
TASK install apache *****************************************************************************************************************
skipping10.10.10.13
changed10.10.10.11
changed10.10.10.12
TASK install apache *****************************************************************************************************************
skipping10.10.10.12
skipping10.10.10.11
changed10.10.10.13
TASK template config to index.html **************************************************************************************************
changed10.10.10.12
changed10.10.10.13
changed10.10.10.11
TASK start apache *******************************************************************************************************************
skipping10.10.10.13
changed10.10.10.11
changed10.10.10.12
TASK check service ******************************************************************************************************************
skipping10.10.10.13
changed10.10.10.12
changed10.10.10.11
TASK check service ******************************************************************************************************************
skipping10.10.10.12
skipping10.10.10.11
changed10.10.10.13
TASK debug **************************************************************************************************************************
ok10.10.10.12 => 
    "check_services_RedHat.stdout_lines"
        "● httpd.service - The Apache HTTP Server"
        "   Loadedloaded (/usr/lib/systemd/system/httpd.service; disabled; vendor presetdisabled)",
        "   Activeactive (running) since Sun 2022-03-13 001833 CST; 550ms ago",
        "     Docsmanhttpd(8)",
        "           man:apachectl(8)"
        " Main PID9025 (httpd)",
        "   Status\"Processing requests...\"",
        "   CGroup/system.slice/httpd.service",
        "           ├─9025 /usr/sbin/httpd -DFOREGROUND"
        "           ├─9026 /usr/sbin/httpd -DFOREGROUND"
        "           ├─9027 /usr/sbin/httpd -DFOREGROUND"
        "           ├─9028 /usr/sbin/httpd -DFOREGROUND"
        "           ├─9029 /usr/sbin/httpd -DFOREGROUND"
        "           └─9030 /usr/sbin/httpd -DFOREGROUND"
        ""
        "Mar 13 00:18:31 centos79 systemd[1]Starting The Apache HTTP Server...",
        "Mar 13 00:18:32 centos79 httpd[9025]AH00558httpdCould not reliably determine the server's fully qualified domain name, using fe80::e132:5b27:135f:b310. Set the 'ServerName' directive globally to suppress this message",
        "Mar 13 00:18:33 centos79 systemd[1]Started The Apache HTTP Server."
    
ok10.10.10.11 => 
    "check_services_RedHat.stdout_lines"
        "● httpd.service - The Apache HTTP Server"
        "   Loadedloaded (/usr/lib/systemd/system/httpd.service; disabled; vendor presetdisabled)",
        "   Activeactive (running) since Sat 2022-03-12 161833 CST; 1s ago",
        "     Docsmanhttpd.service(8)",
        " Main PID19409 (httpd)",
        "   Status\"Started, listening on: port 80\"",
        "    Tasks213 (limit4757)",
        "   Memory24.3M",
        "   CGroup/system.slice/httpd.service",
        "           ├─19409 /usr/sbin/httpd -DFOREGROUND"
        "           ├─19422 /usr/sbin/httpd -DFOREGROUND"
        "           ├─19423 /usr/sbin/httpd -DFOREGROUND"
        "           ├─19424 /usr/sbin/httpd -DFOREGROUND"
        "           └─19425 /usr/sbin/httpd -DFOREGROUND"
        ""
        "Mar 12 16:18:32 CentOS8411 systemd[1]Starting The Apache HTTP Server...",
        "Mar 12 16:18:33 CentOS8411 httpd[19409]AH00558httpdCould not reliably determine the server's fully qualified domain name, using fe80::20c:29ff:fe75:3173. Set the 'ServerName' directive globally to suppress this message",
        "Mar 12 16:18:33 CentOS8411 systemd[1]Started The Apache HTTP Server.",
        "Mar 12 16:18:34 CentOS8411 httpd[19409]Server configured listening onport 80"
    
skipping10.10.10.13
TASK debug **************************************************************************************************************************
skipping10.10.10.12
skipping10.10.10.11
ok10.10.10.13 => 
    "check_services_Debian.stdout_lines"
        "● apache2.service - The Apache HTTP Server"
        "   Loadedloaded (/lib/systemd/system/apache2.service; enabled; vendor presetenabled)",
        "  Drop-In/lib/systemd/system/apache2.service.d",
        "           └─apache2-systemd.conf"
        "   Activeactive (running) since Sat 2022-03-12 081827 UTC; 7s ago",
        " Main PID13485 (apache2)",
        "    Tasks55 (limit2284)",
        "   CGroup/system.slice/apache2.service",
        "           ├─13485 /usr/sbin/apache2 -k start"
        "           ├─13489 /usr/sbin/apache2 -k start"
        "           └─13490 /usr/sbin/apache2 -k start"
        ""
        "Mar 12 08:18:27 ubnutu1804 systemd[1]Starting The Apache HTTP Server...",
        "Mar 12 08:18:27 ubnutu1804 apachectl[13466]AH00558apache2Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message",
        "Mar 12 08:18:27 ubnutu1804 systemd[1]Started The Apache HTTP Server."
    
TASK check port *********************************************************************************************************************
changed10.10.10.13
changed10.10.10.12
changed10.10.10.11
TASK debug **************************************************************************************************************************
ok10.10.10.12 => 
    "msg"
        "State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              "
        "LISTEN     0      128          *:22                       *:*                   users:((\"sshd\",pid=1180,fd=3))"
        "LISTEN     0      128       [::]:80                    [::]:*                   users:((\"httpd\",pid=9030,fd=4),(\"httpd\",pid=9029,fd=4),(\"httpd\",pid=9028,fd=4),(\"httpd\",pid=9027,fd=4),(\"httpd\",pid=9026,fd=4),(\"httpd\",pid=9025,fd=4))"
        "LISTEN     0      128       [::]:22                    [::]:*                   users:((\"sshd\",pid=1180,fd=4))"
    
ok10.10.10.11 => 
    "msg"
        "State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess                       "
        "LISTEN 0      128          0.0.0.0:22        0.0.0.0:*    users:((\"sshd\",pid=951,fd=5))"
        "LISTEN 0      128             [::]:22           [::]:*    users:((\"sshd\",pid=951,fd=7))"
        "LISTEN 0      128                *:80              *:*    users:((\"httpd\",pid=19425,fd=4),(\"httpd\",pid=19424,fd=4),(\"httpd\",pid=19423,fd=4),(\"httpd\",pid=19409,fd=4))"
    
ok10.10.10.13 => 
    "msg"
        "State    Recv-Q    Send-Q        Local Address:Port        Peer Address:Port                                                                                    "
        "LISTEN   0         128           127.0.0.53%lo:53               0.0.0.0:*        users:((\"systemd-resolve\",pid=963,fd=13))                                      "
        "LISTEN   0         128                 0.0.0.0:22               0.0.0.0:*        users:((\"sshd\",pid=1136,fd=3))                                                 "
        "LISTEN   0         128               127.0.0.1:6010             0.0.0.0:*        users:((\"sshd\",pid=1870,fd=9))                                                 "
        "LISTEN   0         128                       *:80                     *:*        users:((\"apache2\",pid=13490,fd=4),(\"apache2\",pid=13489,fd=4),(\"apache2\",pid=13485,fd=4))"
        "LISTEN   0         128                    [::]:22                  [::]:*        users:((\"sshd\",pid=1136,fd=4))                                                 "
        "LISTEN   0         128                   [::1]:6010                [::]:*        users:((\"sshd\",pid=1870,fd=8))                                                 "
    
PLAY RECAP ****************************************************************************************************************************
10.10.10.11                ok=8    changed=5    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0
10.10.10.12                ok=8    changed=5    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0
10.10.10.13                ok=7    changed=4    unreachable=0    failed=0    skipped=4    rescued=0    ignored=0
##验证
root@CentOS84 data# curl 10.10.10.11
IP=10.10.10.11
root@CentOS84 data# curl 10.10.10.12
IP=10.10.10.12
root@CentOS84 data# curl 10.10.10.13
IP=10.10.10.13ansible-playbook卸载脚本
root@CentOS84 data# cat uninstall_apache.yaml
---
hostsweb
  tasks
nameuninstall apache
      yumname=httpd state=absent
      whenansible_os_family == "RedHat"
nameuninstall apache
      aptname=apache2 state=absent
      whenansible_os_family == "Debian"
nameremove diractory
      filepath=/var/www state=absent3、http的报文结构和状态码总结
请求报文
由开始行、首部行和实体主体组成,在请求报文中,开始行就是请求行。
## request报文格式
  <method> <request-URL> <version>
  <headers>
  <entity-body>
响应报文
开始行是状态行,状态行包括HTTP的版本、状态码,以及解释状态码的简单短语。
##response报文格式
    <version> <status> <reason-phrase>
    <headers>
    <entity-body>
状态码总结
200: 成功,请求数据通过响应报文的entity-body部分发送;OK
301: 请求的URL指向的资源已经被删除;但在响应报文中通过首部Location指明了资源现在所处的新位置;Moved Permanently
302: 响应报文Location指明资源临时新位置 Moved Temporarily
304: 客户端发出了条件式请求,但服务器上的资源未曾发生改变,则通过响应此响应状态码通知客户端;Not Modified
307:  浏览器内部重定向
401: 需要输入账号和口令认证方能访问资源;Unauthorized
403: 请求被禁止;Forbidden
404: 服务器无法找到客户端请求的资源;Not Found
500: 服务器内部错误;Internal Server Error
502: 代理服务器从后端服务器收到了一条伪响应,如无法连接到网关;Bad Gateway
503: 服务不可用,临时服务器维护或过载,服务器无法处理请求
504: 网关超时








