文章目录
Ansible中的内置模块非常的多,一般使用ansible-doc来获取模块相关帮助; 可以直接查看doc帮助文档尾部 的example例子 仿照应用
ansible默认模块为command模块
1. yum模块
使用:

应用:
[devops@server1 ansible]$ ansible test -m yum -a "name=httpd state=present" ##对test组中主机yum安装httpd


安装完成:

2. service模块
使用:

应用:
[devops@server1 ansible]$ ansible test -m yum -a "name=httpd state=present" ##开启httpd服务


服务配置成功

3. firewalld模块
使用

应用
[devops@server1 ansible]$ ansible test -m firewalld -a "service=http permanent=yes state=enabled" ##火墙允许http服务 并且开机自启

4. copy模块
使用
- 需要注意的是src源文件需要在执行目录下存在
- 一般在config服务时,使用copy模块来修改服务相应配置文件

应用
[devops@server1 ansible]$ ansible test -m copy -a "src=hosts dest=/mnt/ owner=devops group=devops mode=0644 " ##拷贝文件 并且设置其所有者及所有组为devops 文件权限为644

5.template模块
使用:
template模块与copy模块作用相同都是用于copy文件的,区别在于copy模块copy的src源文件都是静态文件,不存在变量;当年我们需要copy的文件中出现变量,我们就要用到template模块;并且src文件是以".j2"结尾的模板文件



6 uri模块
使用

应用
[devops@server1 ansible]$ ansible test -m uri -a "url=http://172.25.3.2 return_content=yes" ##访问http://172.25.3.2 并回送内容

6. file模块
使用


应用
[devops@server1 ansible]$ ansible test -m file -a "path=/mnt/foo.conf state=touch mode=u=rw,g=r,o=r" ## 设定权限创建文件并


7. user模块
使用

应用
[devops@server1 ansible]$ ansible test -m user -a "name=johnd comment='John Doe' uid=1040 " ##建立用户johnd uid为1040


8. mysql_db模块
使用
在使用数据库时需要下载MySQL-python

应用
[devops@server1 ansible]$ ansible test -m yum -a "name=mariadb-server,MySQL-python state=present" ##创建数据库前先安装数据库
[devops@server1 ansible]$ ansible test -m service -a "name=mariadb state=started"
##开启数据库服务
[devops@server1 ansible]$ ansible test -m mysql_db -a "name=test state=present" ##创建数据库


9. mysql_user模块
使用

应用
[devops@server1 ~]$ ansible test -m mysql_user -a "name=bob password=12345 priv='*.*:ALL,GRANT'" ##创建数据库用户bob 密码为12345 并授权


本文深入解析Ansible中九种常用模块的应用与操作,包括yum、service、firewalld、copy、template、uri、file、user、mysql_db和mysql_user模块。通过具体示例,展示如何利用这些模块进行软件包管理、服务控制、防火墙配置、文件复制与模板渲染、用户管理及数据库操作。
1516

被折叠的 条评论
为什么被折叠?



