本文介绍dnf,service,command/shell使用方法
一、dnf
dnf 模块用于在 Redhat 系列系统中批量安装软件,相当于到远程主机上执行 dnf -y install
命令。
dnf 模块常用参数说明:
name(必须):指定要安装的软件包名称,如 httpd、nginx、tomcat。
state(可选):指定软件包的状态,可以是以下值之一:
installed/present:确保软件包已安装,如果软件包未安装,则进行安装。
removed/absent:确保软件包未安装,如果软件包已安装,则进行卸载。
latest:确保软件包已安装到最新版本,如果软件包已安装但不是最新版本,则进行更新。
使用示例:
安装nginx服务器
bash
[root@node1 ~]# rpm -qa nginx
[root@node1 ~]#
[root@node2 ~]# rpm -qa nginx
[root@node2 ~]#
1、配置ansible
bash
[root@ansible demo1]# cat ansible.cfg
[defaults]
inventory=inventory/hosts
[root@ansible demo1]# cat inventory/hosts
[server]
node[1:2]
[root@ansible demo1]# cat site.yml
---
- name: install nginx server
host: server
tasks:
- name: install nginx
ansible.builtin.dnf:
name: nginx
state: present
[root@ansible demo1]# tree .
.
├── ansible.cfg
├── inventory
│ ├── group_vars
│ ├── hosts
│ └── host_vars
└── site.yml
3 directories, 3 files
2、执行剧本文件
bash
[root@ansible demo1]# ansible-playbook site.yml
PLAY [install nginx server] ****************************************************************
TASK [Gathering Facts] *********************************************************************
ok: [node1]
ok: [node2]
TASK [install nginx] ***********************************************************************
changed: [node2]
changed: [node1]
PLAY RECAP *********************************************************************************
node1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@ansible demo1]#
3、验证结果
bash
[root@node1 ~]# rpm -qa nginx
nginx-1.28.3-1.el9.ngx.x86_64
[root@node2 ~]# rpm -qa nginx
nginx-1.20.1-14.el9_2.1.x86_64
二、service
service 模块用于启动、停止、重新启动或重载系统服务。
常用选项说明如下:
name(必需):要操作的服务的名称。
state(可选):指定服务的状态,可以是以下值之一:
started:确保服务处于启动状态,如果服务未启动,则启动该服务。
stopped:确保服务处于停止状态,如果服务正在运行,则停止该服务。
restarted:总是会重新启动服务。
reloaded:总是重新加载服务。
enabled:确保服务在系统启动时自动启动。
arguments(可选):用于指定启动或停止服务时的额外参数。这可以是一个字符串或列表,用于传递给服务脚本。
sleep(可选):用于在执行操作之前添加延迟,以确保服务配置已生效。单位为秒。
使用示例(基于安装好的nginx服务):
启动前面安装好的ngin服务
bash
[root@node1 ~]# systemctl status nginx.service
○ nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; preset: disabled)
Active: inactive (dead)
Docs: http://nginx.org/en/docs/
[root@node2 ~]# systemctl status nginx.service
○ nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; preset: disabled)
Active: inactive (dead)
1、修改模板文件和定义主机变更
bash
[root@client demo1]# cat ansible.cfg
[defaults]
inventory=inventory/hosts
[root@ansible demo1]# cat templates/index.j2
{{ server_name }}
[root@ansible demo1]# cat inventory/host_vars/node1.yml
server_name: node1
[root@ansible demo1]# cat inventory/host_vars/node2.yml
server_name: node2
[root@ansible demo1]# cat site.yml
---
- name: install nginx server
hosts: server
tasks:
- name: install nginx
ansible.builtin.dnf:
name: nginx
state: present
- name: create index.html
ansible.builtin.template:
src: templates/index.j2
dest: /usr/share/nginx/html/index.html
- name: start nginx:
ansible.builtin.service:
name: nginx
state: started
[root@ansible demo1]# tree .
.
├── ansible.cfg
├── inventory
│ ├── group_vars
│ ├── hosts
│ └── host_vars
│ ├── node1.yml
│ └── node2.yml
├── site.yml
└── templates
└── index.j2
2、执行剧本文件
bash
[root@node1 ~]# systemctl status nginx
○ nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; preset: disabled)
Active: inactive (dead)
Docs: http://nginx.org/en/docs/
[root@node1 ~]# systemctl status nginx.service
○ nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; preset: disabled)
Active: inactive (dead)
Docs: http://nginx.org/en/docs/
[root@node1 ~]# systemctl status nginx.service
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; preset: disabled)
Active: active (running) since Tue 2026-04-14 22:11:37 CST; 10s ago
Docs: http://nginx.org/en/docs/
Process: 692386 ExecStart=/usr/sbin/nginx -c ${conffile} (code=exited, status=0/SUCCESS)
Main PID: 692391 (nginx)
Tasks: 3 (limit: 12044)
Memory: 3.2M
CPU: 87ms
CGroup: /system.slice/nginx.service
├─692391 "nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf"
├─692392 "nginx: worker process"
└─692393 "nginx: worker process"
Apr 14 22:11:37 node1 systemd[1]: Starting nginx - high performance web server...
Apr 14 22:11:37 node1 systemd[1]: nginx.service: Can't open PID file /run/nginx.pid (yet?) >
Apr 14 22:11:37 node1 systemd[1]: Started nginx - high performance web server.
[root@node1 ~]# curl localhost
node1
[root@node2 ~]# systemctl status nginx.service
○ nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; preset: disabled)
Active: inactive (dead)
[root@node2 ~]# mkdir templates
[root@node2 ~]# rm -rf templates/
[root@node2 ~]# systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; preset: disabled)
Active: active (running) since Tue 2026-04-14 22:11:34 CST; 37s ago
Process: 677440 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCE>
Process: 677441 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 677442 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 677444 (nginx)
Tasks: 3 (limit: 12044)
Memory: 2.9M
CPU: 175ms
CGroup: /system.slice/nginx.service
├─677444 "nginx: master process /usr/sbin/nginx"
├─677446 "nginx: worker process"
└─677447 "nginx: worker process"
Apr 14 22:11:34 node2 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Apr 14 22:11:34 node2 nginx[677441]: nginx: the configuration file /etc/nginx/nginx.conf sy>
Apr 14 22:11:34 node2 nginx[677441]: nginx: configuration file /etc/nginx/nginx.conf test i>
Apr 14 22:11:34 node2 systemd[1]: Started The nginx HTTP and reverse proxy server.
[root@node2 ~]# curl localhost
node2
三、command/shell
这两个模块都是在目标主机上执行命令或命令字符串。
shell 模块用于在远程主机上执行复杂的命令,包括使用管道、重定向和其他 shell 功能。这意味着你可以像在命令行中一样运行命令,包括使用通配符、环境变量和其他 shell 特性。
command 模块用于在远程主机上执行单个命令,但不会通过 shell 运行它。这意味着不会使用shell 的特性,如通配符扩展或重定向。它更适合用于简单的命令,而不涉及 shell 的特殊功能。
由于 command 模块不会在 shell 中执行命令,因此它比 shell 模块更安全,因为不会受到shell 注入等攻击。
使用示例:
ansib server -X
-m:指定模块
-a:模块参数
-i:主机清单
-b:提权 root
-k/-K:密码登录
-u:指定用户
1、command
bash
[root@ansible demo1]# ansible server -m command -a "ls /root"
node1 | CHANGED | rc=0 >>
d1
test.conf
node2 | CHANGED | rc=0 >>
d1
f1
f2
opt
t4.txt
test
test.conf
或者
bash
---
- name: run ls root
hosts: server
tasks:
- name: ls root
ansible.builtin.command:
cmd: "ls /root"
2、shell
bash
ansible server -m shell -a "mkdir /root/a; cd /root/a; touch b; chown nobody b; chmod 400 b"
或者
bash
---
- name: shell module
hosts: server
tasks:
- name: use shell
ansible.builtin.shell: |
mkdir /root/a
cd /root/a
touch b
chown nobody b
chmod 400 b
"|" 叫 字面块运算符,其作用:保留多行格式,让你可以写多行命令 / 文本
敬请期待下一篇 ^o^(Kubernetes(K8s))