準備兩台機器
192.168.220.140 a.com ##server端
192.168.220.145 b.com ##client端
1. 安裝 ##a.com上安裝即可
yum install -y epel-release
yum install -y ansible
2. 配置
(1) ssh密鑰配置
首先生成密鑰對
ssh-keygen -t rsa ##直接回車即可,不用設定密鑰密碼,這樣會在root家目錄下生成.ssh目錄,這裡面也會生成兩個檔案 id_rsa 和 id_rsa.pub
cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys ##把公鑰(id_rsa.pub)内容放到對方機器的/root/.ssh/authorized_keys裡面,包括本機
chmod 600 /root/.ssh/authorized_keys ##配置好client端 authorized_keys檔案的權限
(2) ansible 配置
vi /etc/ansible/hosts ##增加
[testhost] ##testhost為主機組名字,自定義的。 下面兩個ip為組内的機器ip。
127.0.0.1
b.com
3. 遠端執行指令
ansible testhost -m command -a 'w' ##testhost 為主機組名,也可以針對某一台機器來執行指令。
錯誤: "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
解決: yum install -y libselinux-python
4. 拷貝檔案或者目錄
ansible testhost -m copy -a "src=/etc/passwd dest=/tmp/ owner=root group=root mode=0644" ##源目錄會放到目标目錄下面去。
5. 遠端執行shell腳本
vim /tmp/test.sh ##增加
#!/bin/bash
echo `11111111` > /tmp/1.txt