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.sock
ansible-playbook安装脚本
root@CentOS84 data # cat mysql_install2.yaml
---
hosts db
remote_user root
gather_factsno
vars
version"mysql-5.7.35-linux-glibc2.12-x86_64"
suffix"tar.gz"
file"{{version}}.{{suffix}}"
password"mysql123"
tasks
name 创建用户组
group name=mysql gid=306
name 创建用户并加入到组
user name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes create_home=no home=/data/mysql
name 拷贝并解压文件
unarchive src=/data/file/ file dest=/usr/local/
name 创建链接文件
file src=/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 环境变量生效
shell source /etc/profile.d/mysql.sh
name 创建数据目录
file name=/data/mysql state=directory owner=mysql group=mysql
name 拷贝配置文件
copy src=/data/file/my.cnf dest=/etc/my.cnf
name 初始化数据文件
shell mysqld --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服务
shell chkconfig --add mysqld && service mysqld start
name 更改root口令 password
shell mysqladmin -uroot password password
执行过程如下:
root@CentOS84 data # ansible-playbook mysql_install2.yaml
PLAY db *****************************************************************************************************************************
TASK 创建用户组 **************************************************************************************************************************
changed 10.10.10.12
changed 10.10.10.11
TASK 创建用户并加入到组 **********************************************************************************************************************
changed 10.10.10.12
changed 10.10.10.11
TASK 拷贝并解压文件 ************************************************************************************************************************
changed 10.10.10.11
changed 10.10.10.12
TASK 创建链接文件 *************************************************************************************************************************
changed 10.10.10.11
changed 10.10.10.12
TASK 配置环境变量 *************************************************************************************************************************
changed 10.10.10.12
changed 10.10.10.11
TASK 环境变量生效 *************************************************************************************************************************
changed 10.10.10.12
changed 10.10.10.11
TASK 创建数据目录 *************************************************************************************************************************
changed 10.10.10.12
changed 10.10.10.11
TASK 拷贝配置文件 *************************************************************************************************************************
changed 10.10.10.12
changed 10.10.10.11
TASK 初始化数据文件 ************************************************************************************************************************
changed 10.10.10.12
changed 10.10.10.11
TASK 拷贝启动脚本 *************************************************************************************************************************
changed 10.10.10.12
changed 10.10.10.11
TASK 启动mysql服务 **********************************************************************************************************************
changed 10.10.10.12
changed 10.10.10.11
TASK 更改root口令 mysql123 **************************************************************************************************************
changed 10.10.10.12
changed 10.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
---
hosts db
remote_user root
gather_factsno
vars
version"mysql-5.7.35-linux-glibc2.12-x86_64"
tasks
name stop mysql service
shell service mysqld stop
name remove file
file name= item state=absent
with_items
/usr/local/mysql
/usr/local/ version
/etc/my.cnf
/etc/init.d/mysqld
/data/mysql
name delete user
user name=mysql state=absent remove=yes
root@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 'warn false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
changed 10.10.10.11
TASK remove file ********************************************************************************************************************
changed 10.10.10.11 => (item=/usr/local/mysql)
changed 10.10.10.11 => (item=/usr/local/mysql-5.7.35-linux-glibc2.12-x86_64)
changed 10.10.10.11 => (item=/etc/my.cnf)
changed 10.10.10.11 => (item=/etc/init.d/mysqld)
changed 10.10.10.11 => (item=/data/mysql)
TASK delete user ********************************************************************************************************************
changed 10.10.10.11
PLAY RECAP ****************************************************************************************************************************
10.10.10.11 ok=3 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2、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
---
hosts web
tasks
name install apache
yum name=httpd
when ansible_os_family == "RedHat"
name install apache
apt name=apache2
when ansible_os_family == "Debian"
name template config to index.html
template src=index.html.j2 dest=/var/www/html/index.html
name start apache
service name=httpd state=started
when ansible_os_family == "RedHat"
name check service
shell systemctl status httpd | cat
when ansible_os_family == "RedHat"
register check_services_RedHat
name check service
shell systemctl status apache2 | cat
when ansible_os_family == "Debian"
register check_services_Debian
debug var=check_services_RedHat.stdout_lines
when ansible_os_family == "RedHat"
debug var=check_services_Debian.stdout_lines
when ansible_os_family == "Debian"
name check port
shell ss -ntlp
register check_port
debug
msg"{{check_port.stdout_lines}}"
ansible-playbook脚本执行过程
root@CentOS84 data # ansible-playbook install_apache.yaml
PLAY web ****************************************************************************************************************************
TASK Gathering Facts ****************************************************************************************************************
ok 10.10.10.13
ok 10.10.10.12
ok 10.10.10.11
TASK install apache *****************************************************************************************************************
skipping 10.10.10.13
changed 10.10.10.11
changed 10.10.10.12
TASK install apache *****************************************************************************************************************
skipping 10.10.10.12
skipping 10.10.10.11
changed 10.10.10.13
TASK template config to index.html **************************************************************************************************
changed 10.10.10.12
changed 10.10.10.13
changed 10.10.10.11
TASK start apache *******************************************************************************************************************
skipping 10.10.10.13
changed 10.10.10.11
changed 10.10.10.12
TASK check service ******************************************************************************************************************
skipping 10.10.10.13
changed 10.10.10.12
changed 10.10.10.11
TASK check service ******************************************************************************************************************
skipping 10.10.10.12
skipping 10.10.10.11
changed 10.10.10.13
TASK debug **************************************************************************************************************************
ok 10.10.10.12 =>
"check_services_RedHat.stdout_lines"
"● httpd.service - The Apache HTTP Server"
" Loaded loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset disabled)",
" Active active (running) since Sun 2022-03-13 00 18 33 CST; 550ms ago",
" Docs man httpd(8)",
" man:apachectl(8)"
" Main PID 9025 (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] AH00558 httpd Could 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."
ok 10.10.10.11 =>
"check_services_RedHat.stdout_lines"
"● httpd.service - The Apache HTTP Server"
" Loaded loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset disabled)",
" Active active (running) since Sat 2022-03-12 16 18 33 CST; 1s ago",
" Docs man httpd.service(8)",
" Main PID 19409 (httpd)",
" Status \"Started, listening on: port 80\"",
" Tasks 213 (limit 4757)",
" Memory 24.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] AH00558 httpd Could 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 on port 80"
skipping 10.10.10.13
TASK debug **************************************************************************************************************************
skipping 10.10.10.12
skipping 10.10.10.11
ok 10.10.10.13 =>
"check_services_Debian.stdout_lines"
"● apache2.service - The Apache HTTP Server"
" Loaded loaded (/lib/systemd/system/apache2.service; enabled; vendor preset enabled)",
" Drop-In /lib/systemd/system/apache2.service.d",
" └─apache2-systemd.conf"
" Active active (running) since Sat 2022-03-12 08 18 27 UTC; 7s ago",
" Main PID 13485 (apache2)",
" Tasks 55 (limit 2284)",
" 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] AH00558 apache2 Could 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 *********************************************************************************************************************
changed 10.10.10.13
changed 10.10.10.12
changed 10.10.10.11
TASK debug **************************************************************************************************************************
ok 10.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))"
ok 10.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))"
ok 10.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.13
ansible-playbook卸载脚本
root@CentOS84 data # cat uninstall_apache.yaml
---
hosts web
tasks
name uninstall apache
yum name=httpd state=absent
when ansible_os_family == "RedHat"
name uninstall apache
apt name=apache2 state=absent
when ansible_os_family == "Debian"
name remove diractory
file path=/var/www state=absent
3、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: 网关超时