ansible常用模块
ansible常用模块有:
ansible官方文档之Modules
ansible常用模块raw、command、shell的区别:
ping模块用于检查指定节点机器是否连通,用法很简单,不涉及参数,主机如果在线,则回复pong
[root@ansible ~]# ansible all -m ping
192.168.188.129 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
command模块用于在远程主机上执行命令,ansible默认就是使用command模块。
command模块有一个缺陷就是不能使用管道符和重定向功能。
#查看受控主机的/tmp目录内容
[root@ansible ~]# ansible 192.168.188.129 -a 'ls /tmp'
192.168.188.129 | CHANGED | rc=0 >>
ansible_command_payload_0cwqgbt4
vmware-root_914-2689209517
vmware-root_925-3988621690
vmware-root_926-2731217702
#在受控主机的/tmp目录下新建一个文件test
[root@ansible ~]# ansible 192.168.188.129 -a 'touch /tmp/test'
[WARNING]: Consider using the file module with state=touch rather than running 'touch'. 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.
192.168.188.129 | CHANGED | rc=0 >>
[root@ansible ~]# ansible 192.168.188.129 -a 'ls /tmp'
192.168.188.129 | CHANGED | rc=0 >>
ansible_command_payload_g3xgq_ox
test
vmware-root_914-2689209517
vmware-root_925-3988621690
vmware-root_926-2731217702
#command模块不支持管道符,不支持重定向
[root@ansible ~]# ansible 192.168.188.129 -a "echo 'hello world' > /tmp/test"
192.168.188.129 | CHANGED | rc=0 >>
hello world > /tmp/test
[root@ansible ~]# ansible 192.168.188.129 -a 'cat /tmp/test'
192.168.188.129 | CHANGED | rc=0 >>
[root@ansible ~]# ansible 192.168.188.129 -a 'ps -ef|grep vsftpd'
192.168.188.129 | FAILED | rc=1 >>
error: unsupported SysV option
Usage:
ps [options]
Try 'ps --help '
or 'ps --help '
for additional help text.
For more details see ps(1).non-zero return code
raw模块用于在远程主机上执行命令,其支持管道符与重定向
#支持重定向
[root@ansible ~]# ansible 192.168.188.129 -m raw -a 'echo "hello world" > /tmp/test'
192.168.188.129 | CHANGED | rc=0 >>
Shared connection to 192.168.188.129 closed.
[root@ansible ~]# ansible 192.168.188.129 -a 'cat /tmp/test'
192.168.188.129 | CHANGED | rc=0 >>
hello world
#支持管道符
[root@ansible ~]# ansible 192.168.188.129 -m raw -a 'cat /tmp/test|grep -Eo hello'
192.168.188.129 | CHANGED | rc=0 >>
hello
Shared connection to 192.168.188.129 closed.
shell模块用于在受控机上执行受控机上的脚本,亦可直接在受控机上执行命令。
shell模块亦支持管道与重定向。
#查看受控机上的脚本
[root@loaclhost ~]# ll /scripts/
total 4
-rwxr-xr-x 1 root root 175 Oct 23 16:54 99.sh
#使用shell模块在受控机上执行受控机上的脚本
[root@ansible ~]# ansible 192.168.188.129 -m shell -a '/bin/bash /scripts/99.sh &> /tmp/test'
192.168.188.129 | CHANGED | rc=0 >>
[root@ansible ~]# ansible 192.168.188.129 -m shell -a 'cat /tmp/test'
192.168.188.129 | CHANGED | rc=0 >>
==== 九九乘法表 ====
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
script模块用于在受控机上执行主控机上的脚本
[root@ansible ~]# ll /etc/ansible/scripts/
total 4
-rwxr-xr-x. 1 root root 458 Oct 23 17:06 cowsay.sh
[root@ansible ~]# ansible 192.168.188.129 -m script -a '/etc/ansible/scripts/cowsay.sh &> /tmp/a'
192.168.188.129 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.188.129 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.188.129 closed."
],
"stdout": "",
"stdout_lines": []
}
#查看受控机上的/tmp/a文件内容
[root@ansible ~]# ansible 192.168.188.129 -m shell -a 'cat /tmp/a' 192.168.188.129 | CHANGED | rc=0 >>
__________________
< Hello, Jinqiang! >
------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
//由此可见确是在受控机上执行了主控机上的脚本,且输出记录到了受控机上。
template模块用于生成一个模板,并可将其传输至远程主机上。
本篇文章只介绍基本作用,后续会出一篇template模块的专题文章。
官方文档之teamplate模块
template常用参数说明:
template模块参数 | 说明 |
---|---|
src | 要复制到远程主机的源模板文件路径,可以是绝对路径,也可以是相对路径 |
dest | 远程主机的目标路径,必须是一个绝对路径 |
mode | 设置文件拷贝到目标主机后的权限 |
owner | 设置文件拷贝到目标主机后的属主 |
group | 设置文件拷贝到目标主机后的属组 |
force | 默认为yes,表示目标主机存在该文件会进行强制覆盖;设为no,则表示只有当目标主机不存在该文件时才会进行传输 |
backup | 默认为no,如果为yes,那么当目标主机的目标路径中已经存在同名文件时,在覆盖之前将原文件进行备份 |
#下载一个163的yum源文件并开启此源
[root@ansible ~]# cd /etc/yum.repos.d/
[root@ansible 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 5402 0 --:--:-- --:--:-- --:--:-- 5420
[root@ansible yum.repos.d]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@ansible yum.repos.d]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
#将设置好的163源传到受控主机
[root@ansible ~]# ansible 192.168.188.129 -m template -a 'src=/etc/yum.repos.d/CentOS7-Base-163.repo dest=/etc/yum.repos.d/163.repo'
192.168.188.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "60b8868e0599489038710c45025fc11cbccf35f2",
"dest": "/etc/yum.repos.d/163.repo",
"gid": 0,
"group": "root",
"md5sum": "5a3e688854d9ceccf327b953dab55b21",
"mode": "0644",
"owner": "root",
"size": 1462,
"src": "/root/.ansible/tmp/ansible-tmp-1666516852.8839266-2787-255496984748959/source",
"state": "file",
"uid": 0
}
#查看受控机上是否有163源
[root@loaclhost ~]# ls /etc/yum.repos.d/
163.repo
yum模块用于在指定节点机器上通过yum管理软件,其支持的参数主要有两个
state常用的值:
若想使用yum来管理软件,请确保受控机上的yum源无异常。
yum模块常用参数:
yum模块参数 | 说明 |
---|---|
name | 指定软件包名 |
state | latest、installed、present(安装软件,默认为安装可不指定)removed、absent(卸载软件) |
updata_cache | 更新yum仓库缓存。默认为no,实际应用建议设为yes开启 |
#在受控机上查询看vsftpd软件是否安装
[root@loaclhost ~]# rpm -qa|grep vsftpd
[root@loaclhost ~]#
#在ansible主机上使用yum模块在受控机上安装vsftpd
[root@ansible ~]# ansible 192.168.188.129 -m yum -a 'name=vsftpd state=present'
192.168.188.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: vsftpd-3.0.3-34.el8.x86_64"
]
}
#查看受控机上是否安装了vsftpd
[root@loaclhost ~]# rpm -qa|grep vsftpd
vsftpd-3.0.3-34.el8.x86_64
copy模块用于复制文件至远程受控机。
copy模块用来将主控节点的文件或目录拷贝到远程服务器上,类似于Linux下的scp命令。但是,copy模块比scp命令更强大,在拷贝文件到远程服务器的同时,也可以设置文件在远程服务器的权限和所有者。
copy模块常用参数:
copy模块参数 | 说明 |
---|---|
src | 要复制到远程主机的源文件路径,可以是绝对路径,也可以是相对路径,如果路径是一个目录,将递归复制。在这种情况下,如果路径使用"/“结尾,则只复制目录里的内容,如果没有使用”/"来结尾,则将包含目录在内整个内容全部复制,类似于rsync。 |
dest | 远程主机的目标路径,必须是一个绝对路径,如果源文件是一个目录,那么,dest指向的也必须是一个目录。 |
force | 默认为yes,表示目标主机存在该文件会进行强制覆盖;设为no,则表示只有当目标主机不存在该文件时才会进行传输 |
backup | 默认为no,如果为yes,那么当目标主机的目标路径中已经存在同名文件时,在覆盖之前将原文件进行备份 |
mode | 设置copy到目标主机上的文件或目录的权限 |
owner | 设置copy到目标主机上的文件或目录的属主 |
group | 设置copy到目标主机上的文件或目录的属组 |
[root@ansible ~]# ls /etc/ansible/scripts/
cowsay.sh
[root@ansible ~]# ansible 192.168.188.129 -m copy -a 'src=/etc/ansible/scripts/cowsay.sh dest=/scripts/'
192.168.188.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "f1865ca66ecfb108bb71a26cc9dcf5b7748c3930",
"dest": "/scripts/cowsay.sh",
"gid": 0,
"group": "root",
"md5sum": "79f2287088b88c77ce68c871ecbf97fb",
"mode": "0644",
"owner": "root",
"size": 458,
"src": "/root/.ansible/tmp/ansible-tmp-1666519714.3028471-2939-168263544543020/source",
"state": "file",
"uid": 0
}
[root@ansible ~]# ansible 192.168.188.129 -m shell -a 'ls /scripts/'
192.168.188.129 | CHANGED | rc=0 >>
99.sh
cowsay.sh
group模块用于在受控机上添加或删除组。
group模块请求的是groupadd,groupdel,groupmod三个指令,用法也与这些命令基本一致
group常用参数说明:
group模块参数 | 说明 |
---|---|
name | 设置组名 |
gid | 设置gid |
state | state=present添加组,state=absent删除组 |
system | 设置创建的组为系统组,默认为no |
#在受控机上添加一个系统组,其gid为306,组名为mysql
[root@ansible ~]# ansible 192.168.188.129 -m group -a 'name=mysql gid=306 state=present'
192.168.188.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 306,
"name": "mysql",
"state": "present",
"system": false
}
[root@ansible ~]# ansible 192.168.188.129 -m shell -a 'grep mysql /etc/group'
192.168.188.129 | CHANGED | rc=0 >>
mysql:x:306:
#删除受控机上的mysql组
[root@ansible ~]# ansible 192.168.188.129 -m group -a 'name=mysql state=absent'
192.168.188.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "mysql",
"state": "absent"
}
[root@ansible ~]# ansible 192.168.188.129 -m shell -a 'grep mysql /etc/group'
192.168.188.129 | FAILED | rc=1 >>
non-zero return code
user模块用于管理受控机的用户帐号。
user模块请求的是useradd, userdel, usermod三个指令,用法也与这些命令基本一致
user常用参数说明:
user模块参数 | 说明 |
---|---|
name | 创建、删除或修改用户名。name=tom |
uid | 设置用户的UID。uid=4000 |
group | 设置用户的主组。group=jerry |
shell | 设置用户登陆时的shell |
create_home | 创建用户时会为该用户创建一个家目录,默认会创建,除非设为no |
state | state=present为添加,state=absent为删除。如果状态与所指定的不一致则更改为指定的状态 |
password | 设置用户的密码 |
expires | 该账户的过期时间。expires=1666886400(意为2022-10-28过期,从1970-1-1到2022-10-28有多少秒) |
#在受控机上添加一个系统用户,用户名为mysql,uid为306,设置其shell为/sbin/nologin,无家目录
[root@ansible ~]# ansible 192.168.188.129 -m user -a 'name=mysql uid=306 system=yes create_home=no shell=/sbin/nologin state=present'
192.168.188.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-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.188.129 -m shell -a 'grep mysql /etc/passwd' 192.168.188.129 | CHANGED | rc=0 >>
mysql:x:306:306::/home/mysql:/sbin/nologin
[root@ansible ~]# ansible 192.168.188.129 -m shell -a 'ls /home'
192.168.188.129 | CHANGED | rc=0 >>
runtime
#修改mysql用户的uid为366
[root@ansible ~]# ansible 192.168.188.129 -m user -a 'name=mysql uid=366'
192.168.188.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-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.188.129 -m shell -a 'grep mysql /etc/passwd'
192.168.188.129 | CHANGED | rc=0 >>
mysql:x:366:306::/home/mysql:/sbin/nologin
#删除受控机上的mysql用户
[root@ansible ~]# ansible 192.168.188.129 -m user -a 'name=mysql state=absent'
192.168.188.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"force": false,
"name": "mysql",
"remove": false,
"state": "absent"
}
[root@ansible ~]# ansible 192.168.188.129 -m shell -a 'grep mysql /etc/passwd'
192.168.188.129 | FAILED | rc=1 >>
non-zero return code
service模块用于管理受控机上的服务。
#查看受控机上的vsftpd服务是否启动
[root@ansible ~]# ansible 192.168.188.129 -m shell -a 'systemctl is-active vsftpd'
192.168.188.129 | FAILED | rc=3 >>
inactivenon-zero return code
#启动受控机上的vsftpd服务
[root@ansible ~]# ansible 192.168.188.129 -m service -a 'name=vsftpd state=started'
192.168.188.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "vsftpd",
"state": "started",
"status": {
......
}
#查看受控机上的vsftpd服务是否启动
[root@ansible ~]# ansible 192.168.188.129 -m shell -a 'systemctl is-active vsftpd'
192.168.188.129 | CHANGED | rc=0 >>
active
#查看受控机上的vsftpd服务是否开机自动启动
[root@ansible ~]# ansible 192.168.188.129 -m shell -a 'systemctl is-enabled vsftpd'
192.168.188.129 | FAILED | rc=1 >>
disablednon-zero return code
#设置受控机上的vsftpd服务开机自动启动
[root@ansible ~]# ansible 192.168.188.129 -m service -a 'name=vsftpd enabled=yes'
192.168.188.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "vsftpd",
......
}
#查看受控机上的vsftpd服务是否开机自动启动
[root@ansible ~]# ansible 192.168.188.129 -m shell -a 'systemctl is-enabled vsftpd'
192.168.188.129 | CHANGED | rc=0 >>
enabled
#停止受控机上的vsftpd服务
[root@ansible ~]# ansible 192.168.188.129 -m service -a 'name=vsftpd state=stopped'
192.168.188.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "vsftpd",
"state": "stopped",
......
}
[root@ansible ~]# ansible 192.168.188.129 -m shell -a 'systemctl is-active vsftpd'
192.168.188.129 | FAILED | rc=3 >>
inactivenon-zero return code
[root@ansible ~]# ansible 192.168.188.129 -m shell -a 'ss -antl'
192.168.188.129 | 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 [::]:*
类似于systemctl命令,用于设置服务的开启、停止、重启、开机自启,与service模块很像
systemd模块的常用参数:
systemd模块参数 | 说明 |
---|---|
name | 指定服务名 |
enabled | =yes 将服务开机自启,=no 不开机自启 |
state | state=started 开启服务、=stopped 关闭服务、=restarted 重启服务=reloaded 重载服务 只有某些服务需要用到(ssh、nfs、nginx) |
daemon-reload | =yes 重新加载对应服务管理配置文件。默认为no |
#将vsftpd服务启动并设为开机自启
[root@ansible ~]# ansible 192.168.188.129 -m systemd -a 'name=vsftpd enabled=yes state=started'
192.168.188.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "vsftpd",
"state": "started",
......
}
[root@ansible ~]# ansible 192.168.188.129 -m systemd -a 'name=firewalld enabled=no state=stopped'
......
该模块用于管理文件、目录、符号链接,包括修改文件的权限,修改文件所有者,创建文件,删除文件等。
详情请参考:官方文档之file模块
file常用参数说明:
file模块参数 | 说明 |
---|---|
path | 指定要操作的文件/目录的路径 |
state | 该选项有多个取值,包括directory, touch,file, link,hard, absent.各个取值的含义如下:取值为directory时,如果目录不存在,创建目录;取值为touch时,如果文件不存在,创建一个新文件,如果文件或目录已存在,更新其最后访问时间和修改时间;取值为file时,即使文件不存在也不会被创建;取值为link时,创建软链接;取值为hard时,创建硬链接;取值为absent时,删除目录,文件或链接。 |
src | 指定源文件,一般用于软链接link |
mode | 定义文件/目录的权限。有两种方式定义:mode=755、u+rwx、u=rw,g=r,o=r |
owner | 创建文件并设置属主,前提是该用户存在 |
group | 创建文件并设置属组,前提是该用户组存在 |
recurse | 递归设置文件属性,只对目录有效 |
#在webserver组中的全部主机上创建一个名为test的文件
[root@ansible project]# ansible webserver -m file -a 'path=/tmp/test state=touch'
192.168.188.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dest": "/tmp/test",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 382,
"state": "file",
"uid": 0
}
#可以看到都有test文件了
[root@ansible project]# ansible webserver -a 'ls -l /tmp/test' -o
192.168.188.129 | CHANGED | rc=0 | (stdout) -rw-r--r-- 1 root root 382 Oct 23 19:38 /tmp/test
#在/tmp/haha目录下创建hehe目录,haha目录事先不存在,会递归一并创建出来
[root@ansible project]# ansible 192.168.188.129 -m file -a 'path=/tmp/haha/hehe state=directory'
192.168.188.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/haha/hehe",
"size": 6,
"state": "directory",
"uid": 0
}
#查看是否有了/tmp/haha/hehe目录
[root@ansible project]# ansible 192.168.188.129 -a 'ls -ld /tmp/haha/hehe'
192.168.188.129 | CHANGED | rc=0 >>
drwxr-xr-x 2 root root 6 Oct 23 19:41 /tmp/haha/hehe
#创建软链接
[root@ansible project]# ansible 192.168.188.129 -m file -a 'src=/tmp/test path=/tete_link state=link'
192.168.188.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dest": "/tete_link",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 9,
"src": "/tmp/test",
"state": "link",
"uid": 0
}
#查看是否创建成功
[root@ansible project]# ansible 192.168.188.129 -a 'ls -l /tete_link'
192.168.188.129 | CHANGED | rc=0 >>
lrwxrwxrwx 1 root root 9 Oct 23 19:43 /tete_link -> /tmp/test
#在node2主机的/opt目录下创建一个名为jojo的文件,属主为tom,属组为jerry,权限分别为属主读写、属组读、其他人读
#注意!tom用户与jerry用户需事先存在,用user与group模块练习一下如何创建
[root@ansible project]# ansible 192.168.188.129 -m file -a 'path=/opt/jojo owner=tom group=jerry mode=644 state=touch'
192.168.188.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dest": "/opt/jojo",
"gid": 2002,
"group": "jerry",
"mode": "0644",
"owner": "tom",
"size": 0,
"state": "file",
"uid": 2001
}
[root@ansible project]# ansible 192.168.188.129 -a 'ls -l /opt/jojo' -o
192.168.188.129 | CHANGED | rc=0 | (stdout) -rw-r--r-- 1 tom jerry 0 Oct 23 19:47 /opt/jojo
#删除目录或文件,删除也是递归的
[root@ansible project]# ansible 192.168.188.129 -m file -a 'path=/tmp/haha state=absent'
192.168.188.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"path": "/tmp/haha",
"state": "absent"
}