Ansible之常用模块
文章目录
前言
模块(也被称为 “task plugins” 或 “library plugins”)是在 Ansible 中实际在执行任务的,Ansible中通过模块实现批量管理。它们可以在Playbook 任务中被执行的,你也可以仅仅在命令行通过Ad-Hoc(临时命令)来运行它们。
Ansible中的模块是幂等的。也就是说,多次执行相同的操作,只有第一次会起作用。但raw、command、shell这三模块是非幂等。
模块类型与帮助信息
Ansible的模块非常多,如果以模块的功能进行分类,可以大致分为:
- 云模块
- 命令模块
- 数据库模块
- 文件模块
- 资产模块
- 消息模块
- 监控模块
- 网络模块
- 通知模块
- 包管理模块
- 源码控制模块
- 系统模块
- 单元模块
- web设施模块
- windows模块等
学习或工作使用ansible中查阅官方文档中的帮助信息的能力是不可或缺的!
查阅官方文档有两种方式:
其一是在浏览器中在线查阅,个人更推荐这种方式,图形化界面相较于命令行界面来说视觉效果好的不是一星半点儿
其二是在命令行中使用ansible-doc 模块名
进行查阅,这种阅读体验不是很好,在不能访问网络的情况下只能这么干了
#进到ansible的帮助文档界面中想快速查找到案例需键入/EXAMPLES
[root@control ~]# ansible-doc 模块名
#选项参数:
-l:列出可用模块
-s:显示指定模块的playbook前段
[root@control ~]# ansible-doc ping #查看指定的模块帮助用法
[root@control ~]# ansible-doc -l #列出所有模块
ping模块
ping模块用于检查指定节点机器是否连通,用法很简单,不涉及参数,主机如果在线,则回复pong
[root@control project]# ansible all -m ping
192.168.92.128 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
远程命令模块
raw、command、shell模块的区别
- shell模块调用的/bin/sh指令执行
- command模块不是调用的shell的指令,所以没有bash的环境变量
- raw很多地方和shell类似,更多的地方建议使用shell和command模块。但是如果是使用老版本python,需要用到raw,又或者是客户端是路由器,因为没有安装python模块,那就需要使用raw模块了
command模块
command模块用于在远程主机上执行命令,ansible默认就是使用command模块。
command模块有一个缺陷就是不能使用管道符和重定向功能,也不支持特殊符号,比如*
#查看受控主机的/tmp目录内容
[root@control project]# ansible 192.168.92.128 -a 'ls /tmp'
192.168.92.128 | CHANGED | rc=0 >>
ansible_command_payload_03yb51_c
vmware-root_913-4013723377
vmware-root_922-2722632355
#在受管主机的/tmp目录下创建一个名为tets的普通文件
#可以看到执行完命令后有警告信息,这是因有专门的file模块来干这件事,
#记住,如果有专门的模块干专门的事儿,那么我们就不应该用这种方式
[root@control project]# ansible 192.168.92.128 -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.92.128 | CHANGED | rc=0 >>
#验证受控主机/tmp目录下是否存在test的普通文件
[root@control project]# ansible 192.168.92.128 -a "ls /tmp"
192.168.92.128 | CHANGED | rc=0 >>
ansible_command_payload__xo00cw5
ssh-B1wiJcCKeY
test
vmware-root_913-4013723377
vmware-root_922-2722632355
#在受控主机执行date命令,-o参数是让命令的状态信息与执行结果在同一行显示
[root@control project]# ansible 192.168.92.128 -a "date" -o
192.168.92.128 | CHANGED | rc=0 | (stdout) Sat Oct 22 16:32:36 CST 2022
#执行受控主机的脚本
[root@control project]# ansible 192.168.92.128 -m shell -a '/bin/bash /scripts/test.sh'
192.168.92.128 | CHANGED | rc=0 >>
1
2
3
4
5
#在不交互的情况下执行top命令。因ansible执行任务时无法交互
[root@control project]# ansible 192.168.92.128 -a 'top -n 1'
192.168.92.128 | CHANGED | rc=0 >>
top - 16:49:45 up 1:11, 2 users, load average: 0.00, 0.00, 0.00
Tasks: 159 total, 1 running, 158 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 3.2 sy, 0.0 ni, 96.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 1790.0 total, 1338.0 free, 205.8 used, 246.2 buff/cache
MiB Swap: 2080.0 total, 2080.0 free, 0.0 used. 1432.7 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 172192 10256 7936 S 0.0 0.6 0:01.30 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
3 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_gp
4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_par+
..........................
shell模块
shell模块用于在受控机上执行受控机上的脚本,可直接在受控机上执行命令, 亦支持特殊符号、管道和重定向。
#仅显示受控主机的ens32网卡的IP。注意特殊字符要使用\转义符。
[root@control project]# ansible webserver -m shell -a "ip a show ens32 | awk -F'[/ ]*' 'NR==3{print \$3}'"
192.168.92.128 | CHANGED | rc=0 >>
192.168.92.128
192.168.92.134 | CHANGED | rc=0 >>
192.168.92.134
raw模块
raw模块用于在远程主机上执行命令,其支持特殊符号、管道符与重定向。
#验证raw模块是否支持重定向
[root@control project]# ansible 192.168.92.128 -m raw -a 'echo "hello world" > /tmp/test'
192.168.92.128 | CHANGED | rc=0 >>
Shared connection to 192.168.92.128 closed.
#验证raw模块是否支持管道
[root@control project]# ansible 192.168.92.128 -m raw -a 'cat /tmp/test | grep -o hello'
192.168.92.128 | CHANGED | rc=0 >>
hello
Shared connection to 192.168.92.128 closed.
script模块
script模块用于在受控机上执行主控机上的脚本
#编写一个脚本
[root@control project]# mkdir scripts
[root@control project]# vim scripts/hello.sh
[root@control project]# cat scripts/hello.sh
#!/bin/bash
hostname
hostname -I
w
date +%F
#列出webserver组的所有主机
[root@control project]# ansible webserver --list-hosts
hosts (2):
192.168.92.134
192.168.92.128
#用ansible控制节点的脚本在webserver组的所有主机上执行
[root@control project]# ansible webserver -m script -a "/opt/project/scripts/hello.sh"
192.168.92.128 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.92.128 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.92.128 closed."
],
"stdout": "node03\r\n192.168.92.128 \r\n 17:38:43 up 2:00, 2 users, load average: 0.02, 0.01, 0.00\r\nUSER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT\r\nroot pts/0 192.168.92.1 16:23 36:54 0.06s 0.06s -bash\r\nroot pts/1 192.168.92.135 17:38 0.00s 0.00s 0.00s w\r\n2022-10-22\r\n",
"stdout_lines": [ #执行结果段
"node03",
"192.168.92.128 ",
" 17:38:43 up 2:00, 2 users, load average: 0.02, 0.01, 0.00",
"USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT",
"root pts/0 192.168.92.1 16:23 36:54 0.06s 0.06s -bash",
"root pts/1 192.168.92.135 17:38 0.00s 0.00s 0.00s w",
"2022-10-22"
]
}
192.168.92.134 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.92.134 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.92.134 closed."
],
"stdout": "node02\r\n192.168.92.134 \r\n 17:38:44 up 2:00, 1 user, load average: 0.50, 0.13, 0.04\r\nUSER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT\r\nroot pts/0 192.168.92.135 17:38 0.00s 0.00s 0.00s /bin/bash /root/.ansible/tmp/ansible-tmp-1666431524.1420517-8354-45080305976936/hello.sh\r\n2022-10-22\r\n",
"stdout_lines": [
"node02",
"192.168.92.134 ",
" 17:38:44 up 2:00, 1 user, load average: 0.50, 0.13, 0.04",
"USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT",
"root pts/0 192.168.92.135 17:38 0.00s 0.00s 0.00s /bin/bash /root/.ansible/tmp/ansible-tmp-1666431524.1420517-8354-45080305976936/hello.sh",
"2022-10-22"
]
}
用户管理模块
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有多少秒) |
#本次示例以node2和node3主机做演示操作
[root@control project]# cat inventory
#192.168.92.130
[webserver]
node2
node3
#先查询这两主机上是否存在tom用户。可以看到node3没有,node2有
[root@control project]# ansible webserver -a 'id tom'
node3 | FAILED | rc=1 >>
id: ‘tom’: no such usernon-zero return code
node2 | CHANGED | rc=0 >>
uid=5500(tom) gid=5500(tom) groups=5500(tom)
#设置webserver组中的全部主机的tom用户的uid为4000
[root@control project]# ansible webserver -m user -a 'name=tom uid=4000 state=present'
node3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 4000,
"home": "/home/tom",
"name": "tom",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 4000
}
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"append": false,
"changed": true,
"comment": "",
"group": 5500,
"home": "/home/tom",
"move_home": false,
"name": "tom",
"shell": "/bin/bash",
"state": "present",
"uid": 4000
}
#可以看到node2和node3都有了tom用户,并且uid都为400。
#细心的小伙伴有没有发现?前面查询的node2主机上是存在tom用户的并且uid为5500
#由此可见,ansible会按照我们吩咐的任务不择手段的完成,只要是合理的。
[root@control project]# ansible webserver -oa 'id tom'
node3 | CHANGED | rc=0 | (stdout) uid=4000(tom) gid=4000(tom) groups=4000(tom)
node2 | CHANGED | rc=0 | (stdout) uid=4000(tom) gid=5500(tom) groups=5500(tom)
#试试卸载,写上用户名然后把状态设为absent即可
[root@control project]# ansible webserver -m user -a 'name=tom state=absent'
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"force": false,
"name": "tom",
"remove": false,
"state": "absent"
}
node3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"force": false,
"name": "tom",
"remove": false,
"state": "absent"
}
#可以看到确实没有tom用户了
[root@control project]# ansible webserver -a 'id tom'
node3 | FAILED | rc=1 >>
id: ‘tom’: no such usernon-zero return code
node2 | FAILED | rc=1 >>
id: ‘tom’: no such usernon-zero return code
group模块
group模块请求的是groupadd,groupdel,groupmod三个指令,用法也与这些命令基本一致
group常用参数说明:
group模块参数 | 说明 |
---|---|
name | 设置组名 |
gid | 设置gid |
state | state=present添加组,state=absent删除组 |
system | 设置创建的组为系统组,默认为no |
#在node2主机上添加一个名为jey的组,组id为3300
[root@control project]# ansible node2 -m group -a 'name=jey gid=3300 state=present'
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 3300,
"name": "jey",
"state": "present",
"system": false
}
#查看该组是否存在
[root@control project]# ansible node2 -m shell -o -a 'cat /etc/group | grep 'jey''
node2 | CHANGED | rc=0 | (stdout) jey:x:3300:
#让node2主机上名为jey的组消失
[root@control project]# ansible node2 -m group -a 'name=jey state=absent'
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "jey",
"state": "absent"
}
#非0返回码就意味着没有查找名为jey的组
[root@control project]# ansible node2 -m shell -a 'cat /etc/group | grep jey'
node2 | FAILED | rc=1 >>
non-zero return codew
文件管理模块
file模块
该模块用于管理文件、目录、符号链接,包括修改文件的权限,修改文件所有者,创建文件,删除文件等。
详情请参考:官方文档之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@control project]# ansible webserver -m file -a 'path=/tmp/test state=touch'
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dest": "/tmp/test",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 0,
"state": "file",
"uid": 0
}
node3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dest": "/tmp/test",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 12,
"state": "file",
"uid": 0
}
#可以看到都有test文件了
[root@control project]# ansible webserver -a 'ls -l /tmp/test' -o
node2 | CHANGED | rc=0 | (stdout) -rw-r--r-- 1 root root 0 Oct 23 13:51 /tmp/test
node3 | CHANGED | rc=0 | (stdout) -rw-r--r-- 1 root root 12 Oct 23 13:51 /tmp/test
#在/tmp/haha目录下创建hehe目录,haha目录事先不存在,会递归一并创建出来。ansible会尽其所能满足你的要求
[root@control project]# ansible node2 -m file -a 'path=/tmp/haha/hehe state=directory'
node2 | 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@control project]# ansible node2 -a 'ls -ld /tmp/haha/hehe'
node2 | CHANGED | rc=0 >>
drwxr-xr-x 2 root root 6 Oct 23 13:58 /tmp/haha/hehe
#创建软链接
[root@control project]# ansible node2 -m file -a 'src=/tmp/test path=/tete_link state=link'
node2 | 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@control project]# ansible node2 -a 'ls -l /tete_link'
node2 | CHANGED | rc=0 >>
lrwxrwxrwx 1 root root 9 Oct 23 14:12 /tete_link -> /tmp/test
#在node2主机的/opt目录下创建一个名为jojo的文件,属主为tom,属组为jerry,权限分别为属主读写、属组读、其他人读
#注意!tom用户与jerry用户需事先存在,快用user与group模块练习一下如何创建吧~
[root@control project]# ansible node2 -m file -a 'path=/opt/jojo owner=tom group=jerry mode=644 state=touch'
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dest": "/opt/jojo",
"gid": 1001,
"group": "jerry",
"mode": "0644",
"owner": "tom",
"size": 0,
"state": "file",
"uid": 1000
}
[root@control project]# ansible node2 -a 'ls -l /opt/jojo' -o
node2 | CHANGED | rc=0 | (stdout) -rw-r--r-- 1 tom jerry 0 Oct 23 14:23 /opt/jojo
#删除目录或文件,删除也是递归的
[root@control project]# ansible node2 -m file -a 'path=/tmp/haha state=absent'
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"path": "/tmp/haha",
"state": "absent"
}
copy模块
copy模块用来将主控节点的文件或目录拷贝到远程服务器上,类似于Linux下的scp命令。但是,copy模块比scp命令更强大,在拷贝文件到远程服务器的同时,也可以设置文件在远程服务器的权限和所有者。
copy模块常用参数:
copy模块参数 | 说明 |
---|---|
src | 要复制到远程主机的源文件路径,可以是绝对路径,也可以是相对路径,如果路径是一个目录,将递归复制。在这种情况下,如果路径使用"/“结尾,则只复制目录里的内容,如果没有使用”/"来结尾,则将包含目录在内整个内容全部复制,类似于rsync。 |
dest | 远程主机的目标路径,必须是一个绝对路径,如果源文件是一个目录,那么,dest指向的也必须是一个目录。 |
force | 默认为yes,表示目标主机存在该文件会进行强制覆盖;设为no,则表示只有当目标主机不存在该文件时才会进行传输 |
backup | 默认为no,如果为yes,那么当目标主机的目标路径中已经存在同名文件时,在覆盖之前将原文件进行备份 |
mode | 设置copy到目标主机上的文件或目录的权限 |
owner | 设置copy到目标主机上的文件或目录的属主 |
group | 设置copy到目标主机上的文件或目录的属组 |
#需求:将控制节点的/etc/下的hosts文件copy至受控节点的/etc/hosts。
#这条命令的影响:势必会覆盖掉node2主机/etc目录下原hosts文件,由于了用了backup会将原文件备份,备份格式是以时间戳结尾的
[root@control project]# ansible node2 -m copy -a 'src=/etc/hosts dest=/etc/hosts backup=yes'
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup_file": "/etc/hosts.9879.2022-10-23@14:51:21~",
"changed": true,
"checksum": "d31e86f803745af296cbad6cc0e3f3ddb13c0ef7",
"dest": "/etc/hosts",
"gid": 0,
"group": "root",
"md5sum": "2792aa65ad51018c0473d9f18bbe01d1",
"mode": "0644",
"owner": "root",
"size": 228,
"src": "/root/.ansible/tmp/ansible-tmp-1666507880.5533988-10290-146156329080716/source",
"state": "file",
"uid": 0
}
#可以看到一个hosts带时间戳的文件名,那就是node2主机上的原hosts文件
[root@control project]# ansible node2 -m shell -a 'ls /etc | grep hosts'
node2 | CHANGED | rc=0 >>
hosts
hosts.9879.2022-10-23@14:51:21~
template模块
template模块用于生成一个模板文件,并可将其传输至远程主机上。
本篇文章只介绍基本作用,后续会出一篇template模块的专题文章。
想更多的了解该模块请参考:官方文档之teamplate模块
template常用参数说明:
template模块参数 | 说明 |
---|---|
src | 要复制到远程主机的源模板文件路径,可以是绝对路径,也可以是相对路径 |
dest | 远程主机的目标路径,必须是一个绝对路径 |
mode | 设置文件拷贝到目标主机后的权限 |
owner | 设置文件拷贝到目标主机后的属主 |
group | 设置文件拷贝到目标主机后的属组 |
force | 默认为yes,表示目标主机存在该文件会进行强制覆盖;设为no,则表示只有当目标主机不存在该文件时才会进行传输 |
backup | 默认为no,如果为yes,那么当目标主机的目标路径中已经存在同名文件时,在覆盖之前将原文件进行备份 |
#将控制节点的yum源仓库文件传至node2主机
[root@control project]# ansible node2 -m template -a 'src=/etc/yum.repos.d/CentOS-Base.repo dest=/etc/yum.repos.d/CentOS-Base.repo'
node2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"checksum": "8bbf30b2d80c3b97292ca7b32f33ef494269a5b8",
"dest": "/etc/yum.repos.d/CentOS-Base.repo",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"path": "/etc/yum.repos.d/CentOS-Base.repo",
"size": 1653,
"state": "file",
"uid": 0
}
#可以看到对方有了
[root@control project]# ansible node2 -a 'ls /etc/yum.repos.d'
node2 | CHANGED | rc=0 >>
CentOS-Base.repo
包管理模块
yum模块
yum模块用于在RHEL/Centos系统上管理软件。
yum模块常用参数:
yum模块参数 | 说明 |
---|---|
name | 指定软件包名 |
state | latest、installed、present(安装软件,默认为安装可不指定) removed、absent(卸载软件) |
updata_cache | 更新yum仓库缓存。默认为no,实际应用建议设为yes开启 |
#在webserver组中的全部主机安装httpd与tree,一次安装多个软件用,作分隔符
[root@control project]# ansible webserver -m yum -a 'name=httpd,tree'
#会卡在这里,别急,这是正在安装软件中,具体安装多久取决于受控主机的网速与性能
node2 | 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: tree-1.7.0-15.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: mod_http2-1.15.7-3.module_el8.4.0+778+c970deab.x86_64",
"Installed: apr-util-openssl-1.6.1-6.el8.x86_64"
]
}
node3 | 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: tree-1.7.0-15.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: mod_http2-1.15.7-3.module_el8.4.0+778+c970deab.x86_64",
"Installed: apr-util-openssl-1.6.1-6.el8.x86_64"
]
}
#暂且无视这警告提示。可以看到受控节点安装了httpd与tree
[root@control project]# ansible webserver -m shell -a 'rpm -qa | egrep "httpd|tree"'
[WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'. If you need to use
command because yum, dnf or zypper 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.
node3 | CHANGED | rc=0 >>
tree-1.7.0-15.el8.x86_64
httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch
centos-logos-httpd-85.8-2.el8.noarch
httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64
httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64
node2 | CHANGED | rc=0 >>
centos-logos-httpd-85.8-2.el8.noarch
httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64
tree-1.7.0-15.el8.x86_64
httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch
httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64
#卸载webserver组中的全部主机的tree软件
[root@control project]# ansible webserver -m yum -a 'name=tree state=absent'
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Removed: tree-1.7.0-15.el8.x86_64"
]
}
node3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Removed: tree-1.7.0-15.el8.x86_64"
]
}
#可以看到已经过滤不出来tree的软件包了
[root@control project]# ansible webserver -m shell -a 'rpm -qa | egrep tree'
[WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'. If you need to use
command because yum, dnf or zypper 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.
node2 | FAILED | rc=1 >>
non-zero return code
node3 | FAILED | rc=1 >>
non-zero return code
yum_repository模块
yum_repository模块用于给受控节点编辑yum源仓库文件
yum仓库模块参数 | 说明 |
---|---|
name | yun源文件中的名字[epel] |
description | yum源注释,对应配置文件中的’name’ |
baseurl | yum源中baseurl链接地址 |
enable | 是否启用该yum源,用yes/no设置 |
gpgcheck | 是否检查合法性,用yes/no设置 |
file | 指定yum源文件名,自动添加.repo后缀 |
#可以看到编辑yum源文件前,我把受控节点的旧yum源文件清理了
[root@control project]# ansible all -a 'ls /etc/yum.repos.d/'
node3 | CHANGED | rc=0 >>
node2 | CHANGED | rc=0 >>
#编辑一个base源
[root@control project]# ansible all -m yum_repository -a 'file=base name="BaseOS" description="This is test" baseurl="http://mirrors.aliyun.com/centos-vault/8.5.2111/BaseOS/$basearch/os/" gpgcheck=no enabled=yes'
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"repo": "BaseOS",
"state": "present"
}
node3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"repo": "BaseOS",
"state": "present"
}
#在之前的yum源文件的base源基础上追加AppStream源
[root@control project]# ansible all -m yum_repository -a 'file=base name="AppStream" description="This is AppStream" baseurl="http://mirrors.aliyun.com/centos-vault/8.5.2111/AppStream/$basearch/os/" gpgcheck=no enabled=yes'
node3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"repo": "AppStream",
"state": "present"
}
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"repo": "AppStream",
"state": "present"
}
#查看受控节点的yum源文件,这里注意我上面写的文件名是base,但yum_repository模块会自行添加.repo后缀
[root@control project]# ansible all -a 'cat /etc/yum.repos.d/base.repo'
node2 | CHANGED | rc=0 >>
[BaseOS]
baseurl = http://mirrors.aliyun.com/centos-vault/8.5.2111/BaseOS/$basearch/os/
enabled = 1
gpgcheck = 0
name = This is test
[AppStream]
baseurl = http://mirrors.aliyun.com/centos-vault/8.5.2111/AppStream/$basearch/os/
enabled = 1
gpgcheck = 0
name = This is AppStream
.......另一节点配置文件内容相同,省略........
服务管理模块
systemd模块
类似于systemctl命令,用于设置服务的开启、停止、重启、开机自启
systemd模块的常用参数:
systemd模块参数 | 说明 |
---|---|
name | 指定服务名 |
enabled | =yes 将服务开机自启,=no 不开机自启 |
state | state=started 开启服务、=stopped 关闭服务、=restarted 重启服务 =reloaded 重载服务 只有某些服务需要用到(ssh、nfs、nginx) |
daemon-reload | =yes 重新加载对应服务管理配置文件。默认为no |
#将crond服务启动并设为开机自启
[root@control project]# ansible all -m systemd -a 'name=crond enabled=yes state=started'
node3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"enabled": true,
"name": "crond",
"state": "started",
..........内容超多,省略...........
[root@control project]# ansible all -m systemd -a 'name=firewalld enabled=no state=stopped'
..........内容超多,省略...........