ansible简单用用

加个hosts

vim /etc/ansible/hosts
[elk]
10.183.93.131
10.183.93.132

执行个ping命令检测下

ansible -i /etc/ansible/hosts  elk -m ping

自己单独的环境
开启全局sudo哈哈

 vim /etc/sudoers
dingyunlong     ALL = (ALL) NOPASSWD: ALL

在dingyunlong账号下,新建ansible_hosts,执行sudo命令ok

/home/dingyunlong/ansible_hosts

[elk]
10.183.93.131
10.183.93.132
sudo ansible -i ansible_hosts  elk -m ping

查看各who命令

sudo ansible -i ansible_hosts elk -m command -a 'w'

本地的shell脚本远程执行

sudo ansible -i ansible_hosts elk -m  script -a 'echo1.sh'

脚本内容

#!/bin/bash
echo $HOSTNAME

这里写图片描述

shell或者raw模块执行awk等操作,command模块不行,注意$前面的\号,很重要

sudo ansible -i ansible_hosts elk -m  raw  -a "df -h | awk '{print \$1}'"

练习一下,去掉第一行

 sudo ansible -i ansible_hosts elk -m  shell  -a "df -h | awk '{print \$1}' | tail -n +2 "

去掉尾部两行

sudo ansible -i ansible_hosts elk -m  shell  -a "df -h | awk '{print \$1}' | head -n -2"

往远程主机拷贝文件

sudo ansible -i ansible_hosts elk -m copy -a "src=/home/dingyunlong/echo1.sh dest=/tmp/echo1_copy.sh mode=755 owner=root group=root"

权限,内容都没问题
这里写图片描述
这里写图片描述

从远程主机拉取文件

sudo ansible -i ansible_hosts elk -m fetch -a "src=/tmp/echo_test.sh dest=/home/dingyunlong/  mode=755 owner=root group=root"

拉取的会有主机名和文件夹

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值