Linux筆記 -- Week13 Q&A

1粹断、ansible-playbook實(shí)現(xiàn)MySQL的二進(jìn)制部署

準(zhǔn)備工作

ansible 免密鑰配置

ssh-keygen # 生成密鑰對(duì)# 為兩臺(tái)被控端傳輸密鑰瓶埋,方便ansible進(jìn)行管理
ssh-copy-id -i root@10.0.0.161

編輯主機(jī)清單

[root@centos-160 ansible]# vim /data/ansible/hosts
#末尾添加
[dbsrvs]
10.0.0.161

創(chuàng)建MySQL8角色目錄

mkdir -pv /data/ansible/roles/mysql8/{defaults,files,handlers,meta,tasks,templates,vars}

files目錄文件準(zhǔn)備

[root@centos-160 mysql8]# tree files/
files/
└── mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz

0 directories, 1 file

templates目錄

[root@centos-160 mysql8]# tree templates/
templates/
└── my.cnf.j2

0 directories, 1 file


注意:配置文件里面如果設(shè)置了 pid-file參數(shù)养筒,pid文件所在的文件夾owner和group需設(shè)置為mysql闽颇,默認(rèn)放在data目錄下可以不用額外設(shè)置(data目錄默認(rèn)設(shè)置為 mysql:mysql)

[root@centos-160 templates]# cat templates/my.cnf.j2
# Ansible managed

[client]
#password = your_password
port = 3306
#socket = /var/lib/mysql/mysql.sock
socket = /data/mysql/mysql.sock
default-character-set=utf8mb4

[mysql]
default-character-set=utf8mb4

[mysqld]
default_authentication_plugin = mysql_native_password
user = mysql
port = 3306
datadir = /data/mysql
#pid-file = /data/mysql/mysqld/mysqld.pid
socket = /data/mysql/mysql.sock
skip-name-resolve

general-log = 1
general_log_file = /data/mysql/mysqld.log
log-error = /data/mysql/mysql.err

...省略

# Other settings.

# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#

vars目錄

[root@centos-160 mysql8]# tree vars/
vars/
└── main.yml

0 directories, 1 file

[root@centos-160 mysql8]# cat vars/main.yml 
---
mysql_version: 8.0.23
mysql_file: mysql-{{ mysql_version }}-linux-glibc2.12-x86_64.tar.xz
mysql_dir: mysql-{{ mysql_version }}-linux-glibc2.12-x86_64
mysql_root_password: 123qwe@MYSQL

tasks目錄

[root@centos-160 mysql8]# tree tasks/
tasks/
├── conf.yml
├── data.yml
├── group.yml
├── install.yml
├── linkfile.yml
├── main.yml
├── path.yml
├── script.yml
├── secure.yml
├── service.yml
├── unarchive.yml
└── user.yml

main.yml 任務(wù)執(zhí)行主文件

[root@centos-160 ansible]# cat roles/mysql8/tasks/main.yml 
---
- name: Install Requirements
  include: install.yml
- name: Add Group
  include: group.yml
- name: Add User
  include: user.yml
- name: Unarchive File
  include: unarchive.yml
- name: Add Link
  include: linkfile.yml
- name: Initialize with Datadir
  include: data.yml
- name: Manage Conf File
  include: conf.yml
- name: Manage Service Script
  include: script.yml
- name: Manage PATH
  include: path.yml
- name: Manage Service
  include: service.yml
- name: Set PASSWD
  include: secure.yml

install.yml 安裝依賴

[root@centos-160 ansible]# cat roles/mysql8/tasks/install.yml 
---
- name: Install mysql requirements
  yum:
    name:
      - libaio
      - numactl-libs
    state: present

group.yml 創(chuàng)建組

[root@centos-160 ansible]# cat roles/mysql8/tasks/group.yml 
---
- name: Create Group
  group:
    name: mysql
    gid: 306
    state: present

user.yml 創(chuàng)建用戶

[root@centos-160 ansible]# cat roles/mysql8/tasks/user.yml 
---
- name: Add User
  user:
    name: mysql
    uid: 306
    group: mysql
    shell: /sbin/nologin
    system: yes
    create_home: no
    home: /data/mysql

