Chinaunix首页 | 论坛 | 博客
  • 博客访问: 385382
  • 博文数量: 112
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 800
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-29 13:41
文章分类

全部博文(112)

文章存档

2020年(1)

2018年(10)

2017年(27)

2016年(18)

2015年(31)

2014年(25)

分类: 系统运维

2017-08-10 14:34:29

新增被控机:192.168.3.71、192.168.3.72、192.168.3.251
如果被控机python版本低于2.6,需升级到2.6版本或以上。
/etc/ansible/hosts:内容如下:

点击(此处)折叠或打开

  1. [wsf1]
  2. 192.168.3.71
  3. [wsf2]
  4. 192.168.3.72
  5. [se]
  6. 192.168.3.251

点击(此处)折叠或打开

  1. #生成秘钥,免交互。
  2. root@ubuntu:/etc/ansible#ssh-keygen -t rsa -"" -/home/ubuntu/.ssh/id_rsa
  3. 需从ansible主控机分发公钥到被控机:
  4. root@ubuntu:/etc/ansible#ssh-copy-id -i /root/.ssh/id_rsa.pub -p 8020 test@192.168.3.71
  5. root@ubuntu:/etc/ansible#ssh-copy-id -/root/.ssh/id_rsa.pub -p 8020 test@192.168.3.72
  6. root@ubuntu:/etc/ansible#ssh-copy-id -/root/.ssh/id_rsa.pub -p 8020 test@192.168.3.251

点击(此处)折叠或打开

  1. 被控机使用非root用户
  2. root@ubuntu:/etc/ansible# ansible se,wsf1,wsf2 -m ping -u test
  3. 192.168.3.72 | SUCCESS => {
  4.     "changed": false,
  5.     "ping": "pong"
  6. }
  7. 192.168.3.71 | SUCCESS => {
  8.     "changed": false,
  9.     "ping": "pong"
  10. }
  11. 192.168.3.251 | SUCCESS => {
  12.     "changed": false,
  13.     "ping": "pong"
  14. }

点击(此处)折叠或打开

  1. 被控机使用root用户
  2. root@ubuntu:/etc/ansible# ansible se,wsf1,wsf2 -m ping -u root

  3. root@ubuntu:/etc/ansible# ansible se,wsf1,wsf2 -m ping
  4. 192.168.3.72 | SUCCESS => {
  5.     "changed": false,
  6.     "ping": "pong"
  7. }
  8. 192.168.3.71 | SUCCESS => {
  9.     "changed": false,
  10.     "ping": "pong"
  11. }
  12. 192.168.3.251 | SUCCESS => {
  13.     "changed": false,
  14.     "ping": "pong"
  15. }
分发公钥脚本:

点击(此处)折叠或打开

  1. #!/bin/bash
  2. IP_CONF="/etc/ansible/hosts"
  3. IP_INFO="$(cat ${IP_CONF}|grep "^[0-9]")"
  4. SSH_USER=`uname -n`
  5. SSH_PUB="/home/${SSH_USER}/.ssh/id_rsa.pub"
  6. #SSH_PWD='123456'

  7. if [ ! -f ${SSH_PUB} ];then
  8.   echo "NO FOUND ${SSH_PUB}."
  9.   exit 1
  10. fi
  11. if [ ! -f ${IP_CONF} ];then
  12.   echo "NO FOUND ${IP_CONF}."
  13.   exit 1
  14. fi
  15. for i in ${IP_INFO}
  16. do
  17.   echo $i
  18.   sleep 1
  19. #-o StrictHostKeyChecking=no,第一次连接服务器时,自动接受新的公钥。或者修改/etc/ssh/ssh_config文件中StrictHostKeyChecking no
  20.   sshpass -p "${SSH_PWD}" ssh-copy-id -i /home/${SSH_USER}/.ssh/id_rsa.pub -p 8020 kylinyunwei@${i} -o StrictHostKeyChecking=no
  21. done







阅读(1830) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~