ansible常用命令20条
1.创建目录
1 | ansible update -m file -a 'path=/tmp/dd state=directory mode=0755' |
2.修改文件属性
1 | ansible update -m file -a 'path=/tmp/dd state=touch mode="u=rw,g=r,o=r"' |
3.创建软连接
1 | ansible update -m file -a 'src=/tmp/dd dest=/tmp/ddl owner=root group=root state=link force=yes' |
4.修改拥有者
1 | ansible update -m file -a 'path=/tmp/dd owner=tomcat group=root mode=0644' |
5.创建用户
1 | ansible update -m user -a 'name=johd' -become |
6.删除用户
1 | ansible update -m user -a 'name=johd state=absent' -become |
7.创建秘钥
1 | ansible update -m user -a 'name=johd generate_ssh_key=yes ssh_key_bits=2048' -become |
8.创建组
1 | ansible test -m group -a 'name=ansible state=present' -become |
9.删除组
1 | ansible update -m group -a 'name=pig state=absent' -become |
10.下载文件的远程服务器
1 | ansible update -m get_url -a 'url=http://www.baidu.com dest=/tmp/baidu.txt mode=0777' |
11.增加一个cron任务
1 | ansible update -m cron -a "backup=yes name='test cron' minute=*/2 hour=* job='ls /tmp >/dev/null'" |
12.安装服务
1 | ansible update -m yum -a 'name=httpd state=prestent' -become |
13.停止httpd
1 | ansible update -m service -a 'name=httpd state=stopped' |
14.重启httpd
1 | ansible update -m service -a 'name=httpd state=restarted' |
15.修改sysctl文件
1 | ansible update -m sysctl -a 'name=vm.overcommit_memory value=1' -become |
16.挂载/dev/vda盘到/mnt/data目录
1 | ansible update -m mount -a 'name=/mnt/data src=/dev/vda fstype=ext4 state=mounted' |
17.远程查看内存
1 | ansible Client -m command -a "free -m " |
18.远程执行脚本
1 | ansible Client -m script -a "/home/test.sh" |
19.实现主控端向目标主机拷贝文件
1 | ansible Client -m copy -a "src=/root/rcp dest=/backup/" |
20.yum安装
1 | ansible Client -m yum -a "name=htop state=latest" |
21.推送目录,把/data/ts_code/ops/下的目录文件推送到目标/data/ops目录下
1 | ansible code -m copy -a 'src=/data/ts_code/ops/ dest=/data/ops owner=root mode=755' |
22.推送目录,推送/tmp/test下面的目录文件到目标/tmp/下面
1 | ansible all -m synchronize -a 'src=/tmp/test dest=/tmp/ compress=yes' |
分类:
自动化运维
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)