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

相关推荐
Sweety丶╮7945 小时前
【Ansible】实施 Ansible Playbook知识点
服务器·云原生·ansible
YC运维1 天前
Ansible题目全解析与答案
java·算法·ansible
--运维实习生--2 天前
自动化运维之ansible
运维·自动化·ansible
神秘人X7073 天前
Ansible 角色使用指南
ansible·角色
维尔切3 天前
自动化运维-ansible中对于大项目的管理
运维·自动化·ansible
Gss7773 天前
ansible变量+管理机密
linux·运维·ansible
误入运维泥潭3 天前
Ansible自动化运维:从入门到精通
linux·ansible·module
半梦半醒*3 天前
ansible阶段练习题
linux·运维·自动化·ansible·负载均衡·运维开发
Wezzer3 天前
jenkins使用ansible单节点lnmp
运维·ansible·jenkins
deeper_wind4 天前
Jenkins主机中安装ansible部署lnmp论坛(小白的”升级打怪“成长之路)
linux·ansible·jenkins