【自动化部署】Ansible 基础命令行模块

文章目录

  • [Ansible 命令行模块](#Ansible 命令行模块)
    • [1. 查看所有已安装的模块](#1. 查看所有已安装的模块)
    • [2. command 模块](#2. command 模块)
    • [3. shell 模块](#3. shell 模块)
    • [4. cron 模块](#4. cron 模块)
    • [5. user 模块](#5. user 模块)
    • [6. group 模块](#6. group 模块)
    • [7. copy 模块](#7. copy 模块)
    • [8. file 模块](#8. file 模块)
    • [9. hostname 模块](#9. hostname 模块)
    • [10. ping 模块](#10. ping 模块)
    • [11. yum/apt 模块](#11. yum/apt 模块)
    • [12. service/systemd 模块](#12. service/systemd 模块)
    • [13. script 模块](#13. script 模块)
    • [14. mount 模块](#14. mount 模块)
    • [15. archive 模块](#15. archive 模块)
    • [16. unarchive 模块](#16. unarchive 模块)
    • [17. replace 模块](#17. replace 模块)
    • [18. setup 模块](#18. setup 模块)

Ansible 命令行模块

1. 查看所有已安装的模块

bash 复制代码
ansible-doc -l

2. command 模块

  • 在远程主机执行命令,不支持管道、重定向等 shell 特性。
bash 复制代码
ansible <host> -m command -a 'command_to_run'
# 示例
ansible 192.168.80.11 -m command -a 'date'
ansible webservers -m command -a 'date'
ansible all -m command -a 'date'
# 使用chdir参数
ansible all -m command -a "chdir=/home ls ./"

3. shell 模块

  • 在远程主机执行命令,支持管道符号等功能。
bash 复制代码
ansible <host> -m shell -a 'shell_command'
# 示例
ansible dbservers -m shell -a 'echo 123456 | passwd --stdin test'
ansible dbservers -m shell -a 'echo $(ifconfig ens33 | awk "NR==2 {print $2}") | cut -d " " -f2'

4. cron 模块

  • 在远程主机定义任务计划。
bash 复制代码
ansible <host> -m cron -a 'minute="*/1" job="command_to_run" name="cron_name" [user="user_name"]'
# 示例
ansible webservers -m cron -a 'minute="*/1" job="/bin/echo helloworld" name="test crontab"'
ansible webservers -m cron -a 'name="test crontab" state=absent'

5. user 模块

  • 用户管理模块。
bash 复制代码
ansible <host> -m user -a 'name="user_name" [state=present|absent] [system=yes|no] [uid=UID] [group=GROUP] [groups=GROUPS] [shell=SHELL] [create_home=yes|no] [password=ENCRYPTED_PASSWORD] [remove=yes|no]'
# 示例
ansible dbservers -m user -a 'name="test01"'
ansible dbservers -m user -a 'name="test01" state=absent'

6. group 模块

  • 用户组管理模块。
bash 复制代码
ansible <host> -m group -a 'name=GROUP_NAME [gid=GID] [system=yes|no]'
# 示例
ansible dbservers -m group -a 'name=mysql gid=306 system=yes'

7. copy 模块

  • 用于复制文件到远程主机。
bash 复制代码
ansible <host> -m copy -a 'src=SOURCE_FILE dest=DESTINATION_PATH [owner=OWNER] [group=GROUP] [mode=MODE] [content="CONTENT"]'
# 示例
ansible dbservers -m copy -a 'src=/etc/fstab dest=/opt/fstab.bak owner=root mode=640'
ansible dbservers -m copy -a 'content="helloworld" dest=/opt/hello.txt'

8. file 模块

  • 设置文件属性。
bash 复制代码
ansible <host> -m file -a 'path=PATH [owner=OWNER] [group=GROUP] [mode=MODE] [state=touch|absent|link] [src=SOURCE_LINK]'
# 示例
ansible dbservers -m file -a 'owner=test01 group=mysql mode=644 path=/opt/fstab.bak'
ansible dbservers -m file -a 'path=/opt/fstab.link src=/opt/fstab.bak state=link'
ansible dbservers -m file -a "path=/opt/abc.txt state=touch"
ansible dbservers -m file -a "path=/opt/abc.txt state=absent"

9. hostname 模块

  • 管理远程主机上的主机名。
bash 复制代码
ansible <host> -m hostname -a "name=NEW_HOSTNAME"
# 示例
ansible dbservers -m hostname -a "name=mysql01"

10. ping 模块

  • 检测远程主机的连通性。
bash 复制代码
ansible <host> -m ping
# 示例
ansible all -m ping

11. yum/apt 模块

  • 在远程主机上安装与卸载软件包。
bash 复制代码
ansible <host> -m yum -a 'name=PACKAGE_NAME [state=present|absent]'
# 示例
ansible webservers -m yum -a 'name=httpd'
ansible webservers -m yum -a 'name=httpd state=absent'

12. service/systemd 模块

  • 管理远程主机上的服务运行状态。
bash 复制代码
ansible <host> -m service -a 'name=SERVICE_NAME [state=started|stopped|restarted|reloaded] [enabled=yes|no]'
# 示例
ansible webservers -m service -a 'enabled=true name=httpd state=started'

13. script 模块

  • 远程批量运行本地的 shell 脚本。
bash 复制代码
ansible <host> -m script -a 'SCRIPT_PATH'
# 示例
ansible webservers -m script -a 'test.sh'

14. mount 模块

  • 挂载文件系统。
bash 复制代码
ansible <host> -m mount -a 'src=SOURCE_PATH path=MOUNT_POINT fstype=FILESYSTEM_TYPE [state=mounted|absent|unmounted] [opts=OPTIONS]'
# 示例
ansible dbservers -m mount -a 'src=/dev/sr0 path=/mnt state=mounted fstype=iso9660'

15. archive 模块

  • 打包压缩。
bash 复制代码
ansible <host> -m archive -a 'path=SOURCE_PATH dest=DESTINATION_PATH format=FORMAT [remove=yes|no]'
# 示例
ansible dbservers -m archive -a "path=/etc/yum.repos.d/ dest=/opt/repo.zip format=zip"

16. unarchive 模块

  • 解包解压缩。
bash 复制代码
ansible <host> -m unarchive -a 'src=SOURCE_PATH dest=DESTINATION_PATH [copy=yes|no] [remote_src=yes|no]'
# 示例
ansible dbservers -m unarchive -a "src=/opt/abc.tar.gz dest=/root copy=yes"
ansible dbservers -m unarchive -a "src=/opt/123.tar.gz dest=/root copy=no"

17. replace 模块

  • 基于正则进行匹配和替换。
bash 复制代码
ansible <host> -m replace -a 'path=FILE_PATH regexp=REGEXP replace=REPLACEMENT [backup=yes|no] [before="BEFORE_TEXT"] [after="AFTER_TEXT"]'
# 示例
ansible dbservers -m replace -a "path=/opt/test.txt regexp='33' replace='cc'"

18. setup 模块

  • 收集被管理节点信息。
bash 复制代码
ansible <host> -m setup [-a 'filter=FILTER']
# 示例
ansible webservers -m setup
ansible dbservers -m setup -a 'filter=*ipv4'
相关推荐
正在走向自律6 小时前
Linux 服务器部署 DeepSeek 大模型完全指南(2026 版)
linux·运维·服务器·deepseek·部署deepseek·安装ollama·配置api服务
mosaicwang6 小时前
使用XSHELL的ssh隧道/代理连接服务器
运维·xshell·ssh隧道
PiaoKe___6 小时前
云手机技术解析:从 ARM 虚拟化到 Python 自动化实战
python·智能手机·自动化
JoyCong19986 小时前
ToDesk鸿蒙PC端又焕新!远控更稳、操作更准,跨端办公如本地
运维·华为·智能手机·harmonyos·远程工作
KaiwuDB6 小时前
企业级数据库数据保护:KaiwuDB KaiwuDB V3.3.0 发布:S3 冷热分层存储与主备集群能力增强实战解析模场景备份恢复方案解析
数据库·时序数据库·kaiwudb·发版·多模时序数据库·冷热分层存储·主备集群
KeyAction66667 小时前
从西班牙高铁追尾看安全系统设计:当违规指令成为输入,防护逻辑如何兜底
运维·服务器·安全
夜雪一千7 小时前
Linux 管道、重定向命令介绍
linux·运维·chrome
DataY7 小时前
MySQL CDC 直接写进 Doris 汇总模型:同步即汇总,明细不用搬两遍
数据库
琳达爱分享7 小时前
客服改了地址,发货任务把它改了回去:一次没人报错的覆盖
数据库
江华森7 小时前
数据中心网络技术基础 —— 从原理到实操
linux·运维