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

相关推荐
一切皆文件linux12 小时前
网站集群批量管理-密钥认证与Ansible模块
ansible
让美好继续发生2 天前
ansible学习
学习·ansible
有谁看见我的剑了?2 天前
ansible学习之 Facts
ansible
peanutfish2 天前
Chapter 4 RH294 RHEL Automation with Ansible
linux·ansible·yaml
henan程序媛3 天前
jenkins项目发布基础
运维·gitlab·ansible·jenkins
pyliumy4 天前
ansible 配置
大数据·ansible
运维小白。。4 天前
自动化运维工具 Ansible
运维·自动化·ansible
weixin_438197385 天前
ansible之playbook\shell\script模块远程自动安装nginx
linux·服务器·ansible
peanutfish5 天前
Chapter 5 RH294 RHEL Automation with Ansible
linux·ansible·yaml
人类群星闪耀时5 天前
自动化运维的利器:Ansible、Puppet和Chef详解
运维·自动化·ansible