设为首页 收藏本站
查看: 1823|回复: 1

[经验分享] ansible安装和常用的命令

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-8-15 10:43:30 | 显示全部楼层 |阅读模式
QQ截图20160815104317.jpg
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
[iyunv@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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-258071-1-1.html 上篇帖子: Ansible自动化运维工具部署及使用 下篇帖子: Centos6.5利用RubyGems的fpm制作zabbix_agent的rpm包,并使用ansible...
累计签到:52 天
连续签到:1 天
发表于 2017-8-14 14:24:31 | 显示全部楼层
不错,学习了

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表