Ansible-playbook练习&http报文结构总结
Posted 终究是想不起来
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ansible-playbook练习&http报文结构总结相关的知识,希望对你有一定的参考价值。
1、ansible-playbook实现mysql的二进制部署
```html/xml
[root@ansible ~]#cat /data/ansible/files/my.cnf
[mysqld]
socket=/tmp/mysql.sock
user=mysql
symbolic-links=0
datadir=/data/mysql
innodb_file_per_table=1
log-bin
pid-file=/data/mysql/mysqld.pid
[client]
port=3306
socket=/tmp/mysql.sock
[mysqld_safe]
log-error=/var/log/mysqld.log
[root@ansible ~]#cat /data/ansible/files/secure_mysql.sh
#!/bin/bash
/usr/local/mysql/bin/mysql_secure_installation <<EOF
y
magedu
magedu
y
y
y
y
EOF
[root@ansible ~]#tree /data/ansible/files/
/data/ansible/files/
├── my.cnf
├── mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
└── secure_mysql.sh
0 directories, 3 files
[root@ansible ~]#cat /data/ansible/install_mysql.yml
install mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
-
hosts: dbsrvs
remote_user: root
gather_fcts: notasks:
- name: install packages
yum: name=libaio,perl-Data-Dumper,perl-Getopt-Long - name: create mysql group
group: name=mysql gid=306 - name: create mysql user
user: name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes create_home=no home=/data/mysql - name: copy tar to remote host and file mode
unarchive: src=/data/ansible/files/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz dest=/usr/local/ owner=root group=root - name: create linkfile /usr/local/mysql
file: src=/usr/local/mysql-5.6.46-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link - name: data dir
shell: chdir=/usr/local/mysql/ ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
tags: data - name: config my.cnf
copy: src=/data/ansible/files/my.cnf dest=/etc/my.cnf - name: service script
shell: /bin/cp /usr/local/mysql/support-files/mysql.server/etc/init.d/mysqld - name: enable service
shell: /etc/init.d/mysqld start;chkconfig --add mysqld;chkconfig mysqld on
tags: service
- name: PATH variable
copy: content=PATH=/usr/local/mysql/bin:$PATH dest=/etc/profile.d/mysql.sh - name: secure script
script: /data/ansible/files/secure_mysql.sh
tags: script
- name: install packages
2、Ansible playbook实现apache批量部署,并对不同主机提供以各自IP地址为内容的index.html
准备
```html/xml
wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.51.tar.bz2 --no-check-certificate
wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.bz2 --no-check-certificate
wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.bz2 --no-check-certificate
vi /apps/httpd/httpd.service
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[Service]
Type=forking
ExecStart=/apps/httpd/bin/apachectl start
ExecReload=/apps/httpd/bin/apachectl graceful
ExecStop=/apps/httpd/bin/apachectl stop
We want systemd to give httpd some time to finish gracefully, but still want
it to kill httpd after TimeoutStopSec if something went wrong during the
graceful stop. Normally, Systemd sends SIGTERM signal right after the
ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now httpd.service
ls #最终准备好四个文件
apr-1.7.0.tar.bz2 apr-util-1.6.1.tar.bz2 httpd-2.4.51.tar.bz2 httpd.service
playbook
```html/xml
- hosts: httpd
remote_user: root
gather_facts: no
vars:
httpd_file: httpd-2.4.46.tar.bz2
arp_file: apr-1.7.0.tar.bz2
arp_util_file: apr-util-1.6.1.tar.bz2
tasks:
- name: install packages
yum:
name:
- gcc
- lrzsz
- wget
- make
- pcre-devel
- openssl-devel
- expat-devel
state: present
- name: Create a directory if it does not exist
file:
path: /apps
state: directory
mode: 0755
- name: Extract httpd_file into /apps/httpd24
unarchive:
src: ./ httpd_file
dest: /apps/
- name: Extract arp_file into /apps/apr/
unarchive:
src: ./ arp_file
dest: /apps/httpd-2.4.46/srclib
- name: Extract arp_util_file into /apps/apr/
unarchive:
src: ./ arp_util_file
dest: /apps/httpd-2.4.46/srclib
- name: move directory /apps/httpd-2.4.46/srclib/apr
shell: mv /apps/httpd-2.4.46/srclib/apr-1.7.0 /apps/httpd-2.4.46/srclib/apr
- name: move directory /apps/httpd-2.4.46/srclib/apr-util
shell: mv /apps/httpd-2.4.46/srclib/apr-util-1.6.1 /apps/httpd-2.4.46/srclib/apr-util
- name: Ensure group "apache" exists
group:
name: apache
state: present
gid: 80
- name: Add the user apache with a specific uid and a primary group of apache
user:
name: apache
comment: apache
uid: 80
group: apache
- name: configure httpd
shell: ./configure --prefix=/apps/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork chdir=/apps/httpd-2.4.46/
- name: make
shell: make -j 2 chdir=/apps/httpd-2.4.46/
- name: make install
shell: make install chdir=/apps/httpd-2.4.46/
- name: make ln
file:
src: /apps/httpd24
dest: /apps/httpd
owner: apache
group: apache
state: link
- name: copy http.service file to remote
copy:
src: httpd.service
dest: /usr/lib/systemd/system/
notify: start httpd service
- name: config index.html
shell: echo `hostname -I` > /apps/httpd/htdocs/index.html
- name: Replace httpd config file
replace:
path: /apps/httpd/conf/httpd.conf
regexp: ^#(ServerName).*$
replace: \\1 :80
handlers:
- name: start httpd service
service:
name: httpd
state: started
enabled: yes
3、http的报文结构和状态码总结
1.在TCP/IP协议簇中的位置
位于四层协议的应用层。基于运输层的TCP协议实现。
2.请求报文结构
包括报文首部、空行、报文主体3部分。
报文首部:
第一行:请求行,请求方法,请求路径,HTTP版本
后续为各个首部:包括请求首部字段、通用首部字段和实体首部字段
空行:
报文主体:
向服务器发送的数据。如get请求中的各个参数。post请求中的参数。
3.响应报文结构
也是包括报文首部、空行、报文主体3部分。
报文首部:
第一行:状态行,包括HTTP版本 状态码 原因短语
后续为首部字段:响应首部字段、通用首部字段、实体首部字段
报文主体:服务器返回的响应体。如HTTM页面。
4.常见状态码
(1) 2xx
2开头的状态码表示成功
200 OK
正常处理并返回了
204 No Content
正常处理了,但响应中不含主体。
用于需要从客户端往服务器发送数据但不需要响应内容的情况。
206 Partial Content
客户端进行了范围请求,服务器正常返回了。请求时通过Content-Range指定范围。
(2)3xx
重定向相关
301 Moved Permanently
永久性重定向。表示请求的资源已经永久性分配了新的URI,以后应该使用该新的URI。
使用Location首部字段表示新URI地址。浏览器会重新请求一次该URI。
302 Found
临时重定向,希望用户本次使用的新分配的URI。
和301非常类似,浏览器也会根据Location字段重新进行请求。
在实际开发中常用于页面跳转。
303 See Other
和302功能相同,只是明确表明客户端应该使用get请求。
304 Not Modified
和重定向没有关系。表示资源没有改变,可直接使用客户端未过期的缓存。在请求附带条件时有可能返回这个状态码。
(4)4xx
客户端错误
400 Bad Request
请求中有语法错误。如参数拼接的的问题等。
401 Unauthorized
未认证
403
禁止访问
404 Not Found
(5)5xx
服务器错误
500
服务器内部错误
503
服务不可用
5.host首部的作用
区分不同的主机。有些服务器运行多个网站,每个网站有不同的域名。当接收请求时如果不指定域名则无法知道需要哪个网站响应。
以上是关于Ansible-playbook练习&http报文结构总结的主要内容,如果未能解决你的问题,请参考以下文章