ansible的常见模块
·
ansible的常见模块
1.ping模块
用于检查指定节点是否连通,如果连通,则会回复pong
[root@ansible ~]# ansible all -m ping
192.168.67.133 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
192.168.67.130 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@ansible ~]#
2.command模块
用于远程执行简单的命令,是ansible默认使用的模块。
注意:command模块不支持变量,“<”,“>”,“|”,“;”,"&"等符号
[root@ansible ~]# ansible 192.168.67.130 -a 'ls /tmp' //查看受控主机的/tmp目录
192.168.67.130 | CHANGED | rc=0 >>
ansible_ansible.legacy.command_payload_4TYOQi
systemd-private-1e375033baac4f069f3951a3d4c49206-chronyd.service-wInTSa
systemd-private-7000b7c472cd4622ab977648090f4280-chronyd.service-FlhXO4
systemd-private-904b21b954ae4e2a901f94e04da8771e-chronyd.service-lFZJeM
systemd-private-c6c0f6c41d1e4e7da89fc9ba9f94d9fa-chronyd.service-WOgc8w
vmware-root
[root@ansible ~]# ansible 192.168.67.130 -a 'touch /tmp/test' //在受控主机/tmp目录下新建test文件
192.168.67.130 | CHANGED | rc=0 >>
[root@ansible ~]# ansible 192.168.67.130 -a 'ls /tmp' //查看/tmp目录
192.168.67.130 | CHANGED | rc=0 >>
ansible_ansible.legacy.command_payload_4KXP2x
systemd-private-1e375033baac4f069f3951a3d4c49206-chronyd.service-wInTSa
systemd-private-7000b7c472cd4622ab977648090f4280-chronyd.service-FlhXO4
systemd-private-904b21b954ae4e2a901f94e04da8771e-chronyd.service-lFZJeM
systemd-private-c6c0f6c41d1e4e7da89fc9ba9f94d9fa-chronyd.service-WOgc8w
test
vmware-root
//command模块不支持重定向(>)和管道符(|)
[root@ansible ~]# ansible 192.168.67.130 -a "echo 'hello world' > /tmp/test"
192.168.67.130 | CHANGED | rc=0 >>
hello world > /tmp/test
[root@ansible ~]# ansible 192.168.67.130 -a 'cat /tmp/test'
192.168.67.130 | CHANGED | rc=0 >>
[root@ansible ~]# ansible 192.168.67.130 -a 'ps -ef|grep vsftpd'
192.168.67.130 | FAILED | rc=1 >>
error: unsupported SysV option
Usage:
ps [options]
Try 'ps --help <simple|list|output|threads|misc|all>'
or 'ps --help <s|l|o|t|m|a>'
for additional help text.
For more details see ps(1).non-zero return code
[root@ansible ~]#
3.raw模块
用于在远程主机上执行命令,该模块支持重定向 (>) 和管道符 (|)。
[root@ansible ~]# ansible 192.168.67.130 -m raw -a "echo 'hello world' > /tmp/test"
192.168.67.130 | CHANGED | rc=0 >>
Shared connection to 192.168.67.130 closed.
[root@ansible ~]# ansible 192.168.67.130 -a 'cat /tmp/test'
192.168.67.130 | CHANGED | rc=0 >>
hello world
[root@ansible ~]# ansible 192.168.67.130 -m raw -a 'ps -ef|grep vsftpd'
192.168.67.130 | CHANGED | rc=0 >>
root 2148 2003 0 19:41 pts/1 00:00:00 bash -c ps -ef|grep vsftpd
root 2160 2148 0 19:41 pts/1 00:00:00 grep vsftpd
Shared connection to 192.168.67.130 closed.
[root@ansible ~]#
4.shell模块
用于在远程主机上通过 shell 解释器执行命令,shell模块支持重定向和管道符。
[root@ansible ~]# ansible 192.168.67.130 -m shell -a '/bin/bash text.sh &> /tmp/test'
192.168.67.130 | CHANGED | rc=0 >>
[root@ansible ~]# ansible 192.168.67.130 -m shell -a 'cat /tmp/test'
192.168.67.130 | CHANGED | rc=0 >>
姓名 性别 体重/kg
张三 男 70.56
李四 男 60.59
王五 男 88.13
The first letter of the alphabet is a
The characters are: a b c
The letter is:
”“ZThe name of this website is Stack Overflow
The names are: Alice Bob Eve
The greeting is: Hello, world!
The number is: 42
The numbers are: 1 2 3
The count is: 5
The number is: 5
The numbers are: 5 6 7
The binary number is: 1101
There are 8 characters in this string.The value of count is: 38
There are 100 characters in string 1.There are 1000 characters in string 2.The value of count1 is: 37
The value of count2 is: 38
The current date and time is: 1970-01-01 08:00:00
The date and time is: 四 1月 01 08:00 上午 CST 1970
[root@ansible ~]#
5.script模块
script模块用于在受控机上执行主控机上的脚本。
[root@localhost ~]# vim file.sh //在主控机上创建一个脚本
[root@localhost ~]# cat file.sh
#!/bin/bash
function foo(){
echo "The script name is :$0"
echo "The first argument is :$1"
echo "The second argument is :$2"
echo "The number argument is :$#"
echo "The argument as a single word :$*"
echo "The argument as separate words :$@"
}
foo wjj ceyz ljx
[root@localhost ~]# ansible 192.168.67.130 -m script -a 'file.sh &> /tmp/test' //执行脚本
192.168.67.130 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.67.130 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.67.130 closed."
],
"stdout": "",
"stdout_lines": []
}
[root@localhost ~]# ansible 192.168.67.130 -m shell -a 'cat /tmp/test' //在受控上查看/tmp/test内容
192.168.67.130 | CHANGED | rc=0 >>
The script name is :/root/.ansible/tmp/ansible-tmp-1732261564.267714-1555-179572057777576/file.sh
The first argument is :wjj
The second argument is :ceyz
The number argument is :3
The argument as a single word :wjj ceyz ljx
The argument as separate words :wjj ceyz ljx
[root@localhost ~]#
6.template模块
生成一个模版,并可将其传输到远程主机上。
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# curl -o CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1572 100 1572 0 0 2132 0 --:--:-- --:--:-- --:--:-- 2130
[root@localhost yum.repos.d]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@localhost yum.repos.d]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@localhost yum.repos.d]# ls
base.repo CentOS7-Base-163.repo local.repo redhat.repo rocky.repo
[root@localhost yum.repos.d]# ansible 192.168.67.130 -m template -a 'src=/etc/yum.repos.d/CentOS7-Base-163.repo dest=/etc/yum.repos.d/163.repo'
192.168.67.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "60b8868e0599489038710c45025fc11cbccf35f2",
"dest": "/etc/yum.repos.d/163.repo",
"gid": 0,
"group": "root",
"md5sum": "5a3e688854d9ceccf327b953dab55b21",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:system_conf_t:s0",
"size": 1462,
"src": "/root/.ansible/tmp/ansible-tmp-1732262204.561825-1602-209500943875075/source",
"state": "file",
"uid": 0
}
[root@localhost yum.repos.d]# cd
[root@localhost ~]# ssh 192.168.67.130
Last login: Fri Nov 22 23:56:45 2024 from 192.168.67.128
[root@managed-node1 ~]# ls /etc/yum.repos.d/
163.repo local.repo
[root@managed-node1 ~]# exit
登出
Connection to 192.168.67.130 closed.
[root@localhost ~]#
7.yum模块
用于在指定节点机器上通过yum管理软件
应用场景:
1.批量安装软件包2.配置软件包3.管理软件包仓库4.监控软件包状态
支持以下参数
- name参数:要管理的包名
- state参数:要进行的操作
state常用值:
- latest:安装软件
- installed:安装软件
- present:安装软件
- removed:卸载软件
- absent:卸载软件
[root@ansible ~]# rpm -qa | grep vsftpd
[root@ansible ~]# ansible 192.168.67.133 -m yum -a 'name=vsftpd state=present'
192.168.67.133 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: vsftpd-3.0.5-5.el9.x86_64"
]
}
8.copy模块
用于复制文件至远程主机
[root@ansible ~]# ansible 192.168.67.130 -m copy -a 'src=/etc/ansible/scripts/a.sh dest=/scripts/'
192.168.67.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "6a1e43636f367471b0711ae36b5f3c4de89ca506",
"dest": "/scripts/a.sh",
"gid": 0,
"group": "root",
"md5sum": "02c82a64ae9ebbc017868bd63aa1d0df",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:default_t:s0",
"size": 144,
"src": "/root/.ansible/tmp/ansible-tmp-1732500017.1640089-1156-208278004228833/source",
"state": "file",
"uid": 0
}
[root@ansible ~]# ansible 192.168.67.130 -m shell -a 'ls /scripts/'
192.168.67.130 | CHANGED | rc=0 >>
a.sh
[root@ansible ~]#
9.group模块
用于管理用户组的模块。
常见参数:
gid参数设置组的id。
name参数用户组的名称
[root@ansible ~]# ansible all -m group -a 'name=mysql gid=306 state=present'
192.168.67.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 306,
"name": "mysql",
"state": "present",
"system": false
}
192.168.67.134 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"gid": 306,
"name": "mysql",
"state": "present",
"system": false
}
192.168.67.133 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"gid": 306,
"name": "mysql",
"state": "present",
"system": false
}
[root@ansible ~]# ansible all -m shell -a 'grep mysql /etc/group'
192.168.67.130 | CHANGED | rc=0 >>
mysql:x:306:
192.168.67.134 | CHANGED | rc=0 >>
mysql:x:306:
192.168.67.133 | CHANGED | rc=0 >>
mysql:x:306:
[root@ansible ~]# ansible all -m group -a 'name=mysql state=absent'
192.168.67.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "mysql",
"state": "absent"
}
192.168.67.134 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"name": "mysql",
"state": "absent"
}
192.168.67.133 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"name": "mysql",
"state": "absent"
}
[root@ansible ~]#
10.user模块
用户管理模块
常见参数:
name 指定用户名
state present:创建用户,可以不加,默认
absent :删除一定要加absent
system=yes|no。标记用户使用是一个程序用户
uid:用户的唯一标识
[root@ansible ~]# ansible 192.168.67.130 -m user -a 'name=mysql uid=306 system=yes create_home=no shell=/sbin/nologin state=present'
192.168.67.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": false,
"group": 306,
"home": "/home/mysql",
"name": "mysql",
"shell": "/sbin/nologin",
"state": "present",
"system": true,
"uid": 306
}
[root@ansible ~]# ansible 192.168.67.130 -m shell -a 'grep mysql /etc/passwd'
192.168.67.130 | CHANGED | rc=0 >>
mysql:x:306:306::/home/mysql:/sbin/nologin
[root@ansible ~]# ansible 192.168.67.130 -m shell -a 'ls /home'
192.168.67.130 | CHANGED | rc=0 >>
[root@ansible ~]# ansible 192.168.67.130 -m uesr -a 'name=mysql uid=366'
192.168.67.130 | FAILED! => {
"msg": "The module uesr was not found in configured module paths"
}
[root@ansible ~]# ansible 192.168.67.130 -m user -a 'name=mysql uid=366'
192.168.67.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"append": false,
"changed": true,
"comment": "",
"group": 306,
"home": "/home/mysql",
"move_home": false,
"name": "mysql",
"shell": "/sbin/nologin",
"state": "present",
"uid": 366
}
[root@ansible ~]# ansible 192.168.67.130 -m shell -a 'grep mysql /etc/passwd'
192.168.67.130 | CHANGED | rc=0 >>
mysql:x:366:306::/home/mysql:/sbin/nologin
[root@ansible ~]# ansible 192.168.67.130 -m user -a 'name=mysql state=absent'
192.168.67.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"force": false,
"name": "mysql",
"remove": false,
"state": "absent"
}
[root@ansible ~]# ansible 192.168.67.130 -m shell -a 'grep mysql /etc/passwd'
192.168.67.130 | FAILED | rc=1 >>
non-zero return code
[root@ansible ~]#
11.service模块
service模块用于管理受控机上的服务。
[root@ansible ~]# ansible 192.168.67.133 -m shell -a 'systemctl is-active vsftpd'
192.168.67.133 | CHANGED | rc=0 >>
active
[root@ansible ~]# ansible 192.168.67.133 -m service -a 'name=vsftpd state=started'
192.168.67.133 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"name": "vsftpd",
"state": "started",
"status": {
"AccessSELinuxContext": "system_u:object_r:ftpd_unit_file_t:s0",
"ActiveEnterTimestampMonotonic": "0",
..............
[root@ansible ~]# ansible 192.168.67.133 -m shell -a 'systemctl is-active vsftpd'
192.168.67.133 | CHANGED | rc=0 >>
active
[root@ansible ~]# ansible 192.168.67.133 -m shell -a 'systemctl is-enabled vsftpd'
192.168.67.133 | FAILED | rc=1 >>
disablednon-zero return code
[root@ansible ~]# ansible 192.168.67.133 -m service -a 'name=vsftpd enabled=yes'
192.168.67.133 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"enabled": true,
"name": "vsftpd",
"status": {
................
[root@ansible ~]# ansible 192.168.67.133 -m shell -a 'systemctl is-enabled vsftpd'
192.168.67.133 | CHANGED | rc=0 >>
enabled
[root@ansible ~]# ansible 192.168.67.133 -m service -a 'name=vsftpd state=stopped'
192.168.67.133 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"name": "vsftpd",
"state": "stopped",
"status": {
.....................
[root@ansible ~]# ansible 192.168.67.133 -m shell -a 'systemctl is-active vsftpd'
192.168.67.133 | FAILED | rc=3 >>
inactivenon-zero return code
[root@ansible ~]# ansible 192.168.67.133 -m shell -a 'ss -antl'
192.168.67.133 | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@ansible ~]#
12.file模块
用于设定文件属性
相关参数:
owner、group、mode等
state=link:创建链接文件
state=touch:创建文件
state=directory 创建目录
state=absent:删除文件
[root@ansible ~]# ansible 192.168.67.130 -m file -a 'path=/tmp/test state=touch owner=root group=root'
192.168.67.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/test",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 7,
"state": "file",
"uid": 0
}
[root@ansible ~]# ansible 192.168.67.130 -m file -a 'path=/tmp/www state=directory mode=0755'
192.168.67.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/www",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 6,
"state": "directory",
"uid": 0
}
[root@ansible ~]# ansible 192.168.67.130 -m shell -a 'ls -l /tmp'
192.168.67.130 | CHANGED | rc=0 >>
总用量 4
drwx------. 2 root root 56 11月 27 17:08 ansible_ansible.legacy.command_payload_CLhtD7
drwx------. 3 root root 17 11月 20 18:33 systemd-private-1e375033baac4f069f3951a3d4c49206-chronyd.service-wInTSa
drwx------. 3 root root 17 11月 27 16:39 systemd-private-2c6dd462dc9b47138e615a7f971617cc-chronyd.service-fVFJps
drwx------. 3 root root 17 11月 22 22:38 systemd-private-47c766f742144fa48779a67a5a78e3cd-chronyd.service-s5xKkR
drwx------. 3 root root 17 11月 19 18:20 systemd-private-7000b7c472cd4622ab977648090f4280-chronyd.service-FlhXO4
drwx------. 3 root root 17 11月 26 17:50 systemd-private-8a780cab4b7d4c20a4f29bdb172821d8-chronyd.service-OVBT2I
drwx------. 3 root root 17 11月 21 03:26 systemd-private-8cc50acb3ddd40d7aa057971265d42bf-chronyd.service-QuUK0A
drwx------. 3 root root 17 11月 25 17:46 systemd-private-e082f866beb148b1856ceff1f26825ef-chronyd.service-CG5ZTk
-rw-r--r--. 1 root root 7 11月 27 17:06 test
drwx------. 2 root root 6 11月 26 17:50 vmware-root
drwxr-xr-x. 2 root root 6 11月 27 17:07 www
[root@ansible ~]#
更多推荐



所有评论(0)