Ansible 批量执行命令

写法如下:

[root@localhost ~]$ ansible edukuo -m command -a 'tar zxvf /usr/local/logstash.tar.gz -C /usr/local/'       # 对指定的主机远程执行命令,-m 指定使用哪个模块,-a 指定执行哪些命令
[root@localhost ~]$ ansible edukuo -m command -a 'rm -rf /root/logstash'   # 对指定的主机组远程执行命令,需要先配置/etc/ansible/hosts
[root@localhost ~]$ ansible 172.30.20.27 -m shell -a 'ps -ef | grep java'   # 如果要执行的命令带有管道,必须使用 shell 模块

ansible edukuo -m copy -a "src=/usr/src/logstash-start.sh dest=/root"   # 从ansible cp文件到其它主机
ansible edukuo -m copy -a "src=/usr/src/logstash-start.sh dest=/root owner=root group=root mode=0755"  #附加权限用户属组

栗子:
在这里插入图片描述
上述edukuo为主机组,手动编辑:
vim /etc/ansible/hosts
在这里插入图片描述
栗子:
批量下载安装脚本
ansible platform -m shell -a ‘wget 172.30.20.18/install.sh’
因缺少wget
ansible platform -m shell -a ‘yum install -y wget’
因dns导致无法访问
Error
ansible platform -m shell -a ‘sed -i ‘s/nameserver 172.30.70.11/#&/’ /etc/resolv.conf’
因不能套双层’ ’ ’ '后改为
ansible platform -m shell -a ‘sed -i “s/nameserver 172.30.70.11/#&/” /etc/resolv.conf’
再执行
ansible platform -m shell -a ‘yum install -y wget’
安装
ansible platform -m shell -a ‘sh /root/install.sh’

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值