这里写目录标题
1. ansible常用模块使用详解
ansible常用模块有:
- ping
- yum
- template
- copy
- user
- group
- service
- raw
- command
- shell
- script
ansible常用模块raw
、command
、shell
的区别:
- shell模块调用的/bin/sh指令执行
- command模块不是调用的shell的指令,所以没有bash的环境变量
- raw很多地方和shell类似,更多的地方建议使用shell和command模块。但是如果是使用老版本python,需要用到raw,又或者是客户端是路由器,因为没有安装python模块,那就需要使用raw模块了
2. ansible常用模块之ping
ping模块用于检查指定节点机器是否连通,用法很简单,不涉及参数,主机如果在线,则回复pong
[root@control ~]# ansible all -m ping
192.168.50.136 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
192.168.50.135 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
3. ansible常用模块之command
command模块用于在远程主机上执行命令,ansible默认就是使用command模块。
command模块有一个缺陷就是不能使用管道符和重定向功能。
//查看受控主机的/tmp目录内容
[root@control ~]# ansible all -a 'ls /tmp' >
node2 | CHANGED | rc=0 >>
ansible_command_payload_u_9ojzo8
ks-script-1gn7sb4o
systemd-private-c4d426a50b5a4692a2d227c9254bdbae-chronyd.service-nrpq2i
vmware-root_1009-4281646761
vmware-root_975-4281646728
vmware-root_988-2991203012
node1 | CHANGED | rc=0 >>
ansible_command_payload_o4yeiy7e
ks-script-i78vqz2d
ks-script-ytpwbt3c
vmware-root_789-4290756532
vmware-root_797-4257069498
vmware-root_812-2957648972
4. ansible常用模块之raw
raw模块用于在远程主机上执行命令,其支持管道符与重定向
[root@control ~]# ansible node1 -m raw -a 'yum list all | grep ansible'
node1 | CHANGED | rc=0 >>
ansible-collection-microsoft-sql.noarch 1.4.1-1.el8 appstream
ansible-collection-redhat-rhel_mgmt.noarch 1.1.0-2.el8 appstream
ansible-core.x86_64 2.15.0-1.el8 appstream
ansible-freeipa.noarch 1.11.0-1.el8 appstream
ansible-freeipa-tests.noarch 1.11.0-1.el8 appstream
ansible-pcp.noarch 2.2.5-1.el8 appstream
ansible-test.x86_64 2.15.0-1.el8 appstream
centos-release-ansible-29.noarch 1-2.el8s extras-common
centos-release-ansible-5.noarch 1-1.el8s extras-common
Shared connection to node1 closed.
5. ansible常用模块之shell
shell模块用于在受控机上执行受控机上的脚本,亦可直接在受控机上执行命令。
shell模块亦支持管道与重定向。
[root@control ~]# ansible node1 -m shell -a "rm -rf /tmp/yi"
[WARNING]: Consider using the file module with state=absent rather than running
'rm'. If you need to use command because file is insufficient you can add
'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message.
node1 | CHANGED | rc=0 >>
6. ansible常用模块之script
script模块用于在受控机上执行主控机上的脚本
[root@control ~]# ansible node1 -m script -a '/root/yu.sh'
node1 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to node1 closed.\r\n",
"stderr_lines": [
"Shared connection to node1 closed."
],
"stdout": "hello world\r\n",
"stdout_lines": [
"hello world"
]
}
7. ansible常用模块之template
template模块用于生成一个模板,并可将其传输至远程主机上。
[root@control ~]# ansible node1 -m template -a "src=/root/333 dest=/root/"
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/root/333",
"gid": 0,
"group": "root",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"mode": "0644",
"owner": "root",
"size": 0,
"src": "/root/.ansible/tmp/ansible-tmp-1690621054.3915315-11292-105712734538459/source",
"state": "file",
"uid": 0
}
[root@node1 ~]# ls
333 abc anaconda-ks.cfg
8. ansible常用模块之yum
yum模块用于在指定节点机器上通过yum管理软件,其支持的参数主要有两个
- name:要管理的包名
- state:要进行的操作
state常用的值:
- latest:安装软件,确保安装最新版本
- installed:安装软件
- present:安装软件,确保安装存在
- removed:卸载软件
- absent:卸载软件
若想使用yum来管理软件,请确保受控机上的yum源无异常。
[root@control ~]# ansible node1 -m yum -a 'name=vsftpd state=present'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: vsftpd-3.0.3-36.el8.x86_64"
]
}
//查看受控机上是否安装了vsftpd
[root@node1 ~]# rpm -qa | grep vsftpd
vsftpd-3.0.3-36.el8.x86_64
9. ansible常用模块之copy
copy模块用于复制文件至远程受控机。
[root@control ~]# ansible node1 -m copy -a 'src=/root/333 dest=/opt/'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/opt/333",
"gid": 0,
"group": "root",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"mode": "0644",
"owner": "root",
"size": 0,
"src": "/root/.ansible/tmp/ansible-tmp-1690621411.6394086-11360-128961526567456/source",
"state": "file",
"uid": 0
}
[root@node1 ~]# ls /opt/
333
10. ansible常用模块之group
group模块用于在受控机上添加或删除组。
添加受控机上的组
[root@control ~]# ansible node1 -m group -a 'name=test gid=333 state=present'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 333,
"name": "test",
"state": "present",
"system": false
}
//删除受控机上的组
[root@control ~]# ansible node1 -m group -a 'name=test state=absent'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "test",
"state": "absent"
11. ansible常用模块之user
user模块用于管理受控机的用户帐号。
[root@control ~]# ansible node1 -m user -a 'name=test uid=666 group=333 state=present'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 333,
"home": "/home/test",
"name": "test",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 666
}
查看用户是否创建成功
[root@node1 ~]# id test
uid=666(test) gid=333(tes) groups=333(tes)
12. ansible常用模块之service
service模块用于管理受控机上的服务。
启动受控机上的vsftpd服务
[root@control ~]# ansible node1 -m service -a "name=vsftpd state=started enabled=yes"
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "vsftpd",
"state": "started"
查看是否启动
[root@node1 ~]# systemctl status vsftpd
● vsftpd.service - Vsftpd ftp daemon
Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; enabled; vendor prese>
Active: active (running) since Sat 2023-07-29 17:17:50 CST; 56s ago
Process: 13699 ExecStart=/usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf (code=exited>
Main PID: 13700 (vsftpd)
Tasks: 1 (limit: 10930)
Memory: 580.0K
CGroup: /system.slice/vsftpd.service
└─13700 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
Jul 29 17:17:50 node1 systemd[1]: Starting Vsftpd ftp daemon...
Jul 29 17:17:50 node1 systemd[1]: Started Vsftpd ftp daemon.