Ansible使用格式
ansible
all #all表示所有主机,host文件配置的主机分类名,例如webservers
-m #接模块,例如copy
-a #接参数
-f #子进程数
ansible-doc -s 模块名 获取指定模块帮助信息说明 ansible-doc -l 查看模块列表
Ansible常用模块
copy模块
从本地copy文件分发到目录主机路径
ansible all -m copy -a 'src=/root/test/scan.sh dest=/root'
src—>源文件路径 dest—>目标路径
fetch模块
从远程主机拉取文件到本地
ansible all -m fetch -a 'src=/root/one-key-nginx-install.sh dest=/root'
script模块
把本地的脚本传到远程主机执行
ansible all -m script -a 'root/weixin.sh test'
command模块
在远程主机上执行命令,属于裸执行,无法使用管道符号进行过滤,所以建议使用shell模块。。
ansible all -m command -a "ip add"
shell模块
shell模块也是在远程主机执行命令,但是可以使用管道符号进行过滤
ansible all -m shell -a "ip add|grep eth0"
file模块
path目标路径
state
- directory—>目录
- touch—>文件
- link—>软链接
- hard—>硬链接
ansible all -m file -a 'path=/root/wangyutao.txt state=touch'
service模块
name—>服务名 state—>reloaded,restarted,started,stopped enabled—>true 开机自启动
ansible all -m service -a 'name=sshd state=restarted enabled=true'
yum模块
安装卸载软件包的模块 name—>安装包名 state installed—>安装,removed—>删除
ansible all -m yum -a 'name=vim state=installed'
cron模块
分(minute)时(hour)日(day)月(month)周(week) nane->名字 job->指定计划的任务中需要实际执行的命令或者脚本 state->当计划任务有名称时,我们可以根据名称修改或删除对应的任务,当删除计划任务时,需要将 state 的值设置为 absent
ansible all -m cron -a "name=test minute=* job='bash /root/test.sh'"
#远程设置一个test定时任务,每分钟执行test脚本
ansible all -m cron -a "name=test state=absent"
#删除test定时任务