第13周作業
利用ansible的playbook實現MySQL5.7的二進制部署
利用角色部署
主配置文件:
vim mysql-role.yml
---
- hosts: 10.0.0.12
remote_user: root
gather_facts: no
roles:
- mysql
roles結構
[root@centos7 mysql]# tree
.
├── tasks
│ ├── 10cpfile.yml
│ ├── 11service.yml
│ ├── 12passwd.yml
│ ├── 1install.yml
│ ├── 2unarchive.yml
│ ├── 3linkfile.yml
│ ├── 4Changeper.yml
│ ├── 5groupadd.yml
│ ├── 6useradd.yml
│ ├── 7Path.yml
│ ├── 8prefile.yml
│ ├── 9chushihua.yml
│ └── main.yml
├── templates
│ └── my.cnf.j2
└── vars
└── main.yml
3 directories, 15 files
tasks文件
[root@centos7 tasks]# cat 1install.yml
- name: install op
yum: name={{ item }}
with_items:
- libaio
- numactl-libs
[root@centos7 tasks]# cat 2unarchive.yml
- name: tar xf mysql
unarchive: src=/data/ansible/bao/{{ MYSQL }} dest=/usr/local owner=root group=root
[root@centos7 tasks]# cat 3linkfile.yml
- name: create link
file: src=/usr/local/mysql-{{ MYSQL_version }}-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link
[root@centos7 tasks]# cat 4Changeper.yml
- name: chown
file:
path: /usr/local/mysql/
owner: root
group: root
[root@centos7 tasks]# cat 5groupadd.yml
- name: group mysql
group: name=mysql gid=306
[root@centos7 tasks]# cat 6useradd.yml
- name: useradd mysql
user: name=mysql group=mysql uid=306 shell=/sbin/nologin system=yes create_home=no home=/data/mysql
[root@centos7 tasks]# cat 7Path.yml
- name: PATH file
copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
[root@centos7 tasks]# cat 8prefile.yml
- name: peizhiwenjian
template: src=my.cnf.j2 dest=/etc/my.cnf
[root@centos7 tasks]# cat 9chushihua.yml
- name: chushihua
shell: /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
[root@centos7 tasks]# cat 10cpfile.yml
- name: cpfile
shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@centos7 tasks]# cat 11service.yml
- name: service start
shell: chkconfig --add mysqld;chkconfig mysqld on;service mysqld start
[root@centos7 tasks]# cat 12passwd.yml
- name: passwd
shell: /usr/local/mysql/bin/mysqladmin -uroot password {{ MYSQL_ROOT_PASSWORD }}
[root@centos7 tasks]# cat main.yml
- include: 1install.yml
- include: 2unarchive.yml
- include: 3linkfile.yml
- include: 4Changeper.yml
- include: 5groupadd.yml
- include: 6useradd.yml
- include: 7Path.yml
- include: 8prefile.yml
- include: 9chushihua.yml
- include: 10cpfile.yml
- include: 11service.yml
- include: 12passwd.yml
變量文件
[root@centos7 mysql]# cd vars/
[root@centos7 vars]# tree
.
└── main.yml
0 directories, 1 file
[root@centos7 vars]# cat main.yml
SRC_DIR: /data/ansible/bao
MYSQL_version: 8.0.28
houzhui: xz
MYSQL: mysql-{{MYSQL_version}}-linux-glibc2.12-x86_64.tar.{{houzhui}}
MYSQL_ROOT_PASSWORD: 123456
模板文件
[root@centos7 mysql]# cd templates/
[root@centos7 templates]# tree
.
└── my.cnf.j2
0 directories, 1 file
[root@centos7 templates]# cat my.cnf.j2
[mysqld]
server-id=1
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
用ansible運行主配置文件即可
ansible-playbook mysql-role.yml
Ansible playbook實現apache批量部署,并對不同主機提供以各自IP地址為內容的index.html
主配置文件
[root@centos7 ansible]# cat httpd-role.yml
---
- hosts: 10.0.0.20
remote_user: root
roles:
- httpd
roles結構
[root@centos7 roles]# tree httpd/
httpd/
├── httpd-2.4.53.tar.bz2
├── tasks
│ ├── build.yml
│ ├── downaprutil.yml
│ ├── downapr.yml
│ ├── downhttpd.yml
│ ├── group.yml
│ ├── install.yml
│ ├── main.yml
│ ├── preaprutil.yml
│ ├── preapr.yml
│ ├── prefile.yml
│ ├── sethttpdgroup.yml
│ ├── sethttpduser.yml
│ ├── setvar.yml
│ ├── start.yml
│ └── user.yml
├── templates
│ └── httpd.service.j2
└── vars
└── main.yml
3 directories, 18 files
tasks
[root@centos7 tasks]# cat main.yml
- include: install.yml
- include: downhttpd.yml
- include: downapr.yml
- include: downaprutil.yml
- include: preapr.yml
- include: preaprutil.yml
- include: build.yml
- include: group.yml
- include: user.yml
- include: sethttpduser.yml
- include: sethttpdgroup.yml
- include: setvar.yml
- include: prefile.yml
- include: start.yml
[root@centos7 tasks]# cat install.yml
- name: install packages
yum: name=gcc,make,pcre-devel,openssl-devel,expat-devel,bzip2 state=installed
[root@centos7 tasks]# cat downhttpd.yml
- name: download httpd file
unarchive: src="{{ httpd_url }}//{{ httpd_version }}.tar.bz2" dest={{ download_dir }} remote_src=yes
[root@centos7 tasks]# cat downapr.yml
- name: download apr
unarchive: src="{{ apr_url }}/{{ apr_version }}.tar.bz2" dest={{ download_dir }} owner=root remote_src=yes
[root@centos7 tasks]# cat downaprutil.yml
- name: download aprutil
unarchive: src="{{ apr_url }}/{{ apr_util_version }}.tar.bz2" dest={{ download_dir }} owner=root remote_src=yes
[root@centos7 tasks]# cat preapr.yml
- name: prepare aprdir
shell: chdir={{ download_dir }} mv {{ apr_version }} {{ download_dir }}/{{ httpd_version }}/srclib/apr
[root@centos7 tasks]# cat preaprutil.yml
- name: prepare aprutil
shell: chdir={{ download_dir }} mv {{ apr_util_version }} {{ download_dir }}/{{ httpd_version }}/srclib/apr-util
[root@centos7 tasks]# cat build.yml
- name: build httpd
shell: chdir={{ download_dir }}/{{ httpd_version }} ./configure --prefix={{ install_dir }} --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 && make -j {{ ansible_processor_vcpus }} && make install
[root@centos7 tasks]# cat group.yml
- name: create group
group: name=apache gid=80 system=yes
[root@centos7 tasks]# cat user.yml
- name: useradd
user: name=apache group=apache uid=80 shell=/sbin/nologin system=yes create_home=no home={{ install_dir }}/conf/httpd
[root@centos7 tasks]# cat sethttpdgroup.yml
- name: set httpd group
lineinfile: path={{ install_dir }}/conf/httpd.conf regexp='^Group' line='Group apache'
[root@centos7 tasks]# cat setvar.yml
- name: set var path
shell: echo PATH=={{ install_dir }}/bin:$PATH >> /etc/profile.d/httpd.sh
[root@centos7 tasks]# cat prefile.yml
- name: prepare service file
template: src=httpd.service.j2 dest=/usr/lib/systemd/system/httpd.service
[root@centos7 tasks]# cat start.yml
- name: start httpd
service: name=httpd state=started enabled=yes
vars
[root@centos7 httpd]# cat vars/main.yml
download_dir: /usr/local/src
install_dir: /apps/httpd
httpd_version: httpd-2.4.53
apr_version: apr-1.7.0
apr_util_version: apr-util-1.6.1
httpd_url: https://mirrors.bit.edu.cn/apache/httpd/
apr_url: https://mirrors.bit.edu.cn/apache/apr/
templates
[root@centos7 httpd]# cat templates/httpd.service.j2
[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
#EnvironmentFile=/etc/sysconfig/httpd
ExecStart={{ install_dir }}/bin/apachectl start
#ExecStart={{ install_dir }}/bin/httpd $OPTIONS -k start
ExecReload={{ install_dir }}/bin/apachectl graceful
#ExecReload={{ install_dir }}/bin/httpd $OPTIONS -k graceful
ExecStop={{ install_dir }}/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
http的報文結構和狀態碼總結
HTTP的報文分為請求報文和響應報文
HTTP請求報文:由三個部分組成,即開始行、首部行和實體主體
在請求報文中,開始行就是請求行。
request報文格式:
<method> <request-URL> <version>
<headers>
<entity-body>
HTTP響應報文:開始行是狀態行
狀態行包括三項內容,即HTTP的版本,狀態碼以及解釋狀態碼的簡單短語
<version> <status> <reason-phrase>
<headers>
<entity-body>
Method方法:
請求方法,標明客戶端希望服務器對資源執行的動作,包括以下:
GET: 從服務器獲取一個資源
HEAD: 只從服務器獲取文檔的響應首部
POST: 向服務器輸入數據,通常會再由網關程序繼續處理
PUT: 將請求的主體部分存儲在服務器中,如上傳文件
DELETE: 請求刪除服務器上指定的文檔
TRACE: 追蹤請求到達服務器中間經過的代理服務器
OPTIONS:請求服務器返回對指定資源支持使用的請求方法
CONNECT:建立一個到由目標資源標識的服務器的隧道
PATCH:用于對資源應用部分修改
首部的分類:
1、通用首部:請求報文和響應報文兩方都會使用的首部
2、請求首部:從客戶端向服務器端發送請求報文時使用的首部。補充了請求的附加內容、客戶端信息、請求內容相關優先級等信息
3、響應首部:從服務器端向客戶端返回響應報文時使用的首部。補充了響應的附加內容,也會要求客戶端附加額外的內容信息
4、實體首部:針對請求報文和響應報文的實體部分使用的首部。補充了資源內容更新時間等與實體有關的的信息
5、協商首部:某資源有多種表示方法時使用
常見的HTTP狀態碼
200 (成功) 服務器已成功處理了請求。 通常,這表示服務器提供了請求的網頁。
301 (永久移動) 請求的網頁已永久移動到新位置。 服務器返回此響應(對 GET 或 HEAD 請求的響應)時,會自動將請求者轉到新位置。
302 (臨時移動) 服務器目前從不同位置的網頁響應請求,但請求者應繼續使用原有位置來進行以后的請求。
304 (未修改) 自從上次請求后,請求的網頁未修改過。 服務器返回此響應時,不會返回網頁內容。
400 (錯誤請求) 服務器不理解請求的語法。
401 (未授權) 請求要求身份驗證。 對于需要登錄的網頁,服務器可能返回此響應。
403 (禁止) 服務器拒絕請求。
404 (未找到) 服務器找不到請求的網頁。
500 (服務器內部錯誤) 服務器遇到錯誤,無法完成請求。
502 (錯誤網關) 服務器作為網關或代理,從上游服務器收到無效響應。
503 (服務不可用) 服務器目前無法使用(由于超載或停機維護)。 通常,這只是暫時狀態。
504 (網關超時) 服務器作為網關或代理,但是沒有及時從上游服務器收到請求。
浙公網安備 33010602011771號