ansible的使用:

http://perin.blog.51cto.com/10410663/1726277

http://www.mamicode.com/info-detail-1428476.html


一、安装ansible

  1、 python版本需要2.6以上,不过通过centos7都会默认安装上python2.7.5,查看方法:python -V

  2、 添加yum 源

    a、 vim /etc/yum.repos.d/ansible.repo

    b、 添加如下内容:

[epel]
name = all source for ansible
baseurl = https://mirrors.aliyun.com/epel/7/x86_64/
enabled = 1
gpgcheck = 0

[ansible]
name = all source for ansible
baseurl = http://mirrors.aliyun.com/centos/7.3.1611/os/x86_64/
enabled = 1
gpgcheck = 0

  3、 yum clean all && yum makecache

  4、 安装ansible:yum install ansible -y

二、配置ansible

   

[root@jeff ansible]# cat /etc/hosts
172.28.8.61 app_xor1
172.28.8.62 app_xor2
172.28.8.80 gateway


  1、 使用ansible前需实现ansible服务器对另外两台机的ssh无密码访问,操作如下:

    a、 输入如下命令,按回车:

ssh-keygen -t rsa -P ''

    b、 命令:ssh-copy-id root@172.28.8.61 ,然后输入连接密码即可实现无密码访问。

  2、 添加ansible客户机组,命令:vim /etc/ansible/hosts,在最后面添加如下内容:

[xor]
app_xor1
app_xor2
[docker]
gateway ansible_ssh_user=root ansible_ssh_pass='itvitv'
172.28.8.81 ansible_ssh_user=root ansible_ssh_pass='itvitv'

三、使用ansible

  1、 在另外两台机上执行ping命令:ansible xor -m ping

  2、 复制本机文件到组group1:ansible xor -m copy -a “src=/etc/hosts dest=/etc/”

  3、 帮group1安装软件:ansible xor -m shell -a “yum install wget -y”


四、FAQ

1、安装完成后连接客户端服务器报错:

[root@jeff ansible]# ansible docker -m ping
172.28.8.81 | FAILED! => {
    "failed": true, 
    "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host."
}


解决:ansible 服务器上使用ssh 登陆下/etc/ansible/hosts 里面配置的服务器(ssh root@172.28.8.81)。然后再次使用ansible 去管理就不会报上面的错误了!但这样大批量登陆就麻烦来。因为默认ansible是使用key验证的,如果使用密码登陆的服务器,使用ansible的话,要不修改ansible.cfg配置文件的ask_pass = True给取消注释,要不就在运行命令时候加上-k,这个意思是-k, --ask-pass ask for SSH password。再修改:host_key_checking= False即可