安装
安装环境:阿里云(centos7)
apt-get update
apt install ansible
配置主机清单
ansible controller 在对主机进行操作时,仅可配置主机清单中的主机。
此外,ansible的前提是ssh免密互通,可参考:《SSH互通免密配置》
- 找到路径 /etc/ansible
cd /etc/ansible
ls
vim /etc/ansible/hosts
- 修改hosts文件,设置组名:webservers
[webservers]
192.168.1.31
- 查看主机清单,按组名:webservers
ansible webservers --list-host
返回:
hosts (1):
121.37.138.197
连通测试
ansible webservers -m ping -o
返回:121.37.138.197 | SUCCESS => {"changed": false, "ping": "pong"}
制作测试脚本
cd /tem
mkdir wyc
cd wyc
echo "echo 'hi'" > sh1.sh
远程拷贝文件
ansible webservers -m copy -a "src=/tmp/wyc/sh1.sh dest=/home/wyc/hi.sh mode=075"
返回:
121.37.138.197 | SUCCESS => {
"changed": true,
"checksum": "a7b8a9fd73c2f4ecd097a412e6bcf3be9ded0c7a",
"dest": "/home/wyc/hi.sh",
"gid": 0,
"group": "root",
"md5sum": "05c83f7505aa72ddb993a0b5a2070790",
"mode": "0075",
"owner": "root",
"size": 10,
"src": "/root/.ansible/tmp/ansible-tmp-1618560120.32-115585452304433/source",
"state": "file",
"uid": 0
}
远程执行脚本
ansible webservers -m shell -a "/home/wyc/hi.sh"
返回:
121.37.138.197 | SUCCESS | rc=0 >>
hi
1112

被折叠的 条评论
为什么被折叠?



