1、ansible-playbook实现MySQL的二进制部署
#安装mysql5.6
[root@ansible ~]#ls -l /data/ansible/files/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
-rw-r--r-- 1 root root 403177622 Dec 4 13:05 /data/ansible/files/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
[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
123456
123456
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_facts: no
tasks:
- 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
#判断centos7或者centos8安装mysql5.7
[root@centos8 ansible]# mkdir files
[root@centos8 ansible]# ls files/
mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
[root@centos8 ansible]# vim files/my.cnf
[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
[root@centos8 ansible]# vim vars.yml
---
# variables file
mysql_version: 5.7.33
[root@centos8 ansible]# cat files/set_password.sh
#!/bin/bash
MYSQL_ROOT_PASSWORD=123456
MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}'
/data/mysql/mysql.log`mysqladmin -uroot -p$MYSQL_OLDPASSWORD password $MYSQL_ROOT_PASSWORD&>/dev/null
[root@centos8 ansible]# vim install_mysql5.7.yml
---
# install mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
- hosts: 10.0.0.150
remote_user: root
gather_facts: yes
vars_files:
- vars.yml
tasks:
- name: install packages for centos7
yum: name=libaio,perl-Data-Dumper
when: ansible_facts['distribution_major_version'] == "7"
- name: install packages for centos8
yum: name=libaio,perl-Data-Dumper,ncurses-compat-libs
when: ansible_facts['distribution_major_version'] == "8"
- name: cteate 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-{{mysql_version}}-linuxglibc2.12-x86_64.tar.gz dest=/usr/local/ owner=root group=root
- name: create linkfile /usr/local/mysql
file: src=/usr/local/mysql-{{mysql_version}}-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link
- name: PATH variable
copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
- name: PATH variable entry
shell: . /etc/profile.d/mysql.sh
- name: config my.cnf
copy: src=/data/ansible/files/my.cnf dest=/etc/my.cnf
- name: data dir
shell: chdir=/usr/local/mysql ./bin/mysqld --initialize --user=mysql --datadir=/data/mysql
- 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: set mysql user password
script: /data/ansible/files/set_password.sh
tags: script
[root@centos8 ansible]# tree
.
├── files
│ ├── my.cnf
│ ├── mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
│ └── set_password.sh
├── install_mysql5.7.yml
└── vars.yml
1 directory, 5 files
[root@centos8 ansible]# ansible-playbook install_mysql5.7.yml
2、Ansible playbook实现apache批量部署,并对不同主机提供以各自IP地址为内容的index.html
[root@ansible ~]# vim /etc/ansible/hosts
[webservers]
10.0.0.150 ansible_connection=local
10.0.0.160
#创建角色相关目录
[root@ansible html]# mkdir -pv /data/ansible/roles/httpd/{tasks,handlers,files}
mkdir: created directory '/data/ansible'
mkdir: created directory '/data/ansible/roles'
mkdir: created directory '/data/ansible/roles/httpd'
mkdir: created directory '/data/ansible/roles/httpd/tasks'
mkdir: created directory '/data/ansible/roles/httpd/handlers'
mkdir: created directory '/data/ansible/roles/httpd/files'
#创建角色相关文件
[root@ansible html]# cd /data/ansible/roles/httpd/
#main.yml是task的入口文件
[root@ansible httpd]# vim tasks/main.yml
- include: group.yml
- include: user.yml
- include: install.yml
- include: config.yml
- include: index.yml
- include: service.yml
[root@ansible httpd]# vim tasks/group.yml
- name: create apache group
group: name=apache system=yes gid=80
[root@ansible httpd]# vim tasks/user.yml
- name: create apache user
user: name=apache system=yes shell=/sbin/nologin home=/var/www/ uid=80 group=apache
[root@ansible httpd]# vim tasks/install.yml
- name: install httpd package
yum: name=httpd
[root@ansible httpd]# vim tasks/config.yml
- name: config file
copy: src=httpd.conf dest=/etc/httpd/conf/ backup=yes
notify: restart
[root@ansible httpd]# vim tasks/index.yml
- name: index.html
copy: content='{{ ansible_default_ipv4.address }}\n' dest=/var/www/html/index.html
[root@ansible httpd]# vim tasks/service.yml
- name: start httpd service
service: name=httpd state=started enabled=yes
[root@ansible httpd]# vim handlers/main.yml
- name: restart
service: name=httpd state=restarted
#file目录下准备httpd.conf配置文件
[root@ansible httpd]# ls files/httpd.conf
files/httpd.conf
[root@ansible httpd]# tree /data/ansible/roles/httpd/
/data/ansible/roles/httpd/
├── files
│ └── httpd.conf
├── handlers
│ └── main.yml
└── tasks
├── config.yml
├── group.yml
├── index.yml
├── install.yml
├── main.yml
├── service.yml
└── user.yml
3 directories, 9 files
#在playbook中调用角色
[root@ansible ~]# vim /data/ansible/role_httpd.yml
- hosts: webservers
remote_user: root
roles:
- httpd
#运行playbook
[root@ansible ~]# ansible-playbook /data/ansible/role_httpd.yml
#验证:
[root@ansible ~]# curl 10.0.0.150
10.0.0.150
[root@ansible ~]# curl 10.0.0.160
10.0.0.160
3、http的报文结构和状态码总结
HTTP报文分为请求报文和响应报文,这两类报文都是由三个部分组成:
(1)开始行:区分请求报文和响应报文
(2)首部行:说明浏览器、服务器报文主题的一些信息
(3)实体主体
请求报文的开始行包括:方法(请求方法如get、post、put、trace等)、URL、协议版本
响应报文的开始行包括:版本、状态码、短语(用于简单解释状态码)
响应报文中的状态码分为五大类:
1XX表示通知信息的,如请求收到了或正在进行处理
2XX表示成功,如接受或知道了
3XX表示重定向,如要完成请求还必须采取进一步的行动
4XX表示客户的差错,如请求中有语法错误或不能完成
5XX表示服务器的差错,如服务器失效无法完成请求
常见状态码:
200 OK:表示从客户端发送给服务器的请求被正常处理并返回
301 Moved Permanently:永久性重定向,表示请求的资源被分配了新的URL,之后应使用更改的URL
302 Found:临时性重定向,表示请求的资源被分配了新的URL,希望本次访问使用新的URL
400 Bad Request:表示请求报文中存在语法错误
401 Unauthorized:未经许可,需要通过HTTP认证
403 Forbidden:服务器拒绝该次访问(访问权限出现问题)
404 Not Found:表示服务器上无法找到请求的资源,除此之外,也可以在服务器拒绝请求但不想给拒绝原因时使用
500 Inter Server Error:表示服务器在执行请求时发生了错误,也有可能是web应用存在的bug或某些临时的错误时
503 Server Unavailable:表示服务器暂时处于超负载或正在进行停机维护,无法处理请求