加个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"
拉取的会有主机名和文件夹