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

相关推荐
风清再凯4 天前
自动化工具ansible,以及playbook剧本
运维·自动化·ansible
IT乌鸦坐飞机4 天前
ansible部署数据库服务随机启动并创建用户和设置用户有完全权限
数据库·ansible·centos7
遇见火星18 天前
如何使用Ansible一键部署MinIO集群?
ansible
粥周粥18 天前
ANSIBLE
ansible
码农101号18 天前
Linux中ansible模块补充和playbook讲解
linux·运维·ansible
码农101号18 天前
Linux的Ansible软件基础使用讲解和ssh远程连接
ansible
烟雨书信19 天前
ANSIBLE运维自动化管理端部署
运维·自动化·ansible
碎碎-li19 天前
ANSIBLE(运维自动化)
运维·自动化·ansible
@donshu@22 天前
Linux运维-ansible-python开发-获取inventroy信息
linux·运维·ansible
Kendra91925 天前
Ansible
ansible