一:远程执行命令
语法:ansible 【group\ip\host】 -m【模块】-a 【命令】
示列: ansible 192.168.2.252 -m command -a 'w'
1.使用远程执行命令前需要
修改ansible配置,
添加testhosts组,
添加对应的远程Ip
[root@ghs ~]# vim /etc/ansible/hosts
[testhosts]
127.0.0.1
192.168.2.252
2:远程执行w命令查看127.0.0.1和192.168.2.252的状态,以ip地址形式执行
[root@ghs ~]# ansible 192.168.2.252 -m command -a 'w'
192.168.2.252 | SUCCESS | rc=0 >>
08:21:03 up 3:05, 5 users, load average: 0.01, 0.03, 0.05
USER TTY FROM LOGIN@ IDLE J
cpu P
cpu WHAT
root tty1 - 05:16 3:03m 0.09s 0.09s -bash
root pts/0 192.168.2.108 06:10 1:12m 0.46s 0.18s bash
root pts/1 192.168.2.251 07:43 37:29 0.04s 0.04s -bash
root pts/2 192.168.2.108 08:13 8:00 0.15s 0.15s -bash
root pts/3 192.168.2.251 08:21 0.00s 0.35s 0.01s /bin/sh -c /usr
或者以定义的组执行
[root@ghs ~]# ansible testhosts -m command -a 'w'
192.168.2.252 | SUCCESS | rc=0 >>
08:21:03 up 3:05, 5 users, load average: 0.01, 0.03, 0.05
USER TTY FROM LOGIN@ IDLE J
cpu P
cpu WHAT
root tty1 - 05:16 3:03m 0.09s 0.09s -bash
root pts/0 192.168.2.108 06:10 1:12m 0.46s 0.18s bash
root pts/1 192.168.2.251 07:43 37:29 0.04s 0.04s -bash
root pts/2 192.168.2.108 08:13 8:00 0.15s 0.15s -bash
root pts/3 192.168.2.251 08:21 0.00s 0.35s 0.01s /bin/sh -c /usr
127.0.0.1 | SUCCESS | rc=0 >>
22:14:29 up 15:33, 7 users, load average: 0.01, 0.05, 0.05
USER TTY FROM LOGIN@ IDLE J
cpu P
cpu WHAT
root tty1 - 06:42 15:15m 0.11s 0.11s -bash
root pts/0 192.168.2.108 19:13 37:35 0.48s 0.07s ssh root@192.16
root pts/1 192.168.2.108 22:06 3.00s 0.44s 0.28s ssh 127.0.0.1
root pts/2 localhost 22:06 3.00s 0.29s 0.21s ssh 127.0.0.1
root pts/3 localhost 22:07 3.00s 0.20s 0.04s ssh 127.0.0.1
root pts/4 localhost 22:14 3.00s 1.14s 0.89s /usr/bin/python
root pts/7 localhost 22:14 1.00s 0.89s 0.00s /bin/sh -c /usr
3:command模块是
不支持|管道符的会报错
[root@ghs ~]# ansible testhosts -m command -a 'cat /etc/passwd|grep root'
192.168.2.252 |
Failed | rc=1 >>
cat: /etc/passwd|grep: 没有那个
文件或目录
cat: root: 没有那个
文件或目录
127.0.0.1 |
Failed | rc=1 >>
cat: /etc/passwd|grep: 没有那个
文件或目录
cat: root: 没有那个
文件或目录
使用管道符号需要
调用shell模块
[root@ghs ~]# ansible testhosts -m shell -a 'cat /etc/passwd|grep root'
192.168.2.252 | SUCCESS | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
127.0.0.1 | SUCCESS | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
二:拷贝目录和
文件copy模板
src:【source】【Route】
dest:【target】【Route】
拷贝的目标路径如果目录存在,会在目标目录下创建,如果不存在,直接创建目标的目录名
host1 :192.168.2.251
1:拷贝目录
将本机的/etc/ansible
文件夹拷贝到远程上的/tmp/
[root@ghs ~]# ansible 192.168.2.252 -m
copy -a "src=/etc/ansible dest=/tmp/ owner=root group=root mode=0755"
192.168.2.252 | SUCCESS => {
"changed": true,
"dest": "/tmp/",
"src": "/etc/ansible"
}
host2:192.168.2.252
查看tmp目录下是否有ansible目录
[root@ghs2 ~]# ls /tmp/ansible/
ansible.cfg hosts
host1 :192.168.2.251
2:拷贝
文件
拷贝本机的/etc/passwd到远程机上的/tmp目录下
[root@ghs ~]# ansible 192.168.2.252 -m
copy -a "src=/etc/passwd dest=/tmp/1.txt "
192.168.2.252 | SUCCESS => {
"changed": true,
"checksum": "bb5f5e7ec89beebcd7dc7fe5d188ccfd4ded3ac5",
"dest": "/tmp/1.txt",
"gid": 0,
"group": "root",
"md5sum": "17f24152f467a0e80b9b4aa96af47ece",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 902,
"src": "/root/.ansible/tmp/ansible-tmp-1500738575.79-49230594941144/source",
"state": "file",
"uid": 0
}
host1 :192.168.2.251
查看是否有/tmp/1.txt
文件
[root@ghs2 ~]# ls /tmp/
1.txt aa ansible yum.log
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。