unarchive.yml 解壓縮

[root@centos-160 ansible]# cat roles/mysql8/tasks/unarchive.yml 
---
- name: Copy tar pkgs to remote machine and unarchive
  unarchive:
    src: '{{ mysql_file }}'
    dest: /usr/local
    owner: root
    group: root

linkfile.yml 創(chuàng)建鏈接

[root@centos-160 ansible]# cat roles/mysql8/tasks/linkfile.yml 
---
- name: Add linkfile
  file:
    src: '/usr/local/{{ mysql_dir }}'
    dest: '/usr/local/mysql'
    state: link
    
#可省略如下步驟
- name: Change owner
  file:
    path: '/usr/local/mysql'
    state: directory
    recurse: yes
    owner: root
    group: root
    mode: '0755'

path.yml 設(shè)置環(huán)境變量

[root@centos-160 ansible]# cat roles/mysql8/tasks/path.yml 
---
- name: Manage PATH Variable
  copy:
    content: 'PATH=$PATH:/usr/local/mysql/bin'
    dest: '/etc/profile.d/mysql-env.sh'

- name: Active env variable
  shell:
    cmd: bash mysql-env.sh
    chdir: '/etc/profile.d/'

data.yml 初始化工作目錄

[root@centos-160 ansible]# cat roles/mysql8/tasks/data.yml 
---
- name: Initialize mysql data
  shell: /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
  tags: init_data

conf.yml 復(fù)制配置文件模板

[root@centos-160 ansible]# cat roles/mysql8/tasks/conf.yml 
---
- name: Conf my.cnf
  template:
    src: my.cnf.j2
    dest: /etc/my.cnf

script.yml 添加服務(wù)腳本

[root@centos-160 ansible]# cat roles/mysql8/tasks/script.yml 
---
- name: Service script
  shell: /usr/bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

service.yml 配置服務(wù)啟動(dòng)

[root@centos-160 ansible]# cat roles/mysql8/tasks/service.yml 
---
- name: Enabled service
  shell: /usr/sbin/chkconfig --add mysqld; /etc/init.d/mysqld start
  tags: service

secure.yml 設(shè)置密碼

[root@centos-160 ansible]# cat roles/mysql8/tasks/secure.yml 
---
- name: Set password
  shell: /usr/local/mysql/bin/mysqladmin -uroot password '{{ mysql_root_password }}'

執(zhí)行MySQL安裝角色:role_mysql.yml

[root@centos-160 ansible]# cat role_mysql.yml 
---
- hosts: dbsrvs
 remote_user: root
 gather_facts: no

 roles:
   - mysql8

##執(zhí)行安裝角色
[root@centos-160 ansible]# ansible-playbook  role_mysql.yml

2剩膘、Ansible playbook實(shí)現(xiàn)apache批量部署,并對(duì)不同主機(jī)提供以各自IP地址為內(nèi)容的index.html

準(zhǔn)備工作

ansible免密鑰配置

ssh-keygen # 生成密鑰對(duì)# 為兩臺(tái)被控端傳輸密鑰怠褐,方便ansible進(jìn)行管理
ssh-copy-id -i root@10.0.0.161
ssh-copy-id -i root@10.0.0.162

編輯主機(jī)清單文件

[root@centos-160 ansible]# vim /data/ansible/hosts
#末尾添加
[websrvs]
10.0.0.161
10.0.0.162

創(chuàng)建apache-httpd角色目錄

mkdir -pv /data/ansible/roles/httpd/{defaults,files,handlers,meta,tasks,templates,vars}

最終目錄文件結(jié)構(gòu)

[root@centos-160 ansible]# tree roles/httpd/
roles/httpd/
├── defaults
│   └── main.yml
├── files
│   └── httpd_ssl_certificate.sh
├── handlers
│   └── main.yml
├── meta
├── tasks
│   ├── certificates.yml
│   ├── group.yml
│   ├── httpd_conf.yml
│   ├── index.yml
│   ├── install.yml
│   ├── main.yml
│   ├── service.yml
│   ├── ssl_conf.yml
│   ├── status.yml
│   └── user.yml
├── templates
│   ├── httpd.conf.j2
│   ├── index.html.j2
│   ├── ssl.conf.j2
│   └── status.conf.j2
└── vars
    └── RedHat.yml

