Ansible 常用模块详解

目录

1、ping 模块

2、command 模块

3、shell 模块

4、copy 模块

5、file 模块

6、fetch 模块

7、cron 模块

8、yum 模块

9、service 模块

10、user 模块

11、group 模块

12、script 模块

13、setup 模块

14、get_url 模块

15、stat 模块

16、unarchive 模块


写入主机组

[web]

192.168.8.129

192.168.8.130

1、ping 模块

ping 模块可以用来进行主机连通性测试

ansible web -m ping

2、command 模块

command 模块可以直接在远程主机上执行命令,并将结果返回本主机

ansible web -m command -a 'ss -nplt'

命令模块接受命令名称,后面是空格分隔的列表参数,给定的命令将在所有选定的节点上执行

它不会通过shell进行处理,比如$HOME和操作如"<",">","|",";","&" 工作(需要使用(shell)模块实现这些功能)

注意:该命令不支持| 管道命令

常用命令

先切换到/home/ 目录,再执行“ls”命令 

ansible web -m command -a 'chdir=/home/ ls'

如果 /opt/123.txt 存在,则不执行“ls”命令

ansible web -m command -a 'creates=/opt/123.txt ls'

如果 /opt/123.txt 存在,则执行“cat /opt/123.txt”命令

ansible web -m command -a 'removes=/opt/123.txt cat /opt/123.txt'

3、shell 模块

shell 模块可以在远程主机上调用shell解释器运行命令,支持shell的各种功能,例如管道等

ansible web -m shell -a 'cat /etc/passwd | grep root'

4、copy 模块

copy 模块用于将文件复制到远程主机,同时支持给定内容生成文件和修改权限等

常用命令

复制文件

ansible web -m copy -a 'src=~/hello dest=/opt/hello'

给定内容生成文件,并制定权限

ansible web -m copy -a 'content="I am lnyxi" dest=/opt/name mode=666'

覆盖文件内容并备份

ansible web -m copy -a 'content="I am lnyunxi" backup=yes dest=/opt/name mode=666'

5、file 模块

file 模块主要用于设置文件的属性,比如创建文件、创建链接文件、删除文件等

常用命令

创建目录

ansible web -m file -a 'path=/opt/app state=directory'

创建链接文件

ansible web -m file -a 'path=/opt/test src=/opt/hello state=link'

删除文件

ansible web -m file -a 'path=/opt/test state=absent'

6、fetch 模块

fetch 模块用于从远程某主机获取(复制)文件到本地

常用命令

ansible web -m fetch -a 'src=/opt/hello dest=/root'

7、cron 模块

cron 模块适用于管理 cron 计划任务的

cron 模块的语法跟 crontab文件中的语法一致

常用命令

添加计划任务

ansible web -m cron -a 'name="ntp update" minute=* job="/sbin/ntpdate 210.72.145.44 &> /dev/null"'

查看计划任务

ansible web -m shell -a 'crontab -l'

删除计划任务

ansible web -m cron -a 'name="ntp update" minute=* job="/sbin/ntpdate 210.72.145.44 &> /dev/null" state=absent'

8、yum 模块

yum 模块主要用于软件的安装

常用命令

安装nginx

ansible web -m yum -a 'name=nginx state=present'

卸载nginx

ansible web -m yum -a 'name=nginx state=absent'

9、service 模块

service 模块用于服务程序的管理

常用命令

开启服务并设置自启动

ansible web -m service -a 'name=nginx state=started enabled=true'

查看端口

ansible web -m shell -a 'ss -nplt'

关闭服务

ansible web -m service -a 'name=nginx state=stopped'

 查看端口

ansible web -m shell -a 'ss -nplt'

10、user 模块

user 模块主要是用来管理用户账号

常用命令

添加一个用户并指定其 uid

ansible web -m user -a 'name=lyunxi uid=1111'

查看用户

ansible web -m shell -a 'id lyunxi'

删除用户

ansible web -m user -a 'name=lyunxi state=absent'

 查看用户

ansible web -m shell -a 'cat /etc/passwd | grep lyunxi'

11、group 模块

group 模块主要用于添加或删除组

常用命令

创建组

ansible web -m group -a 'name=yun gid=2222'

查看组

ansible web -m shell -a 'cat /etc/group | grep 2222'

删除组

ansible web -m group -a 'name=yun state=absent'

 查看组

ansible web -m shell -a 'cat /etc/group | grep 2222'

12、script 模块

script 模块用于将本机的脚本在被管理端的机器上运行

首先,写一个脚本

[root@pc1 ~]# vim file.sh

#!/bin/bash
echo 123456 > /root/file.txt

[root@pc1 ~]# chmod +x file.sh

然后,运行命令实现在被管理端执行该脚本

ansible web -m script -a '/root/file.sh'

查看脚本写出的文件

ansible web -m shell -a 'cat /root/file.txt'

13、setup 模块

setup 模块主要用于收集信息,是通过调用facts组件来实现的

        facts组件是Ansible用于采集被管机器设备信息的一个功能,我们可以使用setup模块查机器的所有facts信息,可以使用filter来查看指定信息。整个facts信息被包装在一个JSON格式的数据结构中,ansible_facts是最上层的值。
   facts就是变量,内建变量 。每个主机的各种信息,cpu颗数、内存大小等。会存在facts中的某个变量中。调用后返回很多对应主机的信息,在后面的操作中可以根据不同的信息来做不同的操作。如redhat系列用yum安装,而debian系列用apt来安装软件。

查看内存

ansible web -m setup -a 'filter="*mem*"'

保存信息(保存在ansible控制主机上)

ansible web -m setup -a 'filter="*mem*"' --tree /opt/memory

查看保存的信息

ll /opt/memory/

14、get_url 模块

get_url 模块用于将文件或软件从http、https或ftp下载到本地节点上

常用命令

下载阿里的epel镜像源

ansible -i /etc/ansible/hosts web -m get_url -a 'url=https://mirrors.aliyun.com/repo/epel-7.repo dest=/etc/yum.repos.d'

15、stat 模块

stat 模块用于检查文件或文件系统的状态

常用的返回值判断:
exists: 判断是否存在
isuid  : 调用用户的ID与所有者ID是否匹配

注意:对于Windows目标,请改用win_stat模块

常用命令

检查文件是否存在

ansible web -m stat -a 'path=/opt/hello'

ansible web -m stat -a 'path=/opt/hell'

16、unarchive 模块

unarchive 模块用于从本地机器上复制存档后,将其解包

说明:
默认情况下,在解包之前将源文件从本地系统复制到目标。
设置remote_src=yes为解包目标上已经存在的档案。

注意:对于Windows目标,请改用win_unzip模块。

常用命令

解压文件 php-7.3.6.tar.gz

ansible -i /etc/ansible/hosts web -m unarchive -a 'src=/root/php-7.3.6.tar.gz dest=/opt'

查看解压的文件

ansible -i /etc/ansible/hosts web -m shell -a 'ls /opt/php-7.3.6'

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值