ansible--常用模块

一、command 默认模块 - 执行命令

适合使用简单的命令,无法支持"<",">","|",";","&"等符号

命令格式:ansible [主机] [-m 模块] [-a args]

ansible-doc -l       #列出所有已经安装的模块   注:按q退出
ansible-doc -s yum   #-s 列出yum 模块描述信息和操作动作

在这里插入图片描述

ansible 192.168.221.30 -m command  -a 'date'   #指定ip执行date命令
ansible webservers -m command  -a 'date'       #指定分类执行date命令

在这里插入图片描述在这里插入图片描述

ansible all -m command -a 'date'               #所有hosts主机执行date命令
[root@localhost ~]# ansible all -a 'ls /'      #如果不加-m模块,则默认运行command模块

在这里插入图片描述
在这里插入图片描述

二、cron - 定时任务模块

两种状态(state):present表示添加(可以省略),absent表示移除。

nsible-doc -s cron         #查看cron模块信息

在这里插入图片描述

[root@localhost ~]# ansible mysql -m cron -a 'minute="*/1" job="/usr/bin/echo haha" name="test cron job"' #创建计划任务

在这里插入图片描述

[root@localhost ~]# ansible mysql -a 'crontab -l'    #查看计划任务

在这里插入图片描述

[root@localhost ~]# ansible mysql -m cron -a 'name="test cron job" state=absent'   #移除定时性任务

在这里插入图片描述

三、user模块 - 用户管理

user 模块请求的是useradd,userdel,usermod 三个指令

ansible-doc -s user #查看user模块信息

在这里插入图片描述

[root@localhost ~]# ansible mysql -m user -a 'name="test01"'    #创建用户test01

在这里插入图片描述

[root@ansible ~]# ansible mysql -m command -a 'tail /etc/passwd'  #查看用户信息

在这里插入图片描述

ansible mysql -m user -a 'name="test01" state=absent' #删除用户test01

在这里插入图片描述

四、group - 用户(组)模块

group 模块请求的是 groupadd、groupdel、groupmod 三个指令。

ansible-doc -s group   #查看group模块信息

在这里插入图片描述

[root@localhost ~]# ansible mysql -m group -a 'name=mysql gid=306 system=yes'
##name=mysql  组名是mysql   gid=306:gid号是306   system=yes: 为系统组

在这里插入图片描述

[root@localhost ~]# ansible mysql -a 'tail /etc/group'   #查看组信息的最后十行,是否添加成功mysql

在这里插入图片描述

[root@localhost ~]# ansible mysql -m user -a 'name=test01 uid=306 group=mysql system=yes'
#向mysql组中添加系统用户test01,用户uid号为306

在这里插入图片描述

[root@ansible ~]# ansible mysql -a 'tail /etc/passwd'
[root@ansible ~]# ansible mysql -a 'id test01'

在这里插入图片描述

五、copy - 复制模块

[root@ansible ~]# ansible-doc -s copy   #查看copy模块信息

在这里插入图片描述

[root@localhost ~]# ansible mysql -m copy -a 'src=/etc/fstab dest=/opt/fstab.bk owner=root mode=640'
##使用copy模块,讲源地址(src)文件复制到目的地址(dest)文件下,属主为root,权限为640

[root@localhost ~]# ansible mysql -a 'ls -l /opt'                  #使用该命令查看mysql中/opt目录下的文件
[root@ansible ~]# ansible mysql -a 'cat /opt/fstab.back'           #查看具体内容

在这里插入图片描述在这里插入图片描述

[root@ansible ~]# ansible mysql -m copy -a 'content="hello world!" dest=/opt/fstab.back'    #将hello world 写入/opt/fstab.back

[root@ansible ~]# ansible mysql -a 'cat /opt/fstab.back'

在这里插入图片描述

六、file模块 - 指定文件属性

[root@ansible ~]# ansible-doc -s file
[root@ansible ~]# ansible mysql -m user -a 'name=mysql system=yes'
[root@ansible ~]# ansible mysql -m group -a 'name=mysql system=yes'

在这里插入图片描述

[root@ansible ~]# ansible mysql -m file -a 'owner=mysql group=mysql mode=644 path=/opt/fstab.back'    #修改文件的属主属组

在这里插入图片描述

[root@ansible ~]# ansible mysql -m file -a 'path=/opt/fstab.link src=/opt/fstab.back state=link'   
#设置/opt/fstab.link为/opt/fstab.back

在这里插入图片描述

[root@ansible ~]# ansible mysql -m file -a 'path=/opt/fstab.back state=absent'    #删除一个文件

在这里插入图片描述

[root@ansible ~]# ansible mysql -m file -a 'path=/opt/test state=touch'    #创建一个文件

在这里插入图片描述

[root@ansible ~]# ansible webserver -m file -a 'path=/opt/temp state=directory mode=755'   #创建目录

在这里插入图片描述

七、ping模块 - 测试连通状态

[root@localhost ~]# ansible all -m ping    #检测被控制端是否能ping通
192.168.221.20 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.221.30 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

八、yum 模块 - 安装/卸载软件包

yum:使用yum软件包管理器安装,升级,降级,删除和列出软件包和组

[root@ansible ~]# ansible-doc -s yum  #查看yum模块信息

在这里插入图片描述

[root@ansible ~]# ansible mysql -m yum -a 'name=zsh' #yum安装zsh
[root@node2 opt]# rpm -q zsh

在这里插入图片描述在另一节点,查看是否安装完成
在这里插入图片描述

[root@ansible ~]# ansible mysql -m yum -a 'name=zsh state=absent'   #卸载zsh

[root@node2 opt]# rpm -q zsh

在这里插入图片描述在另一个节点,查看是否卸载
在这里插入图片描述

九、service模块 - 管理服务状态

service:用于管理服务运行状态

[root@ansible ~]# ansible-doc -s service  #查看模块信息

在这里插入图片描述

[root@node1 ~]# yum install -y httpd
[root@ansible ~]# ansible webserver -m service -a 'enabled=true name=httpd state=started'   #启动httpd服务
[root@ansible ~]# ansible webserver -a 'systemctl status httpd'              #查看web服务器httpd运行状态
[root@ansible ~]# ansible all -m service -a 'name=firewalld state=stopped'   #关闭防火墙

[root@node1 ~]# systemctl status httpd  #查看是否开启

在这里插入图片描述在这里插入图片描述在另一节点,查看是否服务是否开启
在这里插入图片描述

本机的防火墙处于关闭,选择开启防火墙的命令
在这里插入图片描述

十、shell 模块 - 免交互

shell 模块可以使用"<",">","|",";","&"等符号特殊符号,使用方法与 command 模块一致。

[root@ansible ~]# ansible-doc -s shell   #查看模块信息
[root@ansible ~]# ansible mysql -m shell -a 'echo abc123 | passwd --stdin mysql'    #创建用户使用无交互模式给用户设置密码

在这里插入图片描述

十一、script 模块 - 执行脚本

[root@ansible ~]# ansible-doc -s script   #查看script信息

在这里插入图片描述

[root@ansible ~]# vim test.sh   #在ansible管理端创建脚本
#/bin/bash
echo "hello ansible from scriipt">/opt script.txt

[root@ansible ~]# chmod +x test.sh
[root@ansible ~]# ansible mysql -m script -a 'test.sh'

[root@node2 opt]# cat /opt/script.txt

在这里插入图片描述
在另一个节点,查看是否成功
在这里插入图片描述

十二、setup模块

[root@ansible ~]# ansible-doc -s setup   #查看模块信息
[root@ansible ~]# ansible mysql-m setup  

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值