侧边栏壁纸
  • 累计撰写 73 篇文章
  • 累计创建 23 个标签
  • 累计收到 2 条评论

目 录CONTENT

文章目录

【Ansible】Ansible模块

Administrator
2025-06-26 / 0 评论 / 0 点赞 / 17 阅读 / 37343 字 / 正在检测是否收录...

官网内置模块:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/index.html#plugins-in-ansible-builtin

一、file 模块

file模块是ansible中用于管理文件和目录的核心模块,它可以创建文件、目录、符号链接、设置权限和属性等。

常用参数

参数

说明

示例

path

文件/目录路径(别名:dest, name

path: /etc/foo.conf

state

状态:file, directory, link, hard, touch, absent

state: directory

mode

权限(如 0644)

mode: '0644'

owner

文件所有者

owner: root

group

文件所属组

group: www-data

recurse

递归设置目录权限

recurse: yes

src

链接的源文件(当state=linkhard时)

src: /etc/foo.conf

force

强制创建链接(当目标存在时)

force: yes

  • touch:创建文件、更新时间戳。

  • file:修改文件属性

示例:

1. 创建目录

ansible all -m file -a "path=/opt/dir state=directory"

2. 创建文件

ansible all -m file -a "path=/opt/dir/file1.txt state=touch"

3. 创建符号链接

ansible all -m file -a "src=/etc/hosts dest=/opt/dir/hosts_link state=link"

4. 删除文件或目录

### 删除目录
ansible all -m file -a "path=/opt/dir state=absent"

### 删除文件
ansible all -m file -a "path=/opt/dir/file1.txt state=absent"

5. 修改文件属性

### 修改文件权限
ansible all -m file -a "path=/opt/dir/file1.txt mode=0640 owner=root group=root"

### 递归修改目录权限
ansible all -m file -a "path=/opt/dir mode=0640 owner=root group=root recurse=yes"

二、user 模块

Ansible 的 user 模块用于管理系统用户账户,包括创建、修改和删除用户,以及管理用户属性如密码、组、家目录等。

常用参数

参数

说明

示例

name

用户名(必需)

name: testuser

state

用户状态:present(存在)/absent(不存在)

state: present

uid

用户 UID

uid: 1001

group

用户主组

group: developers

groups

用户附加组列表

groups: wheel,devops

home

用户家目录路径

home: /home/testuser

shell

用户默认 shell

shell: /bin/bash

comment

用户描述信息

comment: "Test User"

password

用户密码(加密后的)

password: $6$salt$hash

generate_ssh_key

是否生成 SSH 密钥

generate_ssh_key: yes

ssh_key_type

SSH 密钥类型

ssh_key_type: rsa

ssh_key_file

SSH 密钥文件路径

ssh_key_file: .ssh/id_rsa

system

是否为系统用户

system: yes

system: yes

是否创建家目录

create_home: no

1. 创建用户

ansible all -m user -a "name=testuser state=present shell=/sbin/nologin group=test groups=test1 home=/home/test create_home=yes"

2. 删除用户

ansible all -m user -a "name=testuser state=absent remove=yes"

三、group 模块

group 模块是 Ansible 中用于管理系统用户组的核心模块,可以创建、修改和删除用户组。

主要参数

参数

说明

示例值

name

组名(必需)

developers

state

组状态:present/absent

present

gid

指定组 GID

1001

system

是否为系统组

yes

1. 创建基本用户组

ansible all -m group -a "name=developers"

2. 创建指定 GID 的组

ansible webservers -m group -a "name=deploy gid=1042"

3. 创建系统组

ansible db_servers -m group -a "name=dbadmin system=yes"

4. 删除组

ansible old_servers -m group -a "name=legacy state=absent"

5. 修改组 GID

ansible app_servers -m group -a "name=appusers gid=1500"

四、yum 模块

yum 模块是 Ansible 中用于管理 基于 RPM 的 Linux 系统中软件包的核心模块。

主要参数

参数

说明

示例值

name

包名/包组名/URL

httpd, @development

state

状态:present/latest/absent

latest

enablerepo

临时启用仓库

epel

disablerepo

临时禁用仓库

updates

exclude

排除的包

kernel*

update_cache

更新元数据缓存

yes

list

列出包(不执行操作)

httpd

security

仅安全更新

yes

download_only

仅下载不安装

yes

1. 安装单个软件包

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

2. 安装最新版本

ansible all -m yum -a "name=nginx state=latest"

3. 安装多个软件包

ansible app_servers -m yum -a "name=vim-enhanced,git,tmux state=present"

4. 删除软件包

ansible old_servers -m yum -a "name=telnet state=absent"

5. 使用特定仓库安装

ansible all -m yum -a "name=http enablerepo=epel state=present"

6. 更新所有软件包

ansible production -m yum -a "name=* state=latest"

7. 仅安全更新

ansible critical -m yum -a "security=yes state=latest"

8. 安装包组

ansible dev_hosts -m yum -a "name='@development' state=present"

9. 下载但不安装

ansible all -m yum -a "name=ansible download_only=yes"

10. 从URL安装RPM

ansible all -m yum -a "name=https://example.com/packages/custom.rpm state=present" -b

11. 排除特定包更新

ansible all -m yum -a "name=* state=latest exclude=kernel*"

12. 清理YUM缓存

ansible all -m yum -a "clean=all"

13. 列出可用更新

ansible all -m yum -a "list=updates"

14. 检查包是否安装

ansible all -m yum -a "list=httpd"

15. 验证命令

# 检查包是否安装
ansible all -m shell -a "rpm -q httpd" -b

# 检查版本
ansible all -m shell -a "nginx -v" -b

五、copy 模块

copy 模块是 Ansible 中用于文件传输的核心模块,可以将本地文件复制到远程主机,或直接在远程主机上创建新文件。

主要参数

参数

说明

示例值

src

源文件路径(本地)

/tmp/file.conf

dest

目标路径(远程)

/etc/file.conf

content

直接写入的内容

"Hello World"

owner

文件所有者

root

group

文件所属组

wheel

mode

文件权限

0644

backup

是否备份原文件

yes

force

是否强制覆盖

no

validate

更新前验证命令

"/usr/sbin/apachectl -t %s"

1. 基本文件复制

ansible webservers -m copy -a "src=/tmp/app.conf dest=/etc/app.conf"

2. 设置文件权限和所有者

ansible all -m copy -a "src=/tmp/script.sh dest=/usr/local/bin/script.sh owner=root group=root mode=0755"

3. 直接创建文件内容

ansible db_servers -m copy -a "content='DB_HOST=127.0.0.1' dest=/etc/db.conf"

4. 复制并备份原文件

ansible production -m copy -a "src=/tmp/nginx.conf dest=/etc/nginx/nginx.conf backup=yes"

5. 在单个主机上复制文件

ansible hostA -m copy -a "src=/path/to/source dest=/path/to/dest remote_src=yes"

6. 验证命令

# 检查文件是否存在
ansible all -m shell -a "ls -l /etc/app.conf" -b

# 检查文件内容
ansible all -m shell -a "cat /etc/motd" -b

# 检查备份文件
ansible all -m shell -a "ls -l /etc/nginx/nginx.conf.*" -b

六、systmd 模块

systemd 模块是 Ansible 中用于管理系统服务的核心模块,专门用于基于 systemd 的 Linux 系统。

主要参数

参数

说明

示例值

name

服务名称(必需)

nginx

state

服务状态:started/stopped/restarted/reloaded

started

enabled

开机自启:yes/no

yes

1. 启动服务

ansible webservers -m systemd -a "name=nginx state=started"

2. 停止服务

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

3. 重启服务

ansible app_servers -m systemd -a "name=mysqld state=restarted"

4. 重载服务(不重启)

ansible load_balancers -m systemd -a "name=haproxy state=reloaded"

5. 启用开机自启

ansible db_servers -m systemd -a "name=postgresql enabled=yes"

6. 禁用开机自启

ansible legacy_servers -m systemd -a "name=tomcat enabled=no"

7. 检查服务状态

ansible all -m systemd -a "name=sshd"

8. 验证命令

# 检查服务状态
ansible all -m shell -a "systemctl is-active nginx"

# 检查是否启用
ansible all -m shell -a "systemctl is-enabled nginx"

# 查看详细状态
ansible all -m shell -a "systemctl status nginx --no-pager"

七、mount 模块

mount 模块是 Ansible 中用于管理文件系统挂载的核心模块,可以永久性配置 /etc/fstab 中的挂载项并控制当前挂载状态。

主要参数

参数

说明

示例值

path

挂载点路径(必需)

/mnt/data

src

要挂载的设备/资源

/dev/sdb1nfs-server:/export

fstype

文件系统类型

ext4, nfs, xfs

state

状态:present/absent/mounted/unmounted

mounted

opts

挂载选项

rw,noatime

dump

dump 备份标志

0

passno

fsck 检查顺序

2

boot

是否在启动时挂载

yes

  • mounted会挂载并写入 fstab

  • unmoted 会卸载但不改变 fstab

  • present 只写入 fstab 不执行挂载

  • absent 删除 fstab,也取消挂载

1. 挂载本地设备并永久生效

ansible all -m mount -a "path=/data src=/dev/sdb1 fstype=ext4 state=mounted opts=rw,noatime" -b

2. 挂载 NFS 共享

ansible app_servers -m mount -a "path=/mnt/nfs src=nfs01:/data/share fstype=nfs opts=rw,hard,intr state=mounted" -b

3. 卸载文件系统(保留 fstab 记录)

ansible all -m mount -a "path=/mnt/temp state=unmounted" -b

4. 完全移除挂载配置

ansible old_servers -m mount -a "path=/mnt/legacy state=absent" -b

5. 临时挂载(不写入 fstab)

ansible test_servers -m mount -a "path=/tmp/test src=/dev/sdc1 fstype=xfs state=mounted boot=no" -b

6. 配置交换分区

ansible all -m mount -a "path=none src=/dev/sdb2 fstype=swap state=present" -b

7. 修改现有挂载选项

ansible webservers -m mount -a "path=/var/www src=/dev/sdd1 fstype=ext4 opts=rw,noexec state=present" -b

8. 检查挂载状态

ansible all -m mount -a "path=/mnt/data state=present" -b

9. 挂载 CIFS/SMB 共享

ansible clients -m mount -a "path=/mnt/smb src=//fileserver/share fstype=cifs opts=username=user,password=pass,uid=1000 state=mounted" -b

10. 验证命令

# 检查挂载点是否存在
ansible all -m shell -a "ls -ld /mnt/data"

# 检查当前挂载情况
ansible all -m shell -a "mount | grep /mnt/data"

# 检查 fstab 配置
ansible all -m shell -a "grep /mnt/data /etc/fstab"

# 检查挂载选项
ansible all -m shell -a "findmnt -n -o OPTIONS /mnt/data"

八、cron 模块

name 的作用是判断这个任务是否已经存在,以及更新覆盖。

cron 模块是 Ansible 中用于管理系统定时任务(cron jobs)的核心模块,可以创建、修改或删除 crontab 条目。

主要参数

参数

说明

示例值

name

任务描述/注释(标识用)

"备份数据库"

user

任务所属用户

"root"

job

要执行的命令

"/path/to/script.sh"

minute

分钟(0-59)

"*/5"、"0,30"

hour

小时(0-23)

"3"、"*/2"

day

日期(1-31)

"*"、"15"

month

月份(1-12)

"*"、"1,4,7,10"

weekday

星期几(0-6,0=周日)

"*"、"1-5"

special_time

预设时间(annually/monthly/weekly/daily/hourly/reboot)

"daily"

state

状态:present/absent

"present"

disabled

是否禁用任务

"yes"

environment

环境变量

"PATH=/usr/bin:/bin"

backup

修改前是否备份原crontab

"yes"

1. 创建基本 cron 任务

ansible all -m cron -a 'name="日常备份" user=root job="/opt/scripts/backup.sh" minute=0 hour=2' -b

2. 使用预设时间(每天执行)

ansible webservers -m cron -a 'name="清理日志" job="/opt/scripts/clean_logs.sh" special_time=daily' -b

3. 创建复杂时间任务创建复杂时间任务

ansible db_servers -m cron -a 'name="数据库维护" minute=30 hour=3 day=15 month="*/2" weekday=1-5 job="/opt/db/maintenance.sh"' -b

4. 为特定用户创建任务

ansible app_servers -m cron -a 'user=appuser name="应用监控" job="/home/appuser/monitor.sh" minute="*/10"' -b

5. 禁用 cron 任务

ansible all -m cron -a 'name="日常备份" disabled=yes' -b

6. 删除 cron 任务

ansible old_servers -m cron -a 'name="旧任务" state=absent' -b

7. 带环境变量的任务

ansible all -m cron -a 'name="带环境变量的任务" job="/opt/scripts/env_test.sh" hour=1 environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"' -b

8. 检查 cron 任务是否存在

ansible all -m cron -a 'name="日常备份" job="/opt/scripts/backup.sh" minute=0 hour=2' -b

9. 验证命令

# 查看root用户的cron任务
ansible all -m shell -a 'crontab -l' -b

# 查看特定用户的cron任务
ansible all -m shell -a 'crontab -u appuser -l' -b

# 检查特定任务是否存在
ansible all -m shell -a 'crontab -l | grep "日常备份"' -b

0
博主关闭了所有页面的评论