-rw-r--r-- 1 root root 43 Oct 23 19:00 test.sh
//使用shell模块在受控机上执行受控机上的脚本
[root@ansible ~]# ansible 192.168.111.142 -m shell -a '/bin/bash /scripts/test.sh &> /tmp/test'
192.168.111.142 | CHANGED | rc=0 >>
[root@ansible ~]# ansible 192.168.111.142 -m shell -a 'cat /tmp/test'
192.168.111.142 | CHANGED | rc=0 >>
123456
123
456
5.ansible常用模块之script
script模块用于在受控机上执行主控机上的脚本
[root@ansible ~]# ll /scripts/
total 4
-rw-r--r-- 1 root root 48 Oct 23 19:06 a.sh
[root@ansible ~]# ansible 192.168.111.142 -m script -a '/scripts/a.sh &> /tmp/a'
192.168.111.142 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.111.142 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.111.142 closed."
],
"stdout": "",
"stdout_lines": []
}
//查看受控机上的/tmp/a文件内容
[root@ansible ~]# ansible 192.168.111.142 -m shell -a 'cat /tmp/a'
192.168.111.142 | CHANGED | rc=0 >>
123
456
hello world
6.ansible常用模块之template
template模块用于生成一个模板,并可将其传输至远程主机上。
[root@ansible ~]# ansible 192.168.111.142 -m template -a 'src=/etc/yum.repos.d/CentOS-Base.repo dest=/etc/yum.repos.d/centos.repo'
192.168.111.142 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "8bbf30b2d80c3b97292ca7b32f33ef494269a5b8",
"dest": "/etc/yum.repos.d/centos.repo",
"gid": 0,
"group": "root",
"md5sum": "ed031c350da2532e6a8d09a4d9b05278",
"mode": "0644",
"owner": "root",
"size": 1653,
"src": "/root/.ansible/tmp/ansible-tmp-1666530145.7581205-29420421110479/source",
"state": "file",
"uid": 0
}
//查看受控机上是否有163源
[root@localhost ~]# ls /etc/yum.repos.d/
centos.repo
7.ansible常用模块之yum
yum模块用于在指定节点机器上通过yum管理软件,其支持的参数主要有两个
state常用的值:
- latest:安装软件
- installed:安装软件
- present:安装软件
- removed:卸载软件
- absent:卸载软件
若想使用yum来管理软件,请确保受控机上的yum源无异常。
//在受控机上查询看vsftpd软件是否安装
[root@localhost ~]# rpm -qa|grep httpd
[root@localhost ~]#
//在ansible主机上使用yum模块在受控机上安装httpd
[root@ansible ~]# ansible 192.168.111.142 -m yum -a 'name=httpd state=present'
192.168.111.142 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: mailcap-2.1.48-3.el8.noarch",
"Installed: httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64",
"Installed: apr-1.6.3-12.el8.x86_64",
"Installed: httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch",
"Installed: apr-util-1.6.1-6.el8.x86_64",
"Installed: apr-util-bdb-1.6.1-6.el8.x86_64",
"Installed: httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64",
"Installed: centos-logos-httpd-85.8-2.el8.noarch",
"Installed: m