并发与shell
# bruce用户身份,-m指定模块名称,默认模块名command,all所有目标主机,也可以指定组名或者主机名
ansible all -m ping -u bruce
# bruce用户身份,sudoing到root用户
ansible all -m ping -u bruce --sudo
# bruce用户身份,sudoing到batman用户,-U(--sudo-user)指定sudo用户,-K(--ask-sudo-pass)指定sudo用户密码
ansible all -m ping -u bruce --sudo -U batman
# -f指定并发数, command模块不支持变量
ansible atlanta -m command -a '/sbin/reboot' -f 10
# -k指定用户密码,-a模块参数,shell模块支持变量,命令参数必须使用双引号
ansible all -u root -k -m shell -a "/bin/echo $i"
文件传输
# copy模块可以拷贝文件
ansible atlanta -m copy -a 'src=/etc/hosts dest=/tmp/hosts'
# file模块可以改变文件属性,建立删除目录文件
ansible webservers -m file -a 'dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan'
ansible webservers -m file -a 'dest=/path/to/c mode=755 owner=mdehaan group=mdehaan state=directory'
ansible webservers -m file -a 'dest=/path/to/c state=absent'
包管理(yum模块)
ansible webservers -m yum -a 'name=acme state=present' # 确保acme包被安装,但是不升级
ansible webservers -m yum -a 'name=acme-1.5 state=present' # 指定acme包版本号
ansible webservers -m yum -a 'name=acme state=latest' # acme包升级到最新版本
ansible webservers -m yum -a 'name=acme state=absent' # 确保acme包不被安装
用户&组(user&group模块)
ansible all -m user -a 'name=foo password=<crypted password here>' # 建立用户
ansible all -m user -a 'name=foo state=absent' # 删除用户或者组
版本控制工具(git模块)
ansible webservers -m git -a 'repo=git://foo.example.org/repo.git dest=/srv/myapp version=HEAD'
服务管理(service模块)
ansible webservers -m service -a 'name=httpd state=started' #启动
ansible webservers -m service -a 'name=httpd state=restarted' #重启
ansible webservers -m service -a 'name=httpd state=stopped' #停止
有时限的后台操作
# 后台运行long_running_operation命令,时间为1小时(3600s),系统会给执行同一任务主机一个jid
ansible all -B 3600 -a '/usr/bin/long_running_operation --do-stuff'
# 轮询任务的执行状态
ansible all -m async_status -a 'jid=123456789'
# 后台运行时间1800秒,每60分钟轮询检查一次
ansible all -B 1800 -P 60 -a '/usr/bin/long_running_operation --do-stuff'
系统facts(setup模块)
# 获取目标主机信息
ansible all -m setup
标签:
ansible
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类