当前位置:网站首页>Ansible自动化的运用
Ansible自动化的运用
2022-06-26 15:14:00 【N64-HanYeWei】
1、ansible-playbook实现MySQL的二进制部署
编写给予ssh key认证的脚本han_ssh.sh
#!/bin/bash
#
#*********************************************
#Author: Cry 3 tears 4 me
#QQ: 2512129751
#Date: 2022-06-17
#FileName: han_ssh.sh
#Mail: [email protected]
#Description: The best script
#Copyright (C): 2022 All rights reserved
#*********************************************
PASS=123456
#设置网段最后的地址,4-255之间,越小扫描越快
END=254
IP=`ip a s eth0 | awk -F'[ /]+' 'NR==3{print $3}'`
NET=${IP%.*}.
rm -f /root/.ssh/id_rsa
[ -e ./SCANIP.log ] && rm -f SCANIP.log
for((i=3;i<="$END";i++));do
ping -c 1 -w 1 ${NET}$i &> /dev/null && echo "${NET}$i" >> SCANIP.log &
done
wait
ssh-keygen -P "" -f /root/.ssh/id_rsa
rpm -q sshpass || yum -y install sshpass
sshpass -p $PASS ssh-copy-id -o StrictHostKeyChecking=no $IP
AliveIP=(`cat SCANIP.log`)
for n in ${AliveIP[*]};do
sshpass -p $PASS scp -o StrictHostKeyChecking=no -r /root/.ssh [email protected]${n}:
done
#把.ssh/known_hosts拷贝到所有主机,使它们第一次互相访问时不需要输入回车
for n in ${AliveIP[*]};do
scp /root/.ssh/known_hosts ${n}:.ssh/
done
认证完成后被ansible控制的主机是能够互通的:
(1)实验准备:
mkdir roles/mysql/{tasks,vars,files} -pv
(2)配置数据库的my.cnf文件
[[email protected] files]# cat 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
(3)准备变参数在vars文件夹下准备:
[[email protected] vars]# cat main.yml
mysql_version: 8.0.19
mysql_file: mysql-{
{mysql_version}}-linux-glibc2.12-x86_64.tar.xz
mysql_root_password: 123456
(4)准备好mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz安装包放在files文件下:
(5)编写yml模块,用role来实现ansible自动化安装数据库:
[[email protected] tasks]# cat config.yml
- name: config my.cnf
copy: src=/data/ansible/roles/mysql/files/my.cnf dest=/etc/my.cnf
[[email protected] tasks]# cat data.yml
- name: data dir
shell: /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
tags: data
[[email protected] tasks]# cat group.yml
- name: create mysql group
group: name=mysql gid=306
[[email protected] tasks]# cat install.yml
- name: install packages
yum:
name:
- libaio
- numactl-libs
[[email protected] tasks]# cat linkfile.yml
- name: create linkfile /usr/local/mysql
file: src=/usr/local/mysql-{
{ mysql_version }}-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link
[[email protected] tasks]# cat path.yml
- name: PATH variable
copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
[[email protected] tasks]# cat script.yml
- name: service script
shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[[email protected] tasks]# cat secure.yml
- name: change password
shell: /usr/local/mysql/bin/mysqladmin -uroot password {
{mysql_root_password}}
[[email protected] tasks]# cat service.yml
- name: enable service
shell: chkconfig --add mysqld;/etc/init.d/mysqld start
tags: service
[[email protected] tasks]# cat unarchive.yml
- name: copy tar to remote host and file mode
unarchive: src={
{mysql_file}} dest=/usr/local/ owner=root group=root
[[email protected] tasks]# cat user.yml
- name: create mysql user
user: name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes create_home=no home=/data/mysql
[[email protected] tasks]# cat main.yml
- include: install.yml
- include: group.yml
- include: user.yml
- include: unarchive.yml
- include: linkfile.yml
- include: data.yml
- include: config.yml
- include: script.yml
- include: path.yml
- include: service.yml
- include: secure.yml
(6)最后编写运行脚本role_mysql.yml
[[email protected] ansible]# cat role_mysql.yml
---
- hosts: 10.0.0.6
remote_user: root
gather_facts: no
roles:
- mysql
(7)验证实验是否成功
在10.0.0.6装上mysql的客户端程序进行验证:
yum -y install mysql-8.0.21-1.module_el8.2.0+493+63b41e36.x86_64
mysql -uroot -p123456
2、Ansible playbook实现apache批量部署,并对不同主机提供以各自IP地址为内容的index.html、
roles:多个角色的集合目录,可以将多个的role,分别放至roles目录下的独立子目录中;
roles角色:
简单来讲,roles就是通过分别将变量、文件、任务、模板及处理器放置于单独的目录中,并可以便捷地include它们的-种机制。角色一般用于基于主机构建服务的场景中,但也可以是用于构建守护进程等场景中。
运维复杂的场景:建议使用roles,代码复用度高
roles:多个角色的集合目录,可以将多个的role,分别放至roles目录下的独立子目录中roles各录、roles/project/:项目名称,有以下子目录files/:存放由copy或script模块等调用的文件;
templates/: template模块查找所需要模板文件的目录;
tasks/: 定义task,role的基本元素,至少应该包含一个名为main.yml的文件;其它的文件需要在此文件中通过include进行包含
handlers/:至少应该包含一个名为main.yml的文件;此目录下的其它的文件需要在此文件中通include进行包含;
vars/:定义变量,至少应该包含一个名为main.yml的文件;
此目录下的其它的变量文件需要在此文件中通过include进行包含;
metal:定义当前角色的特殊设定及其依赖关系,至少应该包含一个名为main.yml的文件,其它文件需在此文件中通过include进行包含;
defaultl:设定默认变量时使用此目录中的main.yml文件,比vars的优先级低;
前期准备工作:
准备roles角色的文件夹
mkdir roles/nginx/{tasks,templates,handlers,files} -pv
mkdir roles/httpd/{tasks,templates,handlers,files} -pv
编写安装nginx的playbook模板:
[[email protected] tasks]# cat yum.yml
- name: Install nginx
yum: name=nginx state=present
[[email protected] tasks]# cat config.yml.yml
cat: config.yml.yml: No such file or directory
[[email protected] tasks]# cat config.yml
- name: Modify web page
copy: content='{
{ ansible_eth0.ipv4.address }}\n' dest=/usr/share/nginx/html/index.html
[[email protected] tasks]# cat service.yml
- name: start service
service: name=nginx state=started enabled=yes
[[email protected] tasks]# cat main.yml
- include: yum.yml
- include: config.yml
- include: service.yml
[[email protected] nginx]# cd ..
[[email protected] nginx]# cd handlers/
[[email protected] handlers]# cat main.yml
- name: restart
service: nmae=nginx state=restarted
编写nginx.yml
[[email protected] ansible]# cat nginx.yml
---
- hosts: websrvs
remote_user: root
roles:
- nginx
修改要控制主机的文件
[[email protected] ansible]# cat /etc/ansible/hosts
检查脚本是否有问题
运行脚本
检查实验现象:
httpd的搭建与nginx很类似,但最好被控制的主机都有安装net-tools的软件,才能自动创建文件:
[[email protected] tasks]# cat config.yml
- name: Modify web page
copy: content='{
{ ansible_eth0.ipv4.address }}\n' dest=/var/www/html/index.html
[[email protected] tasks]# cat yum.yml
- name: Install httpd
yum: name=httpd state=present
[[email protected] tasks]# cat yum1.yml
- name: Install net-tools
yum: name=net-tools state=present
[[email protected] tasks]# cat service.yml
- name: start service
service: name=httpd state=started enabled=yes
[[email protected] tasks]# cat main.yml
- include: yum.yml
- include: yum1.yml
- include: config.yml
- include: service.yml
配置httpd.yml
[[email protected] ansible]# cat httpd.yml
---
# httpd role
- hosts: websrvs
remote_user: root
roles:
- httpd
3、http的报文结构和状态码总结
HTTP报文分为请求报文和响应报文,这两类报文都是由三个部分组成:
(1)开始行:区分请求报文和响应报文
(2)首部行:说明浏览器、服务器报文主题的一些信息
(3)实体主体
请求报文的开始行包括:方法(请求方法如get、post、put、trace等)、URL、协议版本
响应报文的开始行包括:版本、状态码、短语(用于简单解释状态码)
响应报文中的状态码分为五大类,共33种:
1XX表示通知信息的,如请求收到了或正在进行处理
2XX表示成功,如接受或知道了
3XX表示重定向,如要完成请求还必须采取进一步的行动
4XX表示客户的差错,如请求中有语法错误或不能完成
5XX表示服务器的差错,如服务器失效无法完成请求
常见状态码:
202 Accepted 接受
400 Bad Reque 错误请求
404 Not Found 找不到
边栏推荐
- IDEA本地代理后,无法下载插件
- 在校生学习生涯总结(2022)
- Deployment of kubernetes' controller
- Particle filter PF - 3D CV target tracking with uniform motion (particle filter vs extended Kalman filter)
- 【TcaplusDB知识库】TcaplusDB运维单据介绍
- PHP file upload 00 truncation
- 数据库-完整性约束
- Unity C# 网络学习(九)——WWWFrom
- 2022北京石景山区专精特新中小企业申报流程,补贴10-20万
- Restcloud ETL resolves shell script parameterization
猜你喜欢
【ceph】cephfs的锁 笔记
Particle filter PF - 3D CV target tracking with uniform motion (particle filter vs extended Kalman filter)
[tcapulusdb knowledge base] tcapulusdb OMS business personnel permission introduction
AbortController的使用
【小程序实战系列】小程序框架 页面注册 生命周期 介绍
【TcaplusDB知识库】TcaplusDB系统用户组介绍
【TcaplusDB知识库】TcaplusDB单据受理-创建业务介绍
Function: crypto JS encryption and decryption
人力资源导出数据 excel VBA
How to load the contour CAD drawing of the engineering coordinate system obtained by the designer into the new earth
随机推荐
Using restcloud ETL shell component to schedule dataX offline tasks
一键分析硬件/IO/全国网络性能脚本(强推)
Unity C# 网络学习(十)——UnityWebRequest(一)
[tcapulusdb knowledge base] Introduction to tcapulusdb data structure
10分钟了解BIM+GIS融合,常见BIM数据格式及特性
SQLite loads CSV files and performs data analysis
About selenium common. exceptions. Webdriverexception: message: an unknown server side error solution (resolved)
AbortController的使用
设计人员拿到的工程坐标系等高线CAD图如何加载进图新地球
功能:crypto-js加密解密
Unity C# 网络学习(九)——WWWFrom
shell脚本多进程并发写法实例(高阶修炼)
一篇博客彻底掌握:粒子滤波 particle filter (PF) 的理论及实践(matlab版)
Unity C # e-learning (IX) -- wwwfrom
音视频学习(三)——sip协议
Pod of kubernetes
Evaluate:huggingface评价指标模块入门详细介绍
[tcapulusdb knowledge base] tcapulusdb doc acceptance - table creation approval introduction
sqlite加载csv文件,并做数据分析
[tcapulusdb knowledge base] tcapulusdb doc acceptance - transaction execution introduction