ansible常用模块汇总

艺帆风顺 发布于 2025-04-07 20 次阅读


本篇文章将讲一下ansible中模块相关问题。

1. 什么是模块

1.1 定义

模块是 Ansible 的核心执行单元,指代一个小型程序,专门用于执行某一类操作,如管理文件、安装软件包、配置服务等。

1.2 特点

  • 模块在远程主机上执行任务并返回结果。
  • 提供大量内置模块,同时支持自定义模块开发。
  • 易用性高,模块参数采用关键字-值的形式,清晰直观。

1.3 模块执行原理

  • 主控机通过 SSH 将模块代码和参数传输到被控主机。
  • 模块在被控主机上执行指定操作。
  • 返回操作结果给主控机。

2 查看模块相关命令

在ansible中,有非常多的模块,你不需要记住每个模块的详细用法,需要使用时可以通过ansible-doc来查看手册。

# 列出所有可用模块ansible-doc -l# 查看某个模块的详细信息ansible-doc 模块名# -s参数可以更人性化得输出帮助ansible-doc -s 模块名

2. 常用模块分类

2.1 文件管理模块

模块功能描述
file管理文件和目录属性(权限、所有权、符号链接等)。
copy将本地文件复制到远程主机。
fetch从远程主机下载文件到主控机。
template使用 Jinja2 模板动态生成文件,并部署到远程主机。
unarchive解压远程主机或从主控机传输的压缩文件

创建目录并设置权限

- name: 创建目录并设置权限  file:    path: /var/www/app    state: directory    owner: www    group: www    mode: '0755'

复制文件到目标主机

- name: 复制文件到目标主机  copy:    src: /path/to/local/file.conf  # 源主机文件路径    dest: /etc/myapp/file.conf  # 目标文件路径    owner: root  # 属主    group: root  # 属组    mode: '0644'

使用模板动态生成配置文件

- name: Deploy configuration using a template  template:    src: templates/nginx.conf.j2    dest: /etc/nginx/nginx.conf

2.2 软件包管理模块

模块功能描述
yum在基于 RedHat 的系统上安装、更新和删除软件包。
apt在基于 Debian 的系统上安装、更新和删除软件包。
dnf用于支持 DNF 包管理的系统(如 CentOS 8)
package跨平台的软件包管理模块,会自动选择合适的管理工具。

使用 yum 模块安装 Nginx

- name: Install Nginx using yum  yum:    name: nginx    state: present

升级所有软件包

- name: Upgrade all packages using apt  apt:    upgrade: dist

2.3 系统用户管理模块

模块功能描述
user创建、修改和删除系统用户
group创建、修改和删除用户组

创建一个用户并加入指定组

- name: Create a deploy user  user:    name: deploy    state: present    groups: sudo

创建用户组

- name: Create a developers group  group:    name: developers    state: present

2.4 服务管理模块

模块功能描述
service启动、停止、重启和启用服务
systemd管理 systemd 服务的启动、状态等

启动并启用服务

- name: Start and enable Nginx  service:    name: nginx    state: started    enabled: true

重启服务

- name: Restart Nginx  systemd:    name: nginx    state: restarted

3 使用临时命令方式调用模块

1. 测试主机连通性

ansible all -m ping

2. 创建目录

ansible all -m file -a "path=/data state=directory mode=0755"

3. 删除文件

ansible all -m file -a "path=/data/file.txt state=absent"

4. 复制文件到目标节点

ansible all -m copy -a "src=/path/to/local/file dest=/path/to/remote/file owner=root group=root mode=0644"

5. 创建定时任务

ansible all -m cron -a "name='backup job' minute=0 hour=1 job='/usr/local/bin/backup.sh'"

6. 启动服务

ansible all -m service -a "name=httpd state=started"

7. 停止服务

ansible all -m service -a "name=httpd state=stopped"

8. 重启服务

ansible all -m systemd -a "name=nginx state=restarted"

9. 安装软件包(CentOS/RHEL)

ansible all -m yum -a "name=httpd state=present"

10. 删除软件包(CentOS/RHEL)

ansible all -m yum -a "name=httpd state=absent"

11. 安装软件包(Ubuntu/Debian)

ansible all -m apt -a "name=nginx state=present update_cache=yes"

12. 删除软件包(Ubuntu/Debian)

ansible all -m apt -a "name=nginx state=absent"

13. 检查系统运行时间

ansible all -m command -a "uptime"

14. 创建文件并写入内容

ansible all -m shell -a "echo 'Hello World' > /tmp/hello.txt"

15. 运行本地脚本文件

ansible all -m script -a "/path/to/local/script.sh"

16. 开放防火墙端口

ansible all -m firewalld -a "port=80/tcp permanent=true state=enabled"

17. 添加 iptables 规则

ansible all -m iptables -a "chain=INPUT protocol=tcp destination_port=22 jump=ACCEPT"

18. 禁用 SELinux

ansible all -m selinux -a "state=disabled"

19. 创建 MySQL 用户

ansible all -m mysql_user -a "name=myuser password=mypassword priv=*.*:ALL state=present"

20. 打印变量

ansible all -m debug -a "msg='The value of my_var is {{ my_var }}'"

如有帮助,请点个赞和“在看”!如有不足,敬请指出!感谢你的关注与支持。

路虽远,行则将至!

事虽难,做则必成!共勉!



往期精彩文章