当前位置:网站首页>Automated operation and maintenance tools - ansible, overview, installation, module introduction
Automated operation and maintenance tools - ansible, overview, installation, module introduction
2022-08-02 06:20:00 【Granny who loves watching square dancing】
目录
一、ansible——自动化运维工具
1.1 Ansible 自动运维工具特点
Ansible 与 Saltstack 均是基于 Python 语言开发,Ansible 只需要在一台普通的服务器上运行即可,不需要在客户端服务器上安装客户端.因为 Ansible 是基于 SSH 远程管理,而Linux服务器大都离不开SSH,所以Ansible不需要为配置工作添加额外的支持.
Ansible 安装使用非常简单,而且基于上千个插件和模块实现各种软件、平台、版本的管理,支持虚拟容器多层级的部署.很多读者在使用 Ansible 工具时,认为 Ansible比 Saltstatck 执行效率慢,其实不是软件本身慢,是由于 SSH 服务慢,可以优化 SSH 连接速度及使用 Ansible 加速模块,满足企业上万台服务器的维护和管理.
1.2 Ansible 运维工具原理
Ansible分为控制端和被控端,主要是基于SSH协议去管理客户端,被控端是无需安装Agent插件的,Ansible会读取控制端hosts文件,根据文件中定义IP列表信息,调取本地的各个模块对被控端机器实现批量、并发的配置管理和维护,如果任务比较复杂可以写成PlayBook剧本进行分发管理;
Ansible 自动运维管理工具优点:
- 轻量级,更新时,只需要在操作机上进行一次更新即可;
- 采用 SSH 协议;
- 不需要去客户端安装 agent;
- 批量任务执行可以写成脚本,而且不用分发到远程就可以执行;
- 使用 python 编写的,维护更简单;
- 支持 sudo 普通用户命令;
- 去中心化管理.
二、安装ansible
#192.168.223.11
hostname ansible
su
#192.168.223.10
hostname webserver
su
#192.168.223.9
hostname mysql
su
systemctl stop firewalld
setenforce 0
#安装epel扩展源
yum -y install epel-release
yum -y install ansible
#树型查询工具
yum -y install tree
tree /etc/ansible
vim /etc/ansible/hosts
#配置主机清单
[webserver]
192.168.223.10
[mysql]
192.168.223.9
#生成密钥对
ssh-keygen -t rsa
123123
123123
ssh-copy-id [email protected]
ssh-copy-id [email protected]
#每次查询都需要输入密钥
#可以使用ssh-agent代理
ssh-agent bash
ssh-add
123123
ansible webserver -m command -a 'date'
三、ansible命令模块
3.1 command模块
命令格式:ansible [主机] [-m 模块] [-a args]
#列出所有已安装的模块,按q退出
ansible-doc -l
#所有主机执行data命令,其中all可以换成IP或者分类名称,例:192.168.223.11 / webserver
ansible all -m command -a 'date'
#不加-m模块,则默认使用command模块
ansible all -a 'date'
ansible all -a 'ls /'
3.2 cron模块
两种状态(state):present表示添加(可以省略),absent表示移除
#查看cron模块信息
ansible-doc -s cron
#webserver:分类 -m指定模块 -a输出模块内的指令 分钟:每分钟,工作:输出hello,工作名称:test
ansible webserver -m cron -a 'minute="*/1" job="/usr/bin/echo hello" name="test"'
#查看计划性任务命令
ansible webserver -a 'crontab -l'
#移除计划性任务
ansible webserver -m cron -a 'name="test" state=absent'
3.3 user模块
user模块是请求三条指令,useradd,userdel,usermod
#模块信息
ansible-doc -s user
#创建用户
ansible all -m user -a 'name=zhangsan'
#查看用户账户信息
ansible all -m 'command' -a 'tail -1 /etc/passwd'
#移除指令
ansible all -m user -a 'name="zhangsan" state=absent'
3.4 group模块
group模块请求的是groupadd、groupdel、groupmod模块
#查看模块信息
ansible-doc -s group
#system=yes 创建系统组
ansible mysql -m group -a 'name=zb2b gid=1111 system=yes'
#查看组账户信息
ansible mysql -a 'tail -1 /etc/group'
#创建用户并加入组
ansible mysql -m user -a 'name=zhangsan uid=1234 group=zb2b system=yes'
#查看用户test02的用户id和组id信息
ansible mysql -a 'id zhangsan'
3.5 copy模块
对文件进行有效的复制
ansible-doc -s copy
ansible mysql -m copy -a 'src=/etc/fstab dest=/opt/fstab.back'
ansible mysql -a 'ls /opt'
ansible mysql -a 'cat /opt/fstab.back'
ansible mysql -m copy -a 'content="hello lic" dest=/opt/test.txt'
ansible mysql -a 'cat /opt/test.txt'
3.6 file模块
ansible-doc -s file
ansible mysql -m user -a 'name=mysql system=yes'
ansible mysql -m file -a 'owner=mysql group=mysql mode=600 path=/opt/test.txt'
ansible mysql -a 'ls -l /opt/test.txt'
#创建
#ansible mysql -m file -a 'path=/opt/abc.txt state=touch'
ansible mysql -m file -a 'src=/opt/test.txt path=/opt/test.txt.link state=link'
ansible mysql -a 'ls -l /opt'
#移除文件/opt/test.txt
ansible mysql -m file -a 'path=/opt/test.txt state=absent'
3.7 ping模块
ansible all -m ping
3.8 service模块
ansible-doc -s service
#192.168.223.10执行
yum -y install httpd
ansible webserver -a 'systemctl status httpd'
ansible webserver -m service -a 'enabled=true name=httpd state=started'
systemctl status httpd
systemctl is-enabled httpd
3.9 shell模块
ansible-doc -s shell
ansible mysql -m user -a 'name=zhangsan'
ansible mysql -m shell -a 'echo 123123 | passwd --stdin zhangsan'
3.10 script模块
ansible-doc -s script
vim test.sh
#!/bin/bash
echo 'hello ansible from script' > /opt/script.txt
chmod +x test.sh
ansible all -m script -a 'test.sh'
3.11 yum模块
ansible-doc -s yum
ansible mysql -m yum -a 'name=httpd'
ansible mysql -a 'rpm -q httpd'
ansible mysql -m yum -a 'name=httpd state=absent'
ansible mysql -a 'rpm -q httpd'
3.12 setup模块
ansible-doc -s setup
#获取MySQL组主机的facts信息
ansible mysql -m setup
Ansible facts 是远程系统的信息,主要包含IP地址,操作系统,以太网设备,mac 地址,时间/日期相关数据,硬件信息等信息.
/etc/ansible/hosts
是ansible默认主机清单
如果名称类似的主机,可以使用列表的方式标识各个主机
[webserver]
www[01:50].example.org ansible_ssh_user=root ansible_ssh_pass=123123
表示www01.example.org~www50.example.org的主机,ssh登陆用户为root,密码为123123
边栏推荐
- [Digital IC hand-tear code] Verilog fixed priority arbiter | topic | principle | design | simulation
- Matlab paper illustration drawing template No. 41 - bubble chart (bubblechart)
- 12 reasons for MySQL slow query
- Matlab论文插图绘制模板第41期—气泡图(bubblechart)
- 腾讯大咖分享 | 腾讯Alluxio(DOP)在金融场景的落地与优化实践
- Review: image saturation calculation formula and image signal-to-noise (PSNR) ratio calculation formula
- Redis数据库
- Use the browser's local storage to realize the function of remembering the user name
- golang generics
- el-input 只能输入整数(包括正数、负数、0)或者只能输入整数(包括正数、负数、0)和小数
猜你喜欢
Three methods of importing sql files in MySQL
【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台
MYSQL unique constraint
Packaging and deployment of go projects
非关系型数据库MongoDB的特点及安装
51 MCU Peripherals: Infrared Communication
ApiPost 真香真强大,是时候丢掉 Postman、Swagger 了
MySQL 8.0.29 解压版安装教程(亲测有效)
25K测试老鸟6年经验的面试心得,四种公司、四种问题…
What do interview test engineers usually ask?The test supervisor tells you
随机推荐
Meta公司内部项目-RaptorX:将Presto性能提升10倍
Use the browser's local storage to realize the function of remembering the user name
APP Bluetooth connection test of test technology
Go语学习笔记 - 处理超时问题 - Context使用 从零开始Go语言
21天学习挑战赛安排
c语言:查漏补缺(三)
51单片机外设篇:DS18B20
公司不重视软件测试,新来的阿里P8给我们撰写了测试用例编写规范
Navicat如何连接MySQL
apisix-入门使用篇
Detailed installation and configuration of golang environment
"Digital reconstruction of the system, getting the CEO is the first step"
How much does a test environment cost? Start with cost and efficiency
[Digital IC hand-tear code] Verilog fixed priority arbiter | topic | principle | design | simulation
[PSQL] window function, GROUPING operator
Redis-集群模式(主从复制模式,哨兵模式,集群化模式)
软件测试的需求人才越来越多,为什么大家还是不太愿意走软件测试的道路?
Brush LeetCode topic series - 10. Regular expression match
navicat无法连接mysql超详细处理方法
navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)