在网上找了很多资料,有很多是得配置epel才能安装,但是配置了半天也没配置好,最后只好用源码包装,整理后发帖记录。

安装ansible
[root@node1 ~]# yum install PyYAML.x86_64python-paramiko.noarch python-jinja2.x86_64 python-devel -y 
[root@node1 ~]# wgethttps://pypi.python.org/packages/source/a/ansible/ansible-1.7.2.tar.gz 
[root@node1 ~]#wgethttps://pypi.python.org/packages/source/s/
setuptools/setuptools-7.0.tar.gz 
[root@node1 ~]# tar zfxvsetuptools-7.0.tar.gz 
[root@node1 ~]# cd setuptools-7.0
[root@node1 setuptools-7.0]# python setup.pyinstall 
[root@node1 setuptools-7.0]# cd .. 
[root@node1 ~]# tar fzvxansible-1.7.2.tar.gz 
[root@node1 ~]# cd ansible-1.7.2
[root@node1 ansible-1.7.2]# python setup.pybuild 
[root@node1 ansible-1.7.2]# python setup.pyinstall 

注意如果提示:error: Setup script exited with error: command 'gcc'failed with exit status 1报错,请安装:

yum install python-devel

 

[root@node1 ansible-1.7.2]# mkdir /etc/ansible 

[root@node1 ansible-1.7.2]# cp examples/ansible.cfg /etc/ansible/ 
[root@node1 ansible-1.7.2]# cp examples/hosts/etc/ansible/
4
)配置ansible(可以默认)
[root@node1 ansible-1.7.2]# vi/etc/ansible/ansible.cfg 
hostfile = /etc/ansible/hosts 
library = /usr/share/ansible 
remote_tmp = $HOME/.ansible/tmp 
pattern = * 
forks = 5
poll_interval = 15
sudo_user = ansible 
#ask_sudo_pass = True 
#ask_pass = True 
transport = smart 
remote_port = 22
module_lang = C 

添加主机(可以把系统默认的主机删掉或注释掉)
[root@node1 ansible-1.7.2]# vi/etc/ansible/hosts 
#server 
[localhost] 
127.0.0.1
#client 
[client] 
192.168.253.129
192.168.253.130
192.168.253.131

ssh互信(达到的效果是ssh主机不输入密码即可登录)

 

首先登入一台linux服务器,此台做为母机(即登入其他linux系统用这台做为入口);执行一行命令生成key文件:ssh-keygen -t rsa

 wKiom1gztljAEUM0AAAFnezpyNs153.png-wh_50                           spacer.gif

然后一直按回车

[ansible@node1 ~]$ cd .ssh/ 
[ansible@node1 .ssh]$ cat *.pub >authorized_keys 
[ansible@node1 .ssh]$ chmod -R 700 . 

在母机上,进入/root/.ssh目录,找到id_rsa.pub该文件,这个文件就是刚才执行ssh-keygen所生成的公钥key文件。

wKiom1gztnmBlSf5AAGRBJB7i4o335.png-wh_50

scp命令,将母机产生的key拷一份到远程的linux服务器上,并命名成authorized_keysscp ~/.ssh/id_rsa.pub  root@192.168.1.113:/root/.ssh/authorized_keys。这一步的操作需要手动输入密码。

[root@localhost.ssh]# scp ~/.ssh/id_rsa.pub  root@192.168.95.4:/root/.ssh/authorized_keys

 

测试是否成功

[root@localhost.ssh]# ansible all -m ping

192.168.95.9 |success >> {

    "changed": false,

    "ping": "pong"

}

 

127.0.0.1 |success >> {

    "changed": false,

    "ping": "pong"

}

 

192.168.95.4 |success >> {

    "changed": false,

    "ping": "pong"

}

 

192.168.95.14 |success >> {

    "changed": false,

    "ping": "pong"

}

 

[root@localhost.ssh]#  ansible all -m command -a"date"

192.168.95.4 |success | rc=0 >>

Wed Jun 2917:27:46 CST 2016

 

192.168.95.14 |success | rc=0 >>

Thu Jun 3000:44:28 CST 2016

 

192.168.95.9 |success | rc=0 >>

Wed Jun 2910:25:32 CST 2016

 

127.0.0.1 |success | rc=0 >>

Thu Jun 3000:41:49 CST 2016