defaults目錄

[root@centos-160 ansible]# tree roles/httpd/defaults/
roles/httpd/defaults/
└── main.yml

0 directories, 1 file

----
----
[root@centos-160 ansible]# cat roles/httpd/defaults/main.yml 
# roles/httpd/defaults/main.yml
---
httpd_access_log: logs/access_log
httpd_access_log_ssl: logs/ssl_access_log
httpd_document_root: '/var/www/html'
httpd_error_log: logs/error_log
httpd_error_log_ssl: logs/ssl_error_log
httpd_extended_status: 'On'
httpd_listen: 80
httpd_listen_ssl: 443
httpd_log_level: warn
httpd_log_level_ssl: warn
httpd_server_admin: root@localhost
httpd_server_root: '/etc/httpd'
httpd_server_tokens: Prod
httpd_ssl_certificate_file: localhost.crt
httpd_ssl_certificate_key_file: localhost.key
httpd_status_enable: false
httpd_status_location: '/server-status'
httpd_status_require: 'host localhost'

# SSL configuration, "Modern profile" according to:
# https://mozilla.github.io/server-side-tls/ssl-config-generator/?server=apache-2.4.34&openssl=1.1.0i&hsts=yes&profile=modern
httpd_ssl_cipher_suite: >
  'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256'
httpd_ssl_compression: 'off'
httpd_ssl_honor_cipher_order: 'on'
httpd_ssl_protocol: 'all -SSLv3 -TLSv1 -TLSv1.1'
httpd_ssl_session_tickets: 'off'
httpd_ssl_stapling_responder_timeout: 5
httpd_ssl_stapling_return_responder_errors: 'off'
httpd_ssl_stapling_cache: 'shmcb:/var/run/ocsp(128000)'
httpd_ssl_use_stapling: 'on'

files目錄

[root@centos-160 ansible]# tree roles/httpd/files/
roles/httpd/files/
└── httpd_ssl_certificate.sh

[root@centos-160 ansible]# cat roles/httpd/files/httpd_ssl_certificate.sh 
#!/bin/bash

umask 077

if [ -f /etc/pki/tls/private/localhost.key -o -f /etc/pki/tls/certs/localhost.crt ]; then
   exit 0
fi

/usr/bin/openssl genrsa -rand /proc/apm:/proc/cpuinfo:/proc/dma:/proc/filesystems:/proc/interrupts:/proc/ioports:/proc/pci:/proc/rtc:/proc/uptime 2048 > /etc/pki/tls/private/localhost.key 2> /dev/null

