ansible执行脚本:ansible liuq  -m script -a "test.sh"

配置ssh密钥登录

expect脚本

#!/usr/bin/expect -f set ip [lindex $argv 0]set user [lindex $argv 1]set passwd [lindex $argv 2] set timeout 10 spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $user@$ip expect {    "*password:" {send "$passwd\r"}    "*yes/no*" {send "yes\r"}} expect off

shell调用expect脚本

#!/bin/bashwhile read id;do    ip=`echo $id | awk '{print $1}'`    user=`echo $id | awk '{print $2}'`    passwd=`echo $id | awk '{print $3}'`    ./copy-key.exp $ip $user $passwddone 

host.list格式

192.168.60.152 root root

192.168.60.155 root root

192.168.60.156 root root

192.168.60.110 root root

192.168.60.111 root root

192.168.60.100 root root

虚拟机同步时间

#!/bin/bashcmd="ntpdate -u time.windows.com"cmd2="hwclock -w"ansible annhe -m command -a "$cmd"ansible annhe -m command -a "$cmd2"ansible annhe -m command -a "date"

copy文件

用ansible copy设置软件源的脚本并执行

ansible hadoop -m copy -a "src=/srv/salt/repo.sh dest=/tmp/repo.sh owner=root group=root mode=0755"

ansible hadoop -m command -a "ls -l /tmp/repo.sh"

ansible hadoop -m command -a "/tmp/repo.sh"

repo.sh用于设置软件源

#!/bin/bash yumdir="/etc/yum.repos.d"epel="$yumdir/epel.repo"backup="$yumdir/backup" [ ! -d $backup ] && mkdir $backupmv -f $yumdir/*.repo $backup rpm -qa |grep epel-release && rpm -e epel-releaserpm -Uvh http://mirrors.yun-idc.com/epel/6/x86_64/epel-release-6-8.noarch.rpm#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearchsed -i 's#download.fedoraproject.org/pub#mirrors.yun-idc.com#g' $epelsed -i -E 's/^mirrorlist(.*)/#mirrorlist\1/g' $epelsed -i -E 's/#baseurl(.*)/baseurl\1/g' $epelcurl -s http://mirrors.163.com/.help/CentOS6-Base-163.repo -o $yumdir/Base-163.repoyum clean allyum makecache