ansible 常用模块介绍

Ad-Hoc

帮助文档:
列出ansible支持的模块:
-l:获取列表
-s module_name:获取指定模块的使用信息
看所有模块(A10,华为,docker,EC2,aws等等广大厂商设备)
[root@ansible-server ~]# ansible-doc -l
查看模块使用信息,了解其功能:
[root@ansible-server ~]# ansible-doc -s yum

远程复制备份模块 copy

src=源文件路径(也就是server端的文件路径)
desr=目标文件路径 (后面被控节点的路径)
group=属组
owner=属主
mode=权限
backup 在覆盖之前会先备份牛的文件
[root@ansible-server ~]#  ansible weball -m copy -a "src=/root/passwd dest=/opt/passwd owner=nginx group=nginx mode=777" -o
[root@ansible-server ~]# vim passwd 
[root@ansible-server ~]# ansible weball -m copy -a "src=/root/passwd dest=/opt/passwd owner=nginx group=nginx mode=777 backup=yes" -o 
# 注释:如果文件没有变化,不会备份。只有文件内容不同,才会做备份。
[root@ansible-web1 ~]# ls /opt/
passwd  passwd.95467.2020-04-25@11:11:54~
[root@ansible-server ~]# ansible weball -m shell -a 'mv /mnt/qf.txt /tmp' -o
移动被控制节点的文件

软件包管理 yum模块

安装apache
[root@ansible-server ~]# ansible webservers1 -m yum -a "name=httpd state=latest" -o
state=     #状态是什么,干什么
state=absent        用于remove安装包
state=latest       表示最新的
state=removed      表示卸载
卸载软件:
[root@ansible-server ~]# ansible webservers1 -m yum -a "name=httpd state=removed" -o
或者
[root@ansible-server ~]# ansible webservers1 -m yum -a "name=httpd state=absent" -o

服务管理service 模块

[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=started" #启动
[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=stopped" #停止
[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=restarted" #重启
[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=started enabled=yes" #开机启动
[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=started enabled=no"  #开机关闭

文件模块

模块参数详解:  
owner:修改属主
group:修改属组
mode:修改权限
path=:要修改文件的路径
recurse:递归的设置文件的属性,只对目录有效
        yes:表示使用递归设置
state:
touch:创建一个新的空文件
directory:创建一个新的目录,当目录存在时不会进行修改
#创建一个文件
[root@ansible-server ~]# ansible webservers1 -m file -a 'path=/tmp/youngfit1.txt mode=777 state=touch'
[root@ansible-server ~]#  ansible ansible-web2 -m file -a 'path=/tmp/youngfit2.txt mode=777 owner=nginx state=touch'
#创建一个目录
[root@ansible-server ~]# ansible webservers1 -m file -a 'path=/tmp/qf mode=777 state=directory' 

被控节点ansible-web2操作:
[root@ansible-web2 tmp]# cd /opt/
[root@ansible-web2 opt]# ll haha
total 0
-rw-r--r--. 1 root  root 0 Sep 12 09:41 haha2.txt
-rw-r--r--. 1 nginx root 0 Sep 12 09:41 haha.txt

[root@ansible-server ~]# ansible ansible-web2 -m file -a "path=/opt/haha owner=nginx group=nginx  state=directory recurse=yes"

被控节点查看:
[root@ansible-web2 opt]# ll haha
total 0
-rw-r--r--. 1 nginx nginx 0 Sep 12 09:41 haha2.txt
-rw-r--r--. 1 nginx nginx 0 Sep 12 09:41 haha.txt

收集信息模块 setup

[root@ansible-server ~]# ansible webservers1 -m setup  #收集所有信息
[root@ansible-server ~]# ansible webservers1 -m setup -a 'filter=ansible_all_ipv4_addresses' #只查询ipv4的地址
filter:过滤
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值