FQDN=`hostname`
if [ "x${FQDN}" = "x" -o ${#FQDN} -gt 59 ]; then
   FQDN=localhost.localdomain
fi

cat << EOF | /usr/bin/openssl req -new -key /etc/pki/tls/private/localhost.key \
         -x509 -sha256 -days 365 -set_serial $RANDOM -extensions v3_req \
         -out /etc/pki/tls/certs/localhost.crt 2>/dev/null
--
SomeState
SomeCity
SomeOrganization
SomeOrganizationalUnit
${FQDN}
root@${FQDN}
EOF

handlers目錄

[root@centos-160 ansible]# tree roles/httpd/handlers/
roles/httpd/handlers/
└── main.yml

0 directories, 1 file

[root@centos-160 ansible]# cat roles/httpd/handlers/main.yml 
# roles/httpd/handlers/main.yml
---
- name: restart httpd
  service:
   name: httpd
   state: restarted

tasks目錄

[root@centos-160 ansible]# tree roles/httpd/tasks/
roles/httpd/tasks/
├── certificates.yml
├── group.yml
├── httpd_conf.yml
├── index.yml
├── install.yml
├── main.yml
├── service.yml
├── ssl_conf.yml
├── status.yml
└── user.yml

0 directories, 10 files

main.yml文件

[root@centos-160 ansible]# cat roles/httpd/tasks/main.yml 
---
- include_vars: "{{ item }}"
  with_first_found:
    - "{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.yml"
    - "{{ ansible_distribution }}.yml"
    - "{{ ansible_os_family }}-{{ ansible_distribution_major_version }}.yml"
    - "{{ ansible_os_family }}.yml"
    - "{{ ansible_distribution_file_variety }}-{{ ansible_distribution_major_version }}.yml"
    - "{{ ansible_distribution_file_variety }}.yml"

- name: Add Group
  include: group.yml
- name: Add User
  include: user.yml
- name: Install apache-httpd
  include: install.yml
  tags: httpd-pkgs
- name: Conf httpd.conf
  include: httpd_conf.yml
  tags: httpd_conf
- name: Conf status.conf
  include: status.yml
  tags: status.conf
- name: Install certificate files
  include: certificates.yml
  tags: ssl_conf
- name: Conf mod_ssl file
  include: ssl_conf.yml
  tags: ssl_conf
- name: Copy Index.html
  include: index.yml
  tags: index
- name: Manage service
  include: service.yml
  tags: service

group.yml文件

---
- name: Add Group
  group:
    name: apache
    gid: 48
    system: yes
    state: present

user.yml文件

[root@centos-160 ansible]# cat roles/httpd/tasks/user.yml 
---
- name: Add User
  user: 
    name: apache
    uid: 48
    group: apache
    shell: /sbin/nologin
    home: /usr/share/httpd
    system: yes
    state: present

install.yml文件

[root@centos-160 ansible]# cat roles/httpd/tasks/install.yml 
---
- name: Install Apache httpd
  package:
    name: "{{ item }}"
    state: present
  with_items: "{{ httpd_packages }}"

httpd_conf.yml文件

[root@centos-160 ansible]# cat roles/httpd/tasks/httpd_conf.yml 
---
- name: Conf httpd_conf
  template:
    src: httpd.conf.j2
    dest: "{{ httpd_config }}"
    owner: root
    group: root
    setype: httpd_config_t
    mode: "0644"
  notify: restart httpd

certificates.yml 文件

# roles/httpd/tasks/certificates.yml
# Install certificate files on the target host
---

- name: Check if default SSL certificate exists
  stat:
    path: "{{ httpd_cert_dir }}/localhost.crt"
  register: ssl_cert_file
  when: httpd_ssl_certificate_file == 'localhost.crt'

- name: Copy scripts to remote node for generate keys
  copy:
    src: httpd_ssl_certificate.sh
    dest: /etc/pki/tls/httpd_ssl_certificate.sh
  when:
    - httpd_ssl_certificate_file == 'localhost.crt'
    - not ssl_cert_file.stat.exists

- name: Generate default SSL certificate
  shell: /bin/bash /etc/pki/tls/httpd_ssl_certificate.sh
  when: >
    httpd_ssl_certificate_file == 'localhost.crt'
    and not ssl_cert_file.stat.exists

# follow 4 copy steps can be deleted
- name: Copy user defined key file
  copy:
    src: "{{ httpd_ssl_certificate_key_file }}"
    dest: "{{ httpd_key_dir }}/{{ httpd_ssl_certificate_key_file }}"
  when: httpd_ssl_certificate_key_file != 'localhost.key'

- name: Copy custom certificate file
  copy:
    src: "{{ httpd_ssl_certificate_file }}"
    dest: "{{ httpd_cert_dir }}/{{ httpd_ssl_certificate_file }}"
  when: httpd_ssl_certificate_file != 'localhost.crt'

- name: Copy custom certificate chain file
  copy:
    src: "{{ httpd_ssl_certificate_chain_file }}"
    dest: "{{ httpd_cert_dir }}/{{ httpd_ssl_certificate_chain_file }}"
  when: httpd_ssl_certificate_chain_file is defined

- name: Copy custom CA certificate file
  copy:
    src: "{{ httpd_ssl_ca_certificate_file }}"
    dest: "{{ httpd_cert_dir }}/{{ httpd_ssl_ca_certificate_file }}"
  when: httpd_ssl_ca_certificate_file is defined

status.conf.yml 文件

[root@centos-160 ansible]# cat roles/httpd/tasks/status.yml 
---
- name: Conf status file
  template:
    src: status.conf.j2
    dest: "{{ httpd_status_config }}"
    owner: root
    group: root
    setype: httpd_config_t
    mode: '0644'
  notify: restart httpd
  when: httpd_status_enable

ssl_conf.yml 文件

[root@centos-160 ansible]# cat roles/httpd/tasks/ssl_conf.yml 
---
- name: Conf ssl.conf
  template:
    src: ssl.conf.j2
    dest: "{{ httpd_ssl_config }}"
    owner: root
    group: root
    setype: httpd_config_t
    mode: "0644"
  notify: restart httpd

index.yml 文件

[root@centos-160 ansible]# cat roles/httpd/tasks/index.yml 
---
- name: Copy index.html
  template:
    src: index.html.j2
    dest: "{{ httpd_document_root }}/index.html"

service.yml 文件

[root@centos-160 ansible]# cat roles/httpd/tasks/service.yml 
---
- name: Manage httpd service
  service:
    name: httpd
    state: started
    enabled: true

templates目錄

[root@centos-160 ansible]# tree roles/httpd/templates/
roles/httpd/templates/
├── httpd.conf.j2
├── index.html.j2
├── ssl.conf.j2
└── status.conf.j2

0 directories, 4 files

index.html.j2 文件

[root@centos-160 ansible]# cat roles/httpd/templates/index.html.j2 
<h1> {{ ansible_eth0.ipv4.address }} TEST </h1>

httpd.conf.j2 文件

[root@centos-160 ansible]# cat ./roles/httpd/templates/httpd.conf.j2 
# Apache HTTP server - main configuration
#
# {{ ansible_managed }}

## General configuration
ServerRoot {{ httpd_server_root }}
Listen {{ httpd_listen }}

Include conf.modules.d/*.conf

User apache
Group apache

## 'Main' server configuration
ServerAdmin {{ httpd_server_admin }}
{% if httpd_server_name is defined %}
ServerName {{ httpd_server_name }}
{% endif %}

ServerTokens {{ httpd_server_tokens }}

# Deny access to the entirety of your server's filesystem.
<Directory />
    AllowOverride none
    Require all denied
</Directory>

DocumentRoot {{ httpd_document_root }}

# Relax access to content within /var/www.
<Directory "/var/www">
    AllowOverride None
    Require all granted
</Directory>

# Further relax access to the default document root:
<Directory "{{ httpd_document_root }}">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

# Load index.html if directory is requested
<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

# Prevent .htaccess and .htpasswd files from being viewed by Web clients. 
<Files ".ht*">
    Require all denied
</Files>

# Logging
ErrorLog "{{ httpd_error_log }}"
LogLevel {{ httpd_log_level }}


<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>
      # You need to enable mod_logio.c to use %I and %O
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>

    CustomLog "{{ httpd_access_log }}" combined
</IfModule>

# CGI
<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>

<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>

<IfModule mime_module>
    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>

AddDefaultCharset UTF-8

<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>

EnableSendfile on

# Supplemental configuration
IncludeOptional conf.d/*.conf

# vim: ft=apache

status.conf.j2 文件

[root@centos-160 ansible]# cat roles/httpd/templates/status.conf.j2 
ExtendedStatus {{ httpd_extended_status }}

<Location "{{ httpd_status_location }}">
    SetHandler server-status
    Require {{ httpd_status_require }}
</Location>

ssl.conf.j2 文件

# Apache TLS configuration
#
# {{ ansible_managed }}

# Listen on port
Listen {{ httpd_listen_ssl }} https

## SSL Global Context
SSLPassPhraseDialog exec:/usr/libexec/httpd-ssl-pass-dialog
SSLSessionCache         shmcb:/run/httpd/sslcache(512000)
SSLSessionCacheTimeout  300
SSLRandomSeed startup file:/dev/urandom  256
SSLRandomSeed connect builtin
SSLCryptoDevice builtin

## SSL Virtual Host Context
<VirtualHost _default_:{{ httpd_listen_ssl }}>

SSLEngine on

ErrorLog {{ httpd_error_log_ssl }}
TransferLog {{ httpd_access_log }}
LogLevel {{ httpd_log_level_ssl }}

# Certificate files
SSLCertificateFile {{ httpd_cert_dir }}/{{ httpd_ssl_certificate_file }}
SSLCertificateKeyFile {{ httpd_key_dir }}/{{ httpd_ssl_certificate_key_file }}
{% if httpd_ssl_certificate_chain_file is defined %}
SSLCertificateChainFile {{ httpd_cert_dir }}/{{ httpd_ssl_certificate_chain_file }}
{% endif %}
{% if httpd_ssl_ca_certificate_file is defined %}
SSLCACertificateFile {{ httpd_cert_dir }}/{{ httpd_ssl_ca_certificate_file }}
{% endif %}

<Files ~ "\.(cgi|shtml|phtml|php3?)$">
    SSLOptions +StdEnvVars
</Files>
<Directory "/var/www/cgi-bin">
    SSLOptions +StdEnvVars
</Directory>

BrowserMatch "MSIE [2-5]" \
         nokeepalive ssl-unclean-shutdown \
         downgrade-1.0 force-response-1.0

CustomLog logs/ssl_request_log \
          "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"

</VirtualHost>

# SSL Configuration
# see https://mozilla.github.io/server-side-tls/ssl-config-generator/
SSLProtocol {{ httpd_ssl_protocol }}
SSLCipherSuite {{ httpd_ssl_cipher_suite }}
SSLHonorCipherOrder {{ httpd_ssl_honor_cipher_order }}
SSLCompression {{ httpd_ssl_compression }}
SSLSessionTickets {{ httpd_ssl_session_tickets }}

# OCSP Stapling, only in httpd 2.3.3 and later
SSLUseStapling {{ httpd_ssl_use_stapling }}
SSLStaplingResponderTimeout {{ httpd_ssl_stapling_responder_timeout }}
SSLStaplingReturnResponderErrors {{ httpd_ssl_stapling_return_responder_errors }}
SSLStaplingCache {{ httpd_ssl_stapling_cache }}

vars目錄

[root@centos-160 ansible]# tree ./roles/httpd/vars/
./roles/httpd/vars/
└── RedHat.yml

0 directories, 1 file

---
---
[root@centos-160 ansible]# cat ./roles/httpd/vars/RedHat.yml
# roles/httpd/vars/RedHat.yml
---
httpd_packages:
  - hostname  # needed for generating default certificate file
  - httpd
  - mod_ssl

httpd_config: /etc/httpd/conf/httpd.conf
httpd_ssl_config: /etc/httpd/conf.d/ssl.conf
httpd_status_config: /etc/httpd/conf.d/status.conf

httpd_cert_dir: /etc/pki/tls/certs
httpd_key_dir: /etc/pki/tls/private
[root@centos-160 ansible]#

安裝及驗(yàn)證

##安裝文件
[root@centos-160 ansible]# cat role_httpd.yml
---
- hosts: websrvs
  remote_user: root
  gather_facts: yes

  roles:
    - httpd

##驗(yàn)證
[root@centos-160 ansible]# curl https://10.0.0.161 -k
<h1> 10.0.0.161 TEST </h1>
[root@centos-160 ansible]# curl https://10.0.0.162 -k
<h1> 10.0.0.162 TEST </h1>
[root@centos-160 ansible]#

3浴捆、http的報(bào)文結(jié)構(gòu)和狀態(tài)碼總結(jié)

HTTP 報(bào)文結(jié)構(gòu)

參考鏈接:HTTP 消息 - HTTP | MDN (mozilla.org)

HTTP 消息是服務(wù)器和客戶端之間交換數(shù)據(jù)的方式选泻。有兩種類型的消息︰

  • 請(qǐng)求(requests)-- 由客戶端發(fā)送用來觸發(fā)一個(gè)服務(wù)器上的動(dòng)作美莫;

  • 響應(yīng)(responses)-- 來自服務(wù)器的應(yīng)答茂嗓。

HTTP 請(qǐng)求和響應(yīng)報(bào)文結(jié)構(gòu)由以下部分組成:

  1. 起始行 -- 用于描述要執(zhí)行的請(qǐng)求,或是對(duì)應(yīng)的響應(yīng)狀態(tài)忿族。

  2. 可選的HTTP頭集合 -- 指明請(qǐng)求或描述消息正文道批。

  3. 空白行 -- 指示所有關(guān)于請(qǐng)求的元數(shù)據(jù)已經(jīng)發(fā)送完畢

  4. 消息主體 -- 包含請(qǐng)求相關(guān)數(shù)據(jù)的正文入撒,或響應(yīng)的相關(guān)文檔茅逮。

請(qǐng)求報(bào)文和響應(yīng)報(bào)文結(jié)構(gòu)的具體說明如下:

  • 請(qǐng)求報(bào)文
起始行 -- 在請(qǐng)求報(bào)文里亦稱為請(qǐng)求行,包括HTTP請(qǐng)求方法碉考、請(qǐng)求目標(biāo)(通常為URL)侯谁、HTTP協(xié)議版本章钾。
HTTP頭部集合 -- 在請(qǐng)求報(bào)文里亦稱為請(qǐng)求首部字段贱傀,總體可分組為通用首部字段、請(qǐng)求首部字段串纺、實(shí)體首部字段纺棺。
空行
消息主體 -- 在請(qǐng)求報(bào)文里亦稱為請(qǐng)求報(bào)文主體。注意并不是每一個(gè)請(qǐng)求報(bào)文都有消息主體茅撞。
  • 響應(yīng)報(bào)文
起始行 -- 在響應(yīng)報(bào)文里亦稱為狀態(tài)行米丘,包括HTTP協(xié)議版本糊啡、響應(yīng)狀態(tài)碼棚蓄、狀態(tài)文本信息。
HTTP頭部集合 -- 在響應(yīng)報(bào)文里亦稱為響應(yīng)首部字段稍算,總體可分組為通用首部字段糊探、響應(yīng)首部字段河闰、實(shí)體首部字段淤击。
空行
消息主體 -- 在響應(yīng)報(bào)文里亦稱為響應(yīng)報(bào)文主體污抬。

HTTP 狀態(tài)碼總結(jié)

狀態(tài)碼通常以3位數(shù)字和原因短語組成绳军,主要分為以下5大類

100-199 信息響應(yīng)
200-299 成功響應(yīng)
300-399 重定向消息
400-499 客戶端錯(cuò)誤響應(yīng)
500-599 服務(wù)端錯(cuò)誤響應(yīng)

常用狀態(tài)碼

200 OK:請(qǐng)求成功门驾。
206 Partial Content:表示客戶端進(jìn)行了范圍請(qǐng)求,而服務(wù)器成功執(zhí)行了這部分的GET請(qǐng)求楣责。
301 Moved Permanently:永久重定向秆麸。請(qǐng)求資源的URL已永久更改沮趣。在響應(yīng)中給出了新的URL。
302 Found:臨時(shí)重定向驻龟。所請(qǐng)求資源的URI已暫時(shí)更改翁狐。未來可能會(huì)對(duì)URI進(jìn)行進(jìn)一步的改變凌蔬。
304 Not Modified:這是用于緩存的目的龟梦。它告訴客戶端響應(yīng)還沒有被修改计贰,因此客戶端可以繼續(xù)使用相同的緩存版本的響應(yīng)。
307 Temporary Redirect:臨時(shí)重定向荞怒。與302區(qū)別在于用戶代理不能更改使用的HTTP方法褐桌。
400 Bad Request:認(rèn)為請(qǐng)求存在語法錯(cuò)誤象迎,需修改請(qǐng)求砾淌。
401 Unauthorized:要求客戶端必須對(duì)自身進(jìn)行身份驗(yàn)證才能獲得請(qǐng)求的響應(yīng)汪厨。
403 Forbidden:客戶端沒有訪問內(nèi)容的權(quán)限。
404 Not Found:服務(wù)器找不到請(qǐng)求的資源织中。
500 Internal Server Error:表明服務(wù)器端在執(zhí)行請(qǐng)求時(shí)發(fā)生了錯(cuò)誤狭吼。
502 Bad Gateway:表示作為網(wǎng)關(guān)或代理角色的服務(wù)器,從上游服務(wù)器中接收到的響應(yīng)是無效的窿春。
503 Service Unavailable:表明服務(wù)器暫時(shí)處于超負(fù)載或正在進(jìn)行停機(jī)維護(hù)旧乞,現(xiàn)在無法處理請(qǐng)求尺栖。
504 Gateway Timeout:網(wǎng)關(guān)或者代理的服務(wù)器無法在規(guī)定的時(shí)間內(nèi)獲得想要的響應(yīng)烦租。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末叉橱,一起剝皮案震驚了整個(gè)濱河市窃祝,隨后出現(xiàn)的幾起案子粪小,更是在濱河造成了極大的恐慌探膊,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,888評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異猾担,居然都是意外死亡绑嘹,警方通過查閱死者的電腦和手機(jī)橘茉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,677評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門擅腰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人歼争,你說我怎么就攤上這事沐绒。” “怎么了蚂踊?”我有些...
    開封第一講書人閱讀 168,386評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)吭净。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么坯辩? 我笑而不...
    開封第一講書人閱讀 59,726評(píng)論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮崩侠,結(jié)果婚禮上濒翻,老公的妹妹穿的比我還像新娘。我一直安慰自己啦膜,他們只是感情好有送,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,729評(píng)論 6 397
  • 文/花漫 我一把揭開白布僧家。 她就那樣靜靜地躺著雀摘,像睡著了一般。 火紅的嫁衣襯著肌膚如雪八拱。 梳的紋絲不亂的頭發(fā)上阵赠,一...
    開封第一講書人閱讀 52,337評(píng)論 1 310
  • 那天,我揣著相機(jī)與錄音肌稻,去河邊找鬼清蚀。 笑死,一個(gè)胖子當(dāng)著我的面吹牛爹谭,可吹牛的內(nèi)容都是我干的枷邪。 我是一名探鬼主播,決...
    沈念sama閱讀 40,902評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼诺凡,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼东揣!你這毒婦竟也來了践惑?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,807評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤嘶卧,失蹤者是張志新(化名)和其女友劉穎尔觉,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體芥吟,經(jīng)...
    沈念sama閱讀 46,349評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡侦铜,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,439評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了钟鸵。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片钉稍。...
    茶點(diǎn)故事閱讀 40,567評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖携添,靈堂內(nèi)的尸體忽然破棺而出嫁盲,到底是詐尸還是另有隱情,我是刑警寧澤烈掠,帶...
    沈念sama閱讀 36,242評(píng)論 5 350
  • 正文 年R本政府宣布羞秤,位于F島的核電站,受9級(jí)特大地震影響左敌,放射性物質(zhì)發(fā)生泄漏瘾蛋。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,933評(píng)論 3 334
  • 文/蒙蒙 一矫限、第九天 我趴在偏房一處隱蔽的房頂上張望哺哼。 院中可真熱鬧,春花似錦叼风、人聲如沸取董。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,420評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽茵汰。三九已至,卻和暖如春孽鸡,著一層夾襖步出監(jiān)牢的瞬間蹂午,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,531評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工彬碱, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留豆胸,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,995評(píng)論 3 377
  • 正文 我出身青樓巷疼,卻偏偏與公主長(zhǎng)得像晚胡,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,585評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • 1搬泥、詳細(xì)敘述ansible的工作原理 以上是兩張ansible工作原理圖桑寨,兩張圖基本都是在架構(gòu)圖的基本上進(jìn)行的拓展...
    Villa_7ca5閱讀 643評(píng)論 0 0
  • 1伏尼、主從復(fù)制及主主復(fù)制的實(shí)現(xiàn) 1-1 主從復(fù)制 1-1-1 主節(jié)點(diǎn)配置修改配置文件忿檩,配置二進(jìn)制日志路徑 備份數(shù)據(jù)庫...
    newjourney閱讀 269評(píng)論 0 0
  • 1.ansible劇本角色功能配置說明 角色功能有什么用:1) 讓劇本配置更加規(guī)范2) 可以讓劇本信息簡(jiǎn)化3) 可...
    斗魂_2e5d閱讀 158評(píng)論 0 0
  • DAY 37 SSH服務(wù)知識(shí)與批量管理項(xiàng)目實(shí)踐 1、基礎(chǔ)端口 873 rsync 22 ssh 25 smtp 郵...
    浩嘫氣灬閱讀 326評(píng)論 0 0
  • day40 playbook 什么是playbook? 把所有操作按照ansible編程語...
    WhatGui_c607閱讀 265評(píng)論 0 1