首页 技术 正文
技术 2022年11月12日
0 收藏 699 点赞 2,439 浏览 2638 个字

安装

yum -y install ansible

配置文件/etc/ansible/hosts

模块介绍与使用

ping模块

 [root@node1 config]# ansible k8s -m ping
192.168.1.3 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.1.2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.1.1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[root@node1 config]#

command模块和shell

shell和command的区别:shell模块可以特殊字符,而command是不支持

 [root@node1 config]# ansible k8s -m command -a 'date'
192.168.1.1 | SUCCESS | rc= >>
2018年 01月 21日 星期日 :: CST 192.168.1.3 | SUCCESS | rc= >>
2018年 01月 21日 星期日 :: CST 192.168.1.2 | SUCCESS | rc= >>
2018年 01月 21日 星期日 :: CST

command万能模块也可以这样写

 [root@node1 config]# ansible k8s -a 'date'
192.168.1.1 | SUCCESS | rc= >>
2018年 01月 21日 星期日 :: CST 192.168.1.3 | SUCCESS | rc= >>
2018年 01月 21日 星期日 :: CST 192.168.1.2 | SUCCESS | rc= >>
2018年 01月 21日 星期日 :: CST [root@node1 config]#

shell模块支持特殊字符比如空格

 [root@node1 config]# ansible k8s -m shell -a 'touch /tmp/ansible.txt' 192.168.1.1 | SUCCESS | rc= >> 192.168.1.3 | SUCCESS | rc= >> 192.168.1.2 | SUCCESS | rc= >> [root@node1 config]# ansible k8s -m shell -a 'ls -l /tmp/ansible.txt'
192.168.1.1 | SUCCESS | rc= >>
-rw-r--r-- root root 1月 : /tmp/ansible.txt 192.168.1.3 | SUCCESS | rc= >>
-rw-r--r-- root root 1月 : /tmp/ansible.txt 192.168.1.2 | SUCCESS | rc= >>
-rw-r--r-- root root 1月 : /tmp/ansible.txt [root@node1 config]#

 copy模块

把本地的文件拷贝到远程机器上去

 

 [root@node1 tmp]# ansible k8s -m copy -a 'src=/tmp/ahahaa dest=/tmp/'
192.168.1.3 | SUCCESS => {
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/tmp/ahahaa",
"gid": ,
"group": "root",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"mode": "",
"owner": "root",
"size": ,
"src": "/root/.ansible/tmp/ansible-tmp-1516526258.65-233570741395818/source",
"state": "file",
"uid":
}
192.168.1.2 | SUCCESS => {
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/tmp/ahahaa",
"gid": ,
"group": "root",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"mode": "",
"owner": "root",
"size": ,
"src": "/root/.ansible/tmp/ansible-tmp-1516526258.64-160953882755803/source",
"state": "file",
"uid":
}
192.168.1.1 | SUCCESS => {
"changed": false,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"gid": ,
"group": "root",
"mode": "",
"owner": "root",
"path": "/tmp/ahahaa",
"size": ,
"state": "file",
"uid":
}
[root@node1 tmp]#

src本地文件路径

dest远程文件路径

script模块

在远程服务器上执行本地脚本

 [root@node1 tmp]# ansible k8s -m script -a '/tmp/sh.sh'
192.168.1.2 | SUCCESS => {
"changed": true,
"rc": ,
"stderr": "Shared connection to 192.168.1.2 closed.\r\n",
"stdout": "caojiaoyue\r\n",
"stdout_lines": [
"caojiaoyue"
]
}
192.168.1.3 | SUCCESS => {
"changed": true,
"rc": ,
"stderr": "Shared connection to 192.168.1.3 closed.\r\n",
"stdout": "caojiaoyue\r\n",
"stdout_lines": [
"caojiaoyue"
]
}
192.168.1.1 | SUCCESS => {
"changed": true,
"rc": ,
"stderr": "Shared connection to 192.168.1.1 closed.\r\n",
"stdout": "caojiaoyue\r\n",
"stdout_lines": [
"caojiaoyue"
]
}
[root@node1 tmp]#

参考文档http://blog.51cto.com/liuzhengwei521/1895480

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903