要求:linux CentOS release 6.8 (Final)
两台机器:192.168.1.2 192.168.1.3
1.只在192.168.1.2 安装
yum install epel-release
yum install ansible
2.在192.168.1.2上生成密钥对
ssh-keygen -t rsa 不设置密码 回车
cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
cat /root/.ssh/id_rsa.pub 复制内容到192.168.1.3
vi /root/.ssh/authorized_keys
把id_rsa.pub内容复制到这里
两台
chmod 600 /root/.ssh/authorized_keys
关闭selinux
3.在192.168.1.2 vim /etc/ansible/hosts
[ahost]
127.0.0.1
192.168.1.3
4.执行远程命令
ansible testhost -m command -a ‘w‘ ->指定工作组
-m 模块名
-a 命令
ansible testhost -m shell -a ‘w‘ ->可以指定管道
eg:
ansible testhost -m shell -a ‘ps -aux | grep ssh‘
ansible 127.0.0.1 -m command -a ‘hostname‘ ->指定单个机器
5.为了防止报错,建议安装
yum install -y libselinux-python
6.文件拷贝
ansible testhost -m copy -a "src=/etc/ansible dest=/tmp/ansibletest owner=root group=root mode=0644"
ansible testhost -m copy -a "src=/etc/passwd dest=/tmp/passwd"