码迷,mamicode.com
首页 > 其他好文 > 详细

ansible安装和常用的命令

时间:2016-08-14 08:00:03      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:ansible基础

技术分享

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

yum  -y install  expect

 1.1cat auto_deploy.sh 

#!/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 

#!/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 

172.16.1.10
172.16.1.27

2.安装ansible

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

2.1.基本的设置

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查看常用的模块

[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常用的命令总结

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" #创建软连接


本文出自 “砖家博客” 博客,请务必保留此出处http://wsxxsl.blog.51cto.com/9085838/1837689

ansible安装和常用的命令

标签:ansible基础

原文地址:http://wsxxsl.blog.51cto.com/9085838/1837689

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!