ssh-keygen -t rsa
格式: ssh-copy-id -i ~/.ssh/id_rsa.pub username@[ip,hostname]html
ssh-copy-id -i ~/.ssh/id_rsa.pub username@192.168.100.2
编辑/etc/ansible/hosts,没有则建立些文件。tomcat
格式:【主机名】 【主机地址】 【主机密码】 默认是root用户来进行的ssh
[tomcat-servers]
1 ansible_ssh_user="tomcat" ansible_ssh_host=192.168.100.1 ansible_ssh_pass="test"
2 ansible_ssh_user="tomcat" ansible_ssh_host=192.168.100.2 ansible_ssh_pass="test"
新版的ansible(2.4) hosts有更新, 用如下方式:post
[tomcat-servers]
192.168.100.1 ansible_user=tomcat ansible_ssh_pass="test" 192.168.100.2 ansible_user=tomcat ansible_ssh_pass="test"
机器多的状况下,使用ssh-copy-id方法有些费时,使用ansible-playbook推送ymal,这里使用到了authoried_keys模块,能够参考 http://docs.ansible.com/authorized_key_module.html测试
将如下文件命名为:push.ssh.ymal this
# Using alternate directory locations:
- hosts: tomcat-servers
user: tomcat
tasks:
- name: ssh-copy
authorized_key: user=tomcat key="{{ lookup('file', '/home/tomcat/.ssh/id_rsa.pub') }}"
tags:
- sshkey
ansible-playbook push.ssh.ymal
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.
修改host_key_checking(默认是check的):spa
vi /home/xiangdong/ansible/ansible.cfg # uncomment this to disable SSH key host checking host_key_checking = False
#查看各机器时间 ansible all -a date #ansible all -m command -a date # 做用同上 #ping ansible all -m ping
输出结果:code
$ ansible all -m ping 192.168.100.1 | SUCCESS => { "changed": false, "ping": "pong" }
参考:server
1.http://mageedu.blog.51cto.com/4265610/1412028htm
2.http://www.justwinit.cn/post/8973/