利用Shell脚本运行Ansible命令:
本实验采用rhel8.3。这次将使用 yum_repository 模块远程写入仓库配置。
写入仓库:
Shell脚本:
#!/bin/bash
ansible dev -m yum_repository -a 'name=myBase description="myTestRepo \
baseurl="/mnt/BaseOS" gpgcheck=no enabled=no'
ansible dev -m yum_repository -a 'name=myStream description="myTestRepo \
baseurl="/mnt/AppStream" gpgcheck=no enabled=no'
若不知道该模块的具体用请浏览Ansible快速上手。
接着我们修改权限:
chmod +x adhoc.sh
执行结果:
[root@Controller ansible]# ./adhoc.sh
192.168.110.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"repo": "myBase",
"state": "present"
}
192.168.110.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"repo": "myStream",
"state": "present"
}
在对端主机检查:
[root@Controller ansible]# ansible dev -m shell -a 'ls -l /etc/yum.repos.d'
192.168.110.129 | CHANGED | rc=0 >>
total 20
-rw-r--r--. 1 root root 75 Jun 29 10:03 myBase.repo #这里
-rw-r--r--. 1 root root 80 Jun 29 10:03 myStream.repo #这里
-rw-r--r--. 1 root root 358 Apr 9 2021 redhat.repo
-rw-r--r--. 1 root root 113 Mar 5 15:12 rpm.repo
用户管理:
Shell脚本:
#!/bin/bash
ansible dev -m user -a 'name="harry" groups="root" shell="/sbin/nologin"'
执行结果:
[root@Controller ansible]# ./users.sh
192.168.110.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1006,
"groups": "root",
"home": "/home/harry",
"name": "harry",
"shell": "/sbin/nologin",
"state": "present",
"system": false,
"uid": 1006
}
检查:
[root@Controller ansible]# ansible dev -m shell -a 'id harry'
192.168.110.129 | CHANGED | rc=0 >>
uid=1006(harry) gid=1006(harry) groups=1006(harry),0(root)
到此Shell运行Ansible命令就基本掌握了。后续自己可以尝试更多模块的使用。文章若有问题请联系更正——wgq3135@163.com