常用的 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 还有许多其他模块,可根据具体需求进行使用。

相关推荐
云攀登者-望正茂8 小时前
使用 Ansible 在 Windows 服务器上安装 SSL 证书
ansible
tianyuanwo2 天前
Ansible自动化运维全解析:从设计哲学到实战演进
运维·自动化·ansible
哆啦A梦15882 天前
在golang中如何将已安装的依赖降级处理,比如:将 go-ansible/[email protected] 更换为 go-ansible/@v1.1.7
开发语言·golang·ansible
lifeng43213 天前
在 CentOS 上将 Ansible 项目推送到 GitHub 的完整指南
centos·github·ansible
运维成长记8 天前
ansible-playbook 进阶 接上一章内容
linux·服务器·ansible
半桶水专家8 天前
Ansible 配置Playbook文件格式、关键字和语法详解
ansible
遇见火星10 天前
Ansible模块——Ansible配置文件!
linux·git·ansible
安顾里12 天前
Ansible安装
linux·运维·自动化·ansible
爱莉希雅&&&14 天前
运维Linux之Ansible详解学习(更新中)
linux·运维·ansible
筑梦之路19 天前
centos 9 Kickstart + Ansible自动化部署 —— 筑梦之路
centos·自动化·ansible