一、安装:

$ sudo apt-get install ansible

二、配置:

a、基本配置

$ cd /etc/ansible/

$ sudo cp hosts hosts_back 备份一个副本并定义自己的管理组

$ sudo ssh-keygen -t rsa -P ''

b、查看密匙及检查权限

$ ll .ssh/

$ ll -d .ssh/

172959352.jpg

c、复制密匙到目标主机

$ ssh-copy-id -i .ssh/id_rsa.pub uname@ip 默认端口为22

$ ssh-copy-id -i .ssh/id_rsa.pub "-p 24 uname@ip" 指定端口

@后面跟的是远程机器的ip

@前面的用户名 表示你是把自己创建的公钥传递到对端电脑的那个用户家目录下面

三、测试使用:

$ ansible groupname -a 'date' 数据某组上所有目标机器的当前时间

报错:

ip| FAILED => FAILED: ssh uname@ip:22 : Private key file is encrypted

To connect as a different user, use -u <username>.

分析问题:原来系统默认会以当期系统的当前用户来登录远程主机,而当前系统的用户根本在目标机器上不一定存在,安提示再测试;

$ ansible -u name host -m 'ping' 用username指定某一目标主机

ip | success >> {

"changed": false,

"ping": "pong"

}

经过测试能成功,于是想到修改hosts里的组的目标主机

[test]

ip ansible_ssh_port=port1 ansible_ssh_host=ip1 ansible_ssh_user=username1

ip ansible_ssh_port=port2 ansible_ssh_host=ip2 ansible_ssh_user=username2

ip ansible_ssh_port=port3 ansible_ssh_host=ip3 ansible_ssh_user=username3

$ ansible test -m 'ping' 测试组里的目标主机

测试通过,一切正常~~~