设为首页 收藏本站
查看: 922|回复: 0

[经验分享] ansible远程执行命令

[复制链接]
累计签到:2 天
连续签到:1 天
发表于 2016-4-22 09:48:11 | 显示全部楼层 |阅读模式
ansible testhost -m command -a 'w'
  这样就可以批量执行命令了,这里的testhost为主机组名,-m 后边是模块的名字,-a后面是命令,当然我们也可以直接写一个ip,针对某一台机器来执行命令
    ansible 127.0.0.1 -m command -a 'hostname'
     错误:"msg":"Aborting,target user selinux but python bindings(libselinux-python)arenot installed!"
     解决:yum install -y libselinux-python
      还有一个模块就是shell同样也可以实现

      ansible testhost -m shell -a 'w'



一. Ansible更改配置文件

     vi /etc/ansible/hosts //增加
     
[testhosts]
127.0.0.1
172.7.15.111


说明:testhost为主机组名字,自定义的。下面两个ip为组内的机器ip。


1
2
3
4
[iyunv@web9 ~]# vim /etc/ansible/hosts
[testhost]
127.0.0.1
172.7.15.111



针对这个组执行操作(比如远程执行一个w命令)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[iyunv@web9 ~]# ansible testhosts -m command -a 'w'
172.7.15.111 | SUCCESS | rc=0 >>
00:32:39 up  1:20,  2 users,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    172-7-15-100.lig 23:13    8:09   0.09s  0.09s -bash
root     pts/1    web9.gz.com      00:32    0.00s  0.12s  0.00s /bin/sh -c LANG

127.0.0.1 | SUCCESS | rc=0 >>
00:32:38 up  1:21,  4 users,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    172-7-15-100.lig 23:13    1.00s  0.21s  0.01s ssh 127.0.0.1
root     pts/1    localhost        00:28    1.00s  0.05s  0.01s ssh 127.0.0.1
root     pts/2    localhost        00:30    1.00s  0.77s  0.54s /usr/bin/python
root     pts/5    localhost        00:32    0.00s  0.08s  0.00s /bin/sh -c LANG




远程执行一个命令ls

1
2
3
4
5
6
7
8
9
10
[iyunv@web9 ~]# ansible testhosts -m command -a 'ls'
172.7.15.111 | SUCCESS | rc=0 >>
anaconda-ks.cfg
install.log
install.log.syslog

127.0.0.1 | SUCCESS | rc=0 >>
anaconda-ks.cfg
install.log
install.log.syslog



1
2
3
4
5
6
7
8
9
10
[iyunv@web9 ~]# ansible testhosts -m command -a 'hostname'
172.7.15.111 | SUCCESS | rc=0 >>
web10.gz.com

127.0.0.1 | SUCCESS | rc=0 >>
web9.gz.com

[iyunv@web9 ~]# ansible web10.gz.com -m command -a 'hostname'
web10.gz.com | SUCCESS | rc=0 >>
web10.gz.com



执行的过程难免加管道符
第一种方法不行第二种加个shell就哦了
1
2
3
4
5
6
7
8
9
[iyunv@web9 ~]# ansible web10.gz.com -m command -a 'cat /etc/passwd|grep root'
web10.gz.com | FAILED | rc=1 >>
cat: /etc/passwd|grep: 没有那个文件或目录
cat: root: 没有那个文件或目录

[iyunv@web9 ~]# ansible web10.gz.com -m shell -a 'cat /etc/passwd|grep root'
web10.gz.com | SUCCESS | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin











如果有提示一下信息时,第一时间检查防火墙有没有关,密钥有没有配对


1
2
3
4
5
6
7
8
9
10
11
12
13
14
[iyunv@web9 ~]# ansible testhosts -m command -a 'w'
The authenticity of host '172.7.15.106 (172.7.15.106)' can't be established.
RSA key fingerprint is 67:31:c8:49:f6:17:59:88:e3:4a:61:a8:2b:44:20:55.
Are you sure you want to continue connecting (yes/no)? 172.7.15.111 | UNREACHABLE! => {
    "changed": false,
    "msg": "SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue",
    "unreachable": true
}

172.7.15.106 | UNREACHABLE! => {
    "changed": false,
    "msg": "SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue",
    "unreachable": true
}













运维网声明 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-207228-1-1.html 上篇帖子: ansible安装配置及实例 下篇帖子: ansible拷贝目录或者文件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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