常用的 Ansible 模块

以下是一些常用的 Ansible 模块:

  • ping 模块:用于检测目标主机是否可达。

  • file 模块:可以管理文件和目录,如创建、删除、修改权限等。

  • copy 模块:用于将本地文件复制到远程主机。

  • service 模块:管理服务的状态,如启动、停止、重启等。

  • yum 模块:如前面提到的,用于管理 yum 包的安装、卸载、更新等。

  • user 模块:用于创建、删除用户等操作。

  • group 模块:管理用户组。

Ansible 的 yum 模块是一个用于管理 Red Hat 系列 Linux 发行版软件包的重要模块。通过 yum 模块,可以在目标主机上安装、更新或删除软件包,以及配置软件包的存储库。

使用 yum 模块可以大大简化系统管理员的工作,节省时间和精力。在 Ansible 中使用 yum 模块非常简单,只需在 Playbook 中指定 yum 模块及相应的参数,Ansible 就会自动在目标主机上执行相应的操作。

以下是一个使用 yum 模块的简单示例:

  • name: Install nginx using yum module

hosts: webserver

tasks:

  • name: Install nginx

yum:

name: nginx

state: present

在这个示例中,通过指定 name 参数为 nginx , state 参数为 present ,Ansible 会自动在目标主机上执行安装操作。

好的,以下是 Ansible 中一些常用模块的实例:

  1. ping 模块:
  • 测试与目标主机的网络连接:

yaml 复制

  • name: Test connection with ping module

hosts: target_host

tasks:

  • name: Ping target host

ping:

  1. file 模块:
  • 创建目录:

yaml 复制

  • name: Create directory with file module

hosts: target_host

tasks:

  • name: Create cdh directory

file:

path=/opt/cdh

state=directory

mode=0755

  • 设置文件权限:

yaml 复制

  • name: Set file permissions with file module

hosts: target_host

tasks:

  • name: Chmod bin file

file:

dest=/opt/bigdata/jdk/bin

mode=0755

recurse=yes

  • 创建软链接:

yaml 复制

  • name: Create symlink with file module

hosts: target_host

tasks:

  • name: Link files

file:

src=/opt/bigdata/jdk/bin

dest=/usr/bin/java

state=link

  1. copy 模块:
  • 复制文件到目标主机:

yaml 复制

  • name: Copy file with copy module

hosts: target_host

tasks:

copy:

src=/path/to/ttt.sh

dest=/opt/ttt.sh

  1. service 模块:
  • 启动服务:

yaml 复制

  • name: Start service with service module

hosts: target_host

tasks:

  • name: Start nginx service

service:

name: nginx

state: started

  • 停止服务:

yaml 复制

  • name: Stop service with service module

hosts: target_host

tasks:

  • name: Stop nginx service

service:

name: nginx

state: stopped

  • 重启服务:

yaml 复制

  • name: Restart service with service module

hosts: target_host

tasks:

  • name: Restart nginx service

service:

name: nginx

state: restarted

这只是 Ansible 中一些常用模块的简单实例,Ansible 还有许多其他模块,可根据具体需求进行使用。

相关推荐
chairon3 天前
Ansible:playbook的高级用法
linux·运维·服务器·ansible·apache
树下一少年3 天前
ansible+docker+docker-compose快速部署4节点高可用minio集群
docker·容器·ansible·docker-compose·minio集群
千航@abc4 天前
Ansible 实战:Roles,运维的 “魔法函数”
ansible·playbook·roles·角色·模版·剧本
一个高效工作的家伙4 天前
ansible可视化自动化平台-semaphore
ansible
树下一少年4 天前
通过ansible+docker-compose快速安装一主两从redis+三sentinel
redis·docker·ansible·sentinel·docker-compose
独隅5 天前
针对Ansible执行脚本时报错“可执行文件格式错误”,以下是详细的解决步骤和示例
运维·开发语言·ansible·lua·lua5.4
大小科圣6 天前
Ansible playbook
ansible
大小科圣6 天前
ansible条件判断及循环
ansible
狂奔solar7 天前
ansible-playbook 写arm版达梦7数据库的一键安装脚本
ansible
晴天Y288 天前
ansible角色 role
ansible