[root@promote ~]# vim test.sh
#!/bin/bash
echo "this is test script" > /opt/script.txt
chmod 666 /opt/script.txt #写一个脚本,表示在/opt/创建一个script.txt文件,权限设为666
[root@promote ~]# chmod +x test.sh
[root@promote ~]# ansible web -m script -a 'test.sh'
192.168.199.130 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.199.130 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.199.130 closed."
],
"stdout": "",
"stdout_lines": []
}
[root@web ~]# ls -l /opt/script.txt #到web服务器上进行查看
-rw-rw-rw-. 1 root root 20 10月 23 00:07 /opt/script.txt
[root@web ~]# cat /opt/script.txt
this is test script
12 setup 模块
在ansible中使用setup模块收集,查看被管理主机的facts(faces是ansible采集被管理主机设备信息的一个功能)。每个被管理主机在接受并运行管理命令之前,都会将自己的相关信息发送给控制主机。
[root@promote ~]# ansible web -m setup #对web服务器进行查看,显示的信息非常多,这里我只选了一部分
192.168.199.130 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.122.1",
"192.168.199.130"
],
"ansible_all_ipv6_addresses": [
"fe80::a392:f598:b619:50"
],
"ansible_apparmor": {
"status": "disabled"
},
"ansible_architecture": "x86_64",
"ansible_bios_date": "05/19/2017",
"ansible_bios_version": "6.00",
"ansible_cmdline": {
"BOOT_IMAGE": "/boot/vmlinuz-3.10.0-693.el7.x86_64",
"LANG": "zh_CN.UTF-8",
"crashkernel": "auto",
"quiet": true,
"rhgb": true,
"ro": true,
"root": "UUID=1eead85f-d0ea-464e-b163-f9c7475dbf65"
},
...........
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx