(二)ansible 使用
2024-09-08 08:59:08
一,ansible 命令格式
#ansible <pattern> -m <module_name> -a <arguments> #单个服务器
ansible 39.108.231.212 -m ping #多个服务器,使用":"连接
ansible 192.168.1.190:192.168.1.191 -m ping #test 组
ansible test -m ping #所有服务器
ansible all -m ping
二,ansible 常用模块
command、copy、fetch、file、ping、shell、service、setup、synchronize、yum
Asible 模块查询
ansible-doc -l #可以直接查看内置模块
ansible-doc -s file #file为模块名
1、命令执行模块
#重启主机 -f 线程数
ansible all –a "hostname" –f #shell模块
ansible all -m shell -a "hostname" #底层ssh 模块
ansible all -m raw -a "hostname"
2、文件操作
#下发文件,且授权以及备份
ansible all –m copy –a "src=/etc/hosts dest=/tmp/hosts owner=root group=root backup=yes" #备份的时候,只有在文件发生了变化的时候,那么会在远程主机上进行备份,而不是在本机上进行备份源文件,备份的是远程主机上被修改的文件 #上传文件(将把192.168.1.126服务器上的/root/test.sh文件上传到ansible 服务器,在/root目录下面保存为:192.168.1.126/root/test.sh)
ansible 192.168.1.126 -m fetch -a "src=/root/test.sh dest=/root" #创建目录
ansible all -m file -a "path=/tmp/hidir state=directory owner=opadmin mode=777" #创建空文件
ansible all -m file -a "path=/tmp/hifile state=touch owner=opadmin mode=777" #创建软连接(/tmp/test2.txt 是目标服务器上的源文件)
ansible all -m file -a "path=/tmp/mytest.txt src=/tmp/test2.txt state=link" #删除符号链接
ansible all -m file -a "path=/tmp/mytest.txt state=absent" #为文件赋予权限
ansible all -m file -a "dest=a.txt mode=600 owner=opadmin group=opadmin"
3,get_url模块
#下载url 文件,保存至tmp目录下
ansible all -m get_url -a "url=http://download.redis.io/releases/redis-4.0.2.tar.gz dest=/tmp/"
4,git模块
#通过yum 模块安装git
ansible webserver -m yum -a "name=git state=latest" #克隆仓库,保存至/tmp/fastdfs
ansible webserver -m git -a"repo=https://github.com/happyfish100/fastdfs.git dest=/tmp/fastdfs"
5,cron 模块
#每隔五分钟所有机器都去172..18.0.1上面同步一次时间
ansible all -m cron -a "name='timesync' job='/usr/sbin/ntpdate 172.18.0.1 &> /dev/null' minute='*/5'"
ansible all -m cron -a "name='check dirs' hour='5,2' job='ls -alh > /dev/null'" #删除crontab
ansible all -m cron -a "name='timesync' job='/usr/sbin/ntpdate 172.18.0.1 &> /dev/null' minute='*/5' state=absent" #注释crontab
ansible all -m cron -a "name='timesync' job='/usr/sbin/ntpdate 172.18.0.1 &> /dev/null' minute='*/5' state=present disabled=true"
6,service模块
#启动服务
ansible all -m service -a "name=nginx enabled=true state=started" #重启服务
ansible all –m service –a "name=nginx state=restarted" #停止服务
ansible all –m service –a "name=nginx state=stopped"
7,yum模块
#安装
ansible all –m yum –a "name=httpd state=installed" #安装指定版本的包
ansible all –m yum –a "name=httpd-2.6 state=installed" #安装最新版本的包
ansible all –m yum –a "name=httpd state=latest" #卸载安装包
ansible all –m yum –a "name=httpd state=removed"
8,用户管理(user)
# 增加用户
ansible all –m user –a "name=tom password=123456" # 删除用户
ansible all –m user –a "name=tom state=absent"
9、设备信息检查
ansible all –m setup
10,script 脚本执行模块
ansible all –m script –a "/root/demo/test.sh"
11,mount 远程主机分区挂载
ansible all -m mount -a "name=/mnt/data src=/dev/sd0 fstype=ext4 opts=ro state=present"
最新文章
- ASP.NET Core 中文文档 第三章 原理(4)路由
- C#基础,C#基础知识点,基础知识点迅速巩固
- C# Winform中如何让PictureBox的背景透明
- Windows系统bug
- [leetcode] Contains Duplicate II
- Centos 如何安装Django环境
- emWin(ucGUI)在PC机上模拟的按键响应多次解决办法 worldsing
- zookeeper集群安装配置
- IEEE802是一个局域网标准系列
- git rm
- canvas、image src、data url、blob file conversion
- javascript每日一练(五)——BOM
- Servlet过滤器——仿盗链过滤器
- Hangfire Highlighter Tutorial
- MongoDB分片(Sharding)技术
- 关于Nginx设置端口号,在Asp.net 获取不到的,解决办法
- Vue-切割json数组字符串并循环输出
- 垃圾wps弹出,现在连关闭按钮都不给了
- Redis缓存系统-Java-Jedis操作Redis,基本操作以及 实现对象保存
- 1082 线段树练习 3 &;&; 树状数组区间修改区间查询
热门文章
- PAT 部分A+B
- MAC抓包工具Charles安装及破解
- 面试三轮我倒在了一道sql题上——sql性能优化
- NumPy之计算两个矩阵的成对平方欧氏距离
- 详解 Flink DataStream中min(),minBy(),max(),max()之间的区别
- 2020/06/06 JavaScript高级程序设计 面向对象的程序设计
- [转载]java内存工具VisualVM的简单使用以及与Idea集成
- Flutter 中由 BuildContext 引发的血案
- 【Navicat】Navicat for MongoDBv15.0.6破解版(附破解教程)
- CISCN 2019-ikun