运维效率翻倍:带你认识 Ansible 最常用的 14 个模块

Ansible 是一款开源的 自动化运维工具,主要用于配置管理、应用部署、任务自动化和持续交付。Ansible 对于运维工作有多重要性,已经不需要再多言,掌握它的使用如同打开了自动化的大门。

本文将介绍Ansible在运维工作中最常用的14个模块,带你实现工作效率的翻倍。

一、基础连接与测试模块

1. ping模块

测试与目标主机的连接性:

复制
ansible all -m ping1.

示例输出:

复制
server1 | SUCCESS => { "changed": false, "ping": "pong" }1.2.3.4.
2. command模块(不支持shell特性)

在远程主机执行命令:

复制
ansible webservers -m command -a "uptime"1.

常用参数:

- `chdir`:执行前切换目录- `creates`:如果文件存在则不执行- `removes`:如果文件不存在则不执行

示例:

复制
ansible db -m command -a "mysqldump -u root -p password dbname > backup.sql chdir=/backups"1.
3. shell模块

在远程主机通过shell执行命令(支持管道、重定向等):

复制
ansible all -m shell -a "df -h | grep /dev/sda1"1.

二、文件操作模块

4. copy模块

复制本地文件到远程主机:

复制
ansible webservers -m copy -a "src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf owner=root group=root mode=0644 backup=yes"1.
5. file模块

管理文件属性或创建文件/目录/链接:

创建目录:
复制
ansible app -m file -a "path=/opt/myapp state=directory mode=0755"1.
创建软链接:
复制
ansible all -m file -a "src=/etc/nginx/nginx.conf dest=/tmp/nginx.conf state=link"1.
删除文件:
复制
ansible all -m file -a "path=/tmp/testfile state=absent"1.
6. stat模块

获取文件状态信息:

复制
ansible webservers -m stat -a "path=/etc/nginx/nginx.conf"1.

输出示例:

复制
{ "changed": false, "stat": { "exists": true, "gid": 0, "group": "root", "mode": "0644", "mtime": 1634567890.1234567, "path": "/etc/nginx/nginx.conf", "size": 1024, "uid": 0, "owner": "root" } }1.2.3.4.5.6.7.8.9.10.11.12.13.14.

三、软件包管理模块

7. yum模块 (RHEL/CentOS)

安装包:

复制
ansible centos_servers -m yum -a "name=nginx state=present"1.

更新所有包:

复制
ansible centos_servers -m yum -a "name=* state=latest"1.

删除包:

复制
ansible centos_servers -m yum -a "name=nginx state=absent"1.
8. apt模块 (Debian/Ubuntu)

安装包:

复制
ansible ubuntu_servers -m apt -a "name=nginx state=present update_cache=yes"1.

删除包:

复制
ansible ubuntu_servers -m apt -a "name=nginx state=absent"1.

四、系统服务管理

9. service模块

启动服务:

复制
ansible webservers -m service -a "name=nginx state=started enabled=yes"1.

重启服务:

复制
ansible webservers -m service -a "name=nginx state=restarted"1.

停止服务:

复制
ansible webservers -m service -a "name=nginx state=stopped"1.

五、用户与组管理

10. user模块

创建用户:

复制
ansible all -m user -a "name=testuser uid=1000 group=admin create_home=yes shell=/bin/bash"1.

删除用户:

复制
ansible all -m user -a "name=testuser state=absent remove=yes"1.
11. group模块

创建组:

复制
ansible all -m group -a "name=admin gid=1000 state=present"1.

删除组:

复制
ansible all -m group -a "name=admin state=absent"1.

六、常用高级模块

12. setup模块

收集主机系统信息:

复制
ansible all -m setup1.

过滤特定信息:

复制
ansible all -m setup -a "filter=ansible_distribution*"1.
13. cron模块

添加cron任务:

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

删除cron任务:

复制
ansible all -m cron -a "name=daily backup state=absent"1.
14. lineinfile模块

确保某行存在:

复制
ansible all -m lineinfile -a "path=/etc/ssh/sshd_config line=PermitRootLogin no regexp=^PermitRootLogin"1.

删除某行:

复制
ansible all -m lineinfile -a "path=/etc/hosts state=absent line=127.0.0.1 badhost"1.

七. 实际使用技巧

(1) 查看模块帮助文档:

复制
ansible-doc copy1.

(2) 限制执行主机:

复制
ansible webservers[0] -m ping # 只对webservers组第一个主机执行1.

(3) 并行执行控制:

复制
ansible all -m ping -f 10 # 使用10个并行进程1.

(4) 使用become提权:

复制
ansible all -m yum -a "name=nginx state=present" --become --ask-become-pass1.

(5) 调试模式:

复制
ansible all -m command -a "ls /nonexistent" --check -vvv1.

掌握这些常用模块的命令行用法,可以快速完成日常运维任务,提高工作效率。当然,对于更加复杂的任务,建议还是使用Playbook来实现更结构化的自动化管理。

阅读剩余
THE END