脚本宝典收集整理的这篇文章主要介绍了Ansible和HTTP,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
常用模块帮助文档参考:
https://docs.ansible.com/ansible/2.9/modules/modules_by_category.html
常用模块使用说明如下:
command模块
功能:在远程主机上执行命令,此为默认模块,可忽略-m选项
注意:此命令不支持$VARNAME<>|;&等,可用shell模块实现
注意:此模块不具有幂等性
范例:
ansible websrvs -m command -a 'service vfstpd start'
ansible websrvs -m command -a 'echo root|passwd --stdin 123456'
ansible websrvs -m command -a 'rm -rf /data/'
ansible websrvs -m command -a 'echo hello > /data/hello.log'
ansible websrvs -m command -a "echo $HOSTNAME"
shell模块
功能:和command模块相似,用shell执行命令,支持各种符号,比如:*,$,>
注意:此模块不具有幂等性
范例:
ansible websrvs -m shell -a "echo $HOSTNAME"
ansible websrvs -m shell -a 'echo $HOSTNAME'
ansible websrvs -m shell -a 'echo root | passwd --stdin 123456'
ansible websrvs -m shell -a 'ls -l /etc/shadow'
ansible websrvs -m shell -a 'echo hello > /data/hello.log'
ansible websrvs -m shell -a 'cat /data/hello.log'
注意:调佣bash执行命令,类似cat /tmp/test.cmd | awk -F'|' '{print $1,$2}' &>/tmp/example.txt
这些复杂命令,即使使用shell也可能会失败,解决办法:写到脚本时,copy到远程,执行,再把需要的结果拉回执行命令的机器
范例:将shell模块代替command,设为默认模块
vim /etc/ansible/ansible.cfg
#修改下面一行
module_name=shell
script模块
功能:在远程主机上运行ansilbe服务器上的脚本(无需脚本具有执行权限)
注意:此模块不具有幂等性
范例:
ansible websrvs -m script -a /data/test.sh
copy模块
功能:从ansible服务器主控端复制文件至远程主机
注意:src=file 如果没指明路径,则为当前目录或当前目录下的files目录下的file文件
范例:
#如目标存在,默认覆盖,此次指定先备份
ansible websrvs -m copy -a "src=/root/test1.sh dest=/tmp/test2.sh owner=wang mode=600 backup=yes"
#指定内容,直接生成目标文件
ansible websrvs -m copy -a "content='test line1ntest line2n' dest=/tmp/test.txt"
#复制/etc目标本身,注意/etc后面没有/
ansible websrvs -m copy -a "src=/etc dest=/backup"
#复制/etc/下的文件,不包括/etc目录自身,注意/etc/后面有/
ansible websrvs -m copy -a "src=/etc/ dest=/backup"
get_url模块
功能:用于将文件从HTTP、HTTPS或FTP下载到被管理机节点上
常用参数如下:
url: 下载文件的URL,支持HTTP,HTTPS或FTP协议
dest: 下载到目标路径(绝对路径),如果目标是一个目录,就用服务器上面文件的名称,如果目标设置了名称就用目标设置的名称
owner:指定属主
group:指定属组
mode:指定权限
force: 如果yes,dest不是目录,将每次下载文件,如果内容改变,替换文件。如果否,则只有在目标不存在时才会下载该文件
checksum: 对目标文件在下载后计算摘要,以确保其完整性
示例: checksum="sha256:D98291AC[...]B6DC7B97",
checksum="sha256:http://example.com/path/sha256sum.txt"
url_username: 用于HTTP基本认证的用户名。 对于允许空密码的站点,此参数可以不使用url_username
url_password: 用于HTTP基本认证的密码。 如果未指定`url_username'参数,则不会使用url_password参数
validate_certs:如果“no”,SSL证书将不会被验证。 适用于自签名证书在私有网站上使用
timeout: URL请求的超时时间,秒为单位
范例:
ansible websrvs -m get_url -a 'url=http://nginx.org/download/nginx-1.18.0.tar.gz dest=/usr/local/src/nginx.tar.gz checksum="md5:b2d33d24d89b8b1f87ff5d251aa27eb8"'
fetch模块
功能:从远程主机提取文件至ansible的主控端,与copy相反,目前不支持目录
范例:
ansible websrvs -m fetch -a 'src=/root/test.sh dest=/data/scripts'
file模块
功能:设置文件属性,创建软链接等
范例:
#创建文件
ansible all -m file -a 'path=/data/test1.txt state=touch'
ansible all -m file -a 'path=/data/test2.log state=absent'
ansible all -m file -a 'path=/data/test3.sh owner=root mode=755'
#创建目录
ansible all -m file -a 'path=/data/mysql state=directory owner=mysql group=mysql'
#创建软链接
ansible all -m file -a 'src=/data/testfile path=/data/testfile-link state=link'
#创建目录
ansible all -m file -a 'path=/data/testdir state=directory'
#递归修改目录属性,但不递归至子目录
ansible all -m file -a 'path=/data/testdir state=directory owner=test group=test'
#递归修改目录及子目录的属性
ansible all -m file -a 'path=/data/testdir state=directory owner=test group=test recurse=yes'
stat模块
功能:检查文件或文件系统的状态
注意:对于windows目标,请使用win_stat模块
选项:
path:文件/对象的完整路径(必须)
常用的返回值判断:
exists:判断是否存在
isuid:调用用户的ID与所有者ID是否匹配
范例:
[root@master ~]# ansible 127.0.0.1 -m stat -a 'path=/etc/passwd'
127.0.0.1 | SUCCESS => {
"changed": false,
"stat": {
"atime": 1653664578.5329995,
"attr_flags": "",
"attributes": [],
"block_size": 4096,
"blocks": 8,
"charset": "us-ascii",
"checksum": "15c497413c76cac5d7aae22f6d643a1ae0fa72b2",
"ctime": 1650713473.118016,
"dev": 64769,
"device_type": 0,
"executable": false,
"exists": true,
"gid": 0,
"gr_name": "root",
"inode": 17717276,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mimetype": "text/plain",
"mode": "0644",
"mtime": 1650713473.1170158,
"nlink": 1,
"path": "/etc/passwd",
"pw_name": "root",
"readable": true,
"rgrp": true,
"roth": true,
"rusr": true,
"size": 1116,
"uid": 0,
"version": "1248249178",
"wgrp": false,
"woth": false,
"writeable": true,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
}
}
unarchive模块
功能:解包解压缩
实现有两种用法:
常见参数:
copy:默认为yes,当copy=yes,拷贝的文件是从ansible主机复制到远程主机上,若copy=no,会在远程主机寻找src源文件
remote_src:和copy功能一样且互斥,yes表示在远程主机,不在ansible主机,no表示文件在ansible主机上
src:源路径,可以是ansible主机的路径,也可以是远程主机(被管理端或者第三方主机)上的路径,如果是远程主机的路径,则需要设置copy=no
dest:远程主机上的目标路径
mode:设置解压缩后的文件权限
范例:
ansible all -m unarchive -a 'src=/data/ansible-20210521.tar dest=/opt owner=shichu group=shichu'
ansible all -m unarchive -a 'src=/data/ansible-20210521.tar dest=/opt copy=no mode=0777'
ansible all -m unarchive -a 'src=https://example.com/example.zip dest=/data copy=no'
ansible websrvs -m unarchive -a 'src=https://releases.ansible.com/ansible/ansible-2.1.6.0-0.1.rc1.tar.gz dest=/data/ owner=root remote_src=yes'
ansible websrvs -m unarchive -a 'src=http://nginx.org/download/nginx-1.18.0.tar.gz dest=/usr/local/src/ copy=no'
archive模块
功能:压缩打包保存至远程主机
范例:
ansible websrvs -m archive -a 'path=/var/log dest=/opt/log.tar format=tar owner=shichu mode=0600'
hostname模块
功能:设置远程主机的主机名
范例:
ansible websrvs -m hostname -a "name=websrv"
cron模块
功能:计划任务
支持时间:minute,hour,day,month,weekday
范例:
#备份数据库脚本
cat /root/mysql_backup.sh
#!/bin/bash
mysqldump -A -F --single-transaction --master-data=2 -q -uroot |gzip > /data/mysql_`date +%F_%T`.sql.gz
#创建任务
ansible websrvs -m cron -a "minute=*/5 job='/usr/bin/chronyc sources -v &>/dev/null' name=Synctime"
#禁用任务计划
ansible websrvs -m cron -a "minute=*/5 job='/usr/bin/chronyc sources -v &>/dev/null' name=Synctime disabled=yes"
#启用任务
ansible websrvs -m cron -a "minute=*/5 job='/usr/bin/chronyc sources -v &>/dev/null' name=Synctime disabled=no"
#删除任务
ansible websrvs -m cron -a "name=Synctime state=absent"
yum和apt模块
功能:
yum管理软件包,只支持RHEL,CentOS,fedora,不支持Ubuntu其他版本
apt管理Debian相关版本的软件包
范例:
#安装
ansible websrvs -m yum -a 'name=httpd state=present'
#启用epel源安装
ansible websrvs -m yum -a 'name=nginx state=present enablerepo=epel'
#升级除kernel和foo开头以外的所有包
ansible websrvs -m yum -a 'name=* state=lastest exclude=kernel*,foo*'
#删除
ansible websrvs -m yum -a 'name=httpd state=absent'
yum_repository模块
功能:管理远程主机上的yum仓库
参数:
name:相当于.repo文件定义中括号的[仓库ID]
baseurl:相当于.repo文件中baseurl
description:相当于.repo文件中的name
file:相当于.repo文件的名称,不使用时默认以name加.repo命令
enabled=yes|no:相当于.repo文件中enabled
gpgcheck=yes|no:相当于.repo文件中gpgcheck
gpgcakey:前提是gpgcheck=yes,相当于.repo文件中gpgkey,验证gpg公钥
state=present|absent:默认present,absent表示删除
范例:
#创建e阿里云pel源
ansible websrvs -m yum_repository -a 'name=aliepel baseurl=https://mirrors.aliyun.com/epel/7/x86_64/ enabled=yes gpgcheck=yes gpgcakey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-7 state=present file=AlicloudEpel'
#删除阿里云epel源
ansible websrvs -m yum_repository -a 'file=AlicloudEpel name=aliepel state=absent'
service模块
功能:管理服务
范例:
ansible websrvs -m service -a 'name=httpd state=started enabled=yes'
ansible websrvs -m service -a 'name=httpd state=stopped'
ansible websrvs -m service -a 'name=httpd state=reloaded'
user模块
功能:管理用户
范例:
#创建用户
ansible websrvs -m user -a 'name=user1 comment="test user" uid=2048 home=/app/user1 group=root'
ansible websrvs -m user -a 'name=nginx comment=nginx uid=88 group=nginx groups="root,daemon" shell=/sbin/nologin system=yes create_home=no home=/data/nginx non_unique=yes'
#remove=yes表示删除用户及家目录等数据,默认remove=no
ansible websrvs -m user -a 'name=nginx state=absent remove=yes'
group模块
功能:管理组
范例:
#创建组
ansible websrvs -m group -a 'name=nginx gid=88 system=yes'
#删除组
ansible websrvs -m group -a 'name=nginx state=absent'
lineinfile模块
ansible在使用sed进行替换时,经常会遇到需要转义的问题,而且ansible在遇到特殊符号进行替换时,存在问题,无法支持进程替换。其实在ansible自身提供了两个模块:lineinfile模块和replace模块,可以方便的进行替换。
一般在ansible当中去修改某个文件的单行进行替换的时候需要使用lineinfile模块
regexp参数:使用正则表达式匹配对应的行,当替换文本时,如果有多行文本都能被匹配,则只有最后被匹配到的那行文本才会被替换,当删除文件时,如果有多行文本都能被匹配,那么这些行都会被删除。
如果想进行多行匹配进行替换需要使用replace模块
功能:相当于sed,可以修改文件内容
范例:
ansible websrvs -m lineinfile -a "path=/etc/httpd/conf/httpd.conf regexp='^Listen' line='Listen 80'"
ansible websrvs -m lineinfile -a "path=/etc/selinux/config regexp='^SELINUX=' line='SELINUX=disabled'"
ansible websrvs -m lineinfile -a 'path=/etc/fstab state=absent regexp="^#"'
replace模块
功能:该模块有点类似于sed命令,主要也是基于正则进行匹配和替换,建议使用
范例:
ansible websrvs -m replace -a "path=/etc/fstab regexp='^(UUID.*)' replace='#1'"
ansible websrvs -m replace -a "path=/etc/fstab regexp='^#(UUID.*)' replace='1'"
selinux模块
功能:管理SELinux策略
范例:
ansible websrvs -m selinux -a 'state=disabled'
reboot模块
功能:重启远程主机
范例:
ansible websrvs -m reboot
mount模块
功能:挂载和卸载文件系统
范例:
#临时挂载
mount websrvs -m mount -a 'src="UUID=b3e48f45-f933-4c8e-a700-22a159ec9077" path=/home fstype=xfs opts=noatime state=present'
#临时取消挂载
mount websrvs -m mount -a 'path=/home fstype=xfs opts=noatime state=unmounted'
#永久挂载
ansible websrvs -m mount -a 'src=10.0.0.44:/data/wordpress path=/var/www/html/wp/content/uploads opts="_netdev" state=mounted'
#永久卸载
ansible websrvs -m mount -a 'src=10.0.0.44:/data/wordpress path=/var/www/html/wp/content/uploads state=absent'
setup模块
功能:setup模块来收集主机的系统信息,这些facts信息可以直接以变量的形式使用,但如果主机较多,会影响执行速度
注意:可以使用gather_facts: no来禁止ansible收集facts信息
范例:
ansible all -m setup
ansible all -m setup -a "filter=ansible_nodename"
ansible all -m setup -a "filter=ansible_hostname"
ansible all -m setup -a "filter=ansible_domain"
ansible all -m setup -a "filter=ansible_memtotal_mb"
ansible all -m setup -a "filter=ansible_memory_mb"
ansible all -m setup -a "filter=ansible_memfree_mb"
ansible all -m setup -a "filter=ansible_os_family"
ansible all -m setup -a "filter=ansible_distribution_major_version"
ansible all -m setup -a "filter=ansible_distribution_version"
ansible all -m setup -a "filter=ansible_processor_vcpus"
ansible all -m setup -a "filter=ansible_all_ipv4_addresses"
ansible all -m setup -a "filter=ansible_architecture"
ansible all -m setup -a "filter=ansible_uptime_seconds"
ansible all -m setup -a "filter=ansible_processor*"
ansible all -m setup -a 'filter=ansible_env'
debug模块
功能:此模块可以用于输出信息,并且通过msg定制输出信息内容
注意:msg后面的变量有时需要加""引起来
范例:
#debug模块默认输出Hello world
[root@master ~]# ansible websrvs -m debug
10.0.0.45 | SUCCESS => {
"msg": "Hello world!"
}
10.0.0.44 | SUCCESS => {
"msg": "Hello world!"
# 利用debug 模块输出变量
[root@master playbook]# cat debug.yaml
- hosts: websrvs
tasks:
- name: output variables
debug:
msg: Host "{{ ansible_nodename }}" IP "{{ ansible_default_ipv4.address}}"
[root@master playbook]# ansible-playbook debug.yaml
PLAY [websrvs] ***********************************************************************************************
TASK [Gathering Facts] ***************************************************************************************
ok: [10.0.0.44]
ok: [10.0.0.45]
TASK [output variables] **************************************************************************************
ok: [10.0.0.45] => {
"msg": "Host "client2" IP "10.0.0.45""
}
ok: [10.0.0.44] => {
"msg": "Host "client1" IP "10.0.0.44""
}
PLAY RECAP ***************************************************************************************************
10.0.0.44 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.0.0.45 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
#显示字符串特定字符
[root@master playbook]# cat debug-2.yaml
- hosts: websrvs
gather_facts: no
vars:
a: "12345"
tasks:
- debug:
msg: "{{a[2]}}"
[root@master playbook]# ansible-playbook debug-2.yaml
PLAY [websrvs] ***********************************************************************************************
TASK [debug] *************************************************************************************************
ok: [10.0.0.45] => {
"msg": "3"
}
ok: [10.0.0.44] => {
"msg": "3"
}
PLAY RECAP ***************************************************************************************************
10.0.0.44 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.0.0.45 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
palybook官方文档: https://docs.ansible.com/ansible/latest/reference_appendices/playbooks_keywords.html#playbook-keywords
palybook组件说明
一个playbook 中由多个组件组成,其中所用到的常见组件类型如下:
Hosts 执行的远程主机列表
Tasks 任务集,由多个task的元素组成的列表实现,每个task是一个字典,一个完整的代码块功能需最少元素需包括 name 和 task,一个name只能包括一个task
Variables 内置变量或自定义变量在playbook中调用
Templates 模板,可替换模板文件中的变量并实现一些简单逻辑的文件
Handlers 和 notify 结合使用,由特定条件触发的操作,满足条件方才执行,否则不执行
tags 标签 指定某条任务执行,用于选择运行playbook中的部分代码。ansible具有幂等性,因此会自动跳过没有变化的部分,即便如此,有些代码为测试其确实没有发生变化的时间依然会非常地长。此时,如果确信其没有变化,就可以通过tags跳过此些代码片断
准备条件:
开启ssh免密登录
ssh-keygen
ssh-copy-id 127.0.0.1
scp -r ~/.ssh 10.0.0.44:~/
下载mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz文件至/opt目录下
提前生成my.cnf配置文件至/opt目录下
my.cnf配置文件
[root@master opt]# cat my.cnf
[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
port=3306
socket=/data/mysql/mysql.sock
mysql5.7安装脚本
[root@master opt]# cat install_mysql.yml
# install mysql5.7
- hosts: 10.0.0.44
remote_user: root
gather_facts: no
vars:
version: "mysql-5.7.33-linux-glibc2.12-x86_64"
suffix: "tar.gz"
file: "{{version}}.{{suffix}}"
tasks:
- name: 安装依赖包
yum: name=libao,numactl-libs
- name: 新增mysql组
group: name=mysql
- name: 新增mysql用户
user: name=mysql group=mysql shell=/bin/false
- name: 解包
unarchive: src=/opt/{{file}} dest=/usr/local copy=yes
- name: 创建链接
file: src=/usr/local/{{version}} path=/usr/local/mysql state=link
- name: 修改属组
file: path=/usr/local/mysql state=directory owner=mysql group=mysql recurse=yes
- name: 配置环境变量
copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
- name: 环境变量生效
shell: source /etc/profile.d/mysql.sh
- name: 创建mysql目录
file: path=/data/mysql state=directory
- name: 生成mysql配置文件
copy: src=/opt/my.cnf dest=/etc/ backup=yes
- name: 初始化数据库,生成root空密码
shell: mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
- name: 准备服务脚本
shell: /usr/bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
- name: 启动服务
service: name=mysqld state=started enabled=yes
ansible-playbook执行过程
[root@master opt]# ansible-playbook install_mysql.yml
PLAY [10.0.0.44] ********************************************************************************************************************
TASK [安装依赖包] ************************************************************************************************************************
changed: [10.0.0.44]
TASK [新增mysql组] *********************************************************************************************************************
changed: [10.0.0.44]
TASK [新增mysql用户] ********************************************************************************************************************
changed: [10.0.0.44]
TASK [解包] ***************************************************************************************************************************
changed: [10.0.0.44]
TASK [创建链接] *************************************************************************************************************************
changed: [10.0.0.44]
TASK [修改属组] *************************************************************************************************************************
changed: [10.0.0.44]
TASK [配置环境变量] ***********************************************************************************************************************
changed: [10.0.0.44]
TASK [环境变量生效] ***********************************************************************************************************************
changed: [10.0.0.44]
TASK [创建mysql目录] ********************************************************************************************************************
changed: [10.0.0.44]
TASK [生成mysql配置文件] ******************************************************************************************************************
changed: [10.0.0.44]
TASK [初始化数据库] ***********************************************************************************************************************
changed: [10.0.0.44]
TASK [准备服务脚本] ***********************************************************************************************************************
changed: [10.0.0.44]
TASK [启动服务] *************************************************************************************************************************
[WARNING]: The service (mysqld) is actually an init script but the system is managed by systemd
changed: [10.0.0.44]
PLAY RECAP **************************************************************************************************************************
10.0.0.44 : ok=13 changed=13 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
登录远程主机查看mysql是否成功安装
[root@client1 ~]# systemctl status mysqld
● mysqld.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
Active: active (running) since Sun 2022-05-29 22:50:50 CST; 1min 23s ago
Docs: man:systemd-sysv-generator(8)
Process: 2226 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/mysqld.service
├─2236 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/mysql.pid
└─2388 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/p...
May 29 22:50:49 shichu systemd[1]: Starting LSB: start and stop MySQL...
May 29 22:50:50 shichu mysqld[2226]: Starting MySQL. SUCCESS!
May 29 22:50:50 shichu systemd[1]: Started LSB: start and stop MySQL.
[root@shichu ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 3
Server version: 5.7.33 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>
注意:
template文件必须存放于templates目录下,且命名为 .j2 结尾
yaml/yml 文件需和templates目录平级,目录结构如下示例:
[root@master data]# cat httpd.yml
- hosts: websrvs
remote_user: root
tasks:
- name: install httpd
yum: name=httpd
- name: template config
template: src=httpd.conf.j2 dest=/var/www/html/index.html
- name: start service
service: name=httpd state=started enabled=yes
Jinja2官方中文文档:
http://docs.jinkan.org/docs/jinja2/
https://www.w3cschool.cn/yshfid/
[root@master data]# cat templates/httpd.conf.j2
hostname {{ansible_hostname}}
[root@master data]# ansible-playbook httpd.yml
PLAY [websrvs] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [10.0.0.45]
ok: [10.0.0.44]
TASK [install httpd] ****************************************************************************************************************
ok: [10.0.0.45]
changed: [10.0.0.44]
TASK [template config] **************************************************************************************************************
changed: [10.0.0.45]
changed: [10.0.0.44]
TASK [start service] ****************************************************************************************************************
changed: [10.0.0.45]
changed: [10.0.0.44]
PLAY RECAP **************************************************************************************************************************
10.0.0.44 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.0.0.45 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@master data]# curl 10.0.0.44
hostname client1
[root@master data]# curl 10.0.0.45
hostname client2
[root@master data]#
访问不同主机,显示内容不同
http报文由三个部分组成,即开始行、首部行和实体主体。
在请求报文中,开始行是请求行。
request报文格式
<method> <request-URL> <version>
<headers>
<entity-body>
范例:
GET / HTTP/1.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7
Cache-Control: max-age=0
Connection: keep-alive
Cookie:
Host: www.baidu.com
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
在响应报文中,开始行是状态行。
状态行包括三项内容,即HTTP版本,状态码,以及解释状态码的简单短语。
response报文格式
<version> <status> <reason-phrase>
<headers>
<entity-body>
范例:
HTTP/1.1 200 OK
Bdpagetype: 2
Bdqid: 0xb3280c990017df87
Cache-Control: private
Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/html;charset=utf-8
Date: Sun, 29 May 2022 17:18:33 GMT
Expires: Sun, 29 May 2022 17:18:32 GMT
Server: BWS/1.1
Set-Cookie: BDSVRTM=363; path=/
Set-Cookie: BD_HOME=1; path=/
Set-Cookie: H_PS_PSSID=36429_36454_31253_34812_36424_36165_36487_36055_36235_26350_36467_36314; path=/; domain=.baidu.com
Strict-Transport-Security: max-age=172800
Traceid: 1653844713060698701812909582183128620935
X-Frame-Options: sameorigin
X-Ua-Compatible: IE=Edge,chrome=1
Transfer-Encoding: chunked
http协议状态码分类
1xx:100-101 信息提示
2xx:200-206 成功
3xx:300-307 重定向
4xx:400-415 错误类信息,客户端错误
5xx:500-505 错误类信息,服务器端错误
http协议常用状态码
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:网关超时
以上是脚本宝典为你收集整理的Ansible和HTTP全部内容,希望文章能够帮你解决Ansible和HTTP所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。