博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ansible安装和常用的命令
阅读量:7297 次
发布时间:2019-06-30

本文共 2948 字,大约阅读时间需要 9 分钟。

1.建立ssh无秘钥认证的关系

1
yum  -y 
install  
expect

 1.1cat auto_deploy.sh 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/sh
/etc/init
.d
/functions
#1.product key pair
ssh
-keygen -t dsa -P 
'' 
-f ~/.
ssh
/id_dsa 
>
/dev/null 
2>&1
 
if 
[ $? -
eq 
0 ];
then
  
action 
"create dsa success" 
/bin/true
 
else
  
action 
"create dsa failed" 
/bin/false
  
exit 
1
fi
#2.dis pub key
for 
ip 
in  
`
cat 
ip.txt`
do
 
expect expect_fenfagongyao.exp  ~/.
ssh
/id_dsa
.pub $ip >
/dev/null 
2>&1
  
if 
[ $? -
eq 
0 ];
then
     
action 
"$ip" 
/bin/true
  
else
     
action 
"$ip" 
/bin/false
  
fi
done

1.2cat expect_fenfagongyao.exp 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/expect
   
if 
{ $argc != 2 }  {
       
send_user  
"usege: expect expect_fenfagongyao.exp file host\n"
       
exit
      
}
##define var
set 
file 
[lindex $argv 0]
set 
host [lindex $argv 1]
set 
password 
"123456"
spawn 
ssh
-copy-
id 
-i $
file 
"-p 22 root@$host"
    
expect  {
            
"yes/no"  
{send 
"yes\r"
;exp_continue}
             
"*password"  
{send 
"$password\r"
}
    
}
expect eof   
exit 
-onexit {
  
send_user 
"say good bye to you!\n"
}

1.3 cat ip.txt 

1
2
172.16.1.10
172.16.1.27

2.安装ansible

说明:采用yum安装,源码包安装特别麻烦

2.1.基本的设置

1
2
3
4
5
6
7
8
9
yum -y 
install 
ansible
cd  
/etc/ansible
ll
ansible.cfg   
#ansible的系统配置文件
 
hosts      
#客户端的主机的配置文件
/etc/ansible/hosts
文件的最后添加:
[webservers]
172.16.1.10
172.16.1.27

2.2查看常用的模块

1
2
3
4
5
6
7
8
9
10
[root@centos67 ansible]
# ansible-doc  -l
a10_server                         Manage A10 Networks AX
/SoftAX/Thunder/vThunder 
devices                          
a10_service_group                  Manage A10 Networks devices' service 
groups                                     
a10_virtual_server                 Manage A10 Networks devices' virtual servers                                    
acl                                Sets and retrieves 
file 
ACL information.                                        
add_host                           add a host (and alternatively a group) to the ansible-playbook 
in
-memory inventor...
airbrake_deployment                Notify airbrake about app deployments                                           
alternatives                       Manages alternative programs 
for 
common commands                                
apache2_module                     enables
/disables 
a module of the Apache2 webserver                              
apk                                Manages apk packages

2.2ansible常用的命令总结

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ansible  webservers  
#webservers模块
ansible  all  
#所有的模块
ansible webservers -m 
ping   
#查看主机存活状态
 
ansible webservers -m shell  -a 
"bash  /tmp/test.sh"  
#远程执行shell脚本,执行客户端的/tmp/test.sh 脚本
ansible webservers  -m script -a 
'/root/run.sh'  
#执行脚本/root/run.sh  为本地的脚本
ansible webservers  -m   
command  
-a  
'uptime'  
#远程执行命令
ansible webservers  -m 
command  
-a  
'yum  -y install  httpd'  
#远程安装apache
ansible   webservers  -m service  -a  
'name=httpd state=started'
name:软件的名字
stated:有started  stoped  restarted  reloaded
 
ansible webservers  -m copy -a  
'dest=/tmp src=/root/run.sh' 
#本机的/root/run.sh 拷贝到客户机/tmp下
ansible all  -m 
cron  
-a  
'name="cron job" minute=*/5 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate time.nist.gov"'    
#定时任务
ansible webservers -m 
file 
-a 
"dest=/tmp/test.sh mode=777 owner=sanlang group=sanlang" 
#修改客户端文件权限
 
ansible -i 
/etc/ansible/hosts  
webservers   -m setup  
#查看客户端主机的详细信息
ansible webservers -m 
file  
-a  
"src=/etc/fstab dest=/tmp/fstab state=link" 
#创建软连接
本文转自 小小三郎1 51CTO博客,原文链接:http://blog.51cto.com/wsxxsl/1837689,如需转载请自行联系原作者
你可能感兴趣的文章
手机端网页使用html5地理定位获取位置失败的解决办法
查看>>
负载均衡知识
查看>>
Android中单位
查看>>
Java进阶篇设计模式之三 ----- 建造者模式和原型模式
查看>>
OpenCV 3.0中IplImage* 转cv::Mat
查看>>
软件工程第1次个人作业
查看>>
【python3的学习之路十三】错误和调试
查看>>
CentOS 6.7安装HBase 0.98.23
查看>>
python 基础复习 05
查看>>
高交会聚集战略性新兴产业
查看>>
在Solaris下安装中文语言包
查看>>
JS正则表达式验证数字非常全
查看>>
JVM内存管理的机制
查看>>
too many open files
查看>>
一些可能用的上的资源
查看>>
[LeetCode] Read N Characters Given Read4
查看>>
inner join on, left join on, right join on讲解(转载)
查看>>
java for循环
查看>>
关于web XSS注入问题
查看>>
jQuery EasyUI---validatebox 校验规则扩展
查看>>