ansible常用模块有哪些?

5、Ad-Hoc

ad hoc其实就是执行简单的命令——一条命令。对于复杂的命令则为 playbook。

用的最多的就是shell模块

帮助文档:
列出ansible支持的模块:
-l:获取列表
-s module_name:获取指定模块的使用信息
看所有模块(A10,华为,docker,EC2,aws等等广大厂商设备)
[root@ansible-server ~]# ansible-doc -l
查看模块使用信息,了解其功能:
[root@ansible-server ~]# ansible-doc -s yum
查看shell模块
ansible-doc -s shell
查看copy模块
ansible-doc -s copy
常用模块
ansible webservers -m copy -a 'src=/soft/galera-3-25.3.37-1.el7.x86_64.rpm dest=/opt owner=root group=root mode=644 backup=yes' -o
//进行查看
ansible webservers -a 'ls /opt'

1.远程复制备份模块:copy
模块参数详解:  
src=:指定源文件路径
dest=:目标地址(拷贝到哪里)
owner:指定属主
group:指定属组
mode:指定权限,可以以数字指定比如0644
backup:在覆盖之前将原文件备份,备份文件包含时间信息。有两个选项:yes|no
[root@ansible-server ~]# vim a.txt  #创建一个测试文件
123123

[root@ansible-server ~]# ansible webservers -m copy -a 'src=/root/a.txt dest=/opt owner=root group=root mode=644' -o
[root@ansible-server ~]# vim a.txt  #追加如下内容
123123
234234
[root@ansible-server ~]# ansible webservers -m copy -a \
 'src=/soft/1.txt dest=/opt owner=root group=root mode=644 backup=yes' -o
登录被控制机器其中一台查看
[root@ansible-web1 ~]# cat /opt/a.txt.15301.2019-09-01\@00\:35\:18~
//给对方创建账号密码
ansible webservers -m user -a 'name=tom password=123456'

2.用户管理user模块
添加用户并设置密码:
[root@ansible-server ~]# ansible ansible-web1 -m user -a 'name=qianfeng password="77777"'
"name=   "  #如:指定的用户名,要安装的软件
====================================================================
删除用户:
[root@ansible-server ~]# ansible ansible-web1 -m user -a "name=qianfeng state=absent" -o
absent #删除用户,但是不会删除家目录
//删除tom账户
ansible webservers -m user -a 'name=tom state=absent'

//安装最新版的telnet
ansible webservers -m yum -a 'name=telnet state=late
//在另一台终端进行查询
rpm -qa | grep telnet
//删除telnet
ansible webservers -m yum -a 'name=telnet state=removed'
//远程执行
ansible-doc -s script

3.软件包管理 yum模块
安装apache
[root@ansible-server ~]# ansible webservers1 -m yum -a "name=httpd state=latest" -o
state=     #状态是什么,干什么
state=absent        用于删除安装包
state=latest       表示最新的
state=removed      表示卸载
卸载软件:
[root@ansible-server ~]# ansible webservers1 -m yum -a "name=httpd state=removed" -o
//启服务一般用systemctl
systemctl status mysqld
//下载httpd
ansible webservers -m yum -a 'name=httpd state=latest'
//在另一台终端进行查询
rpm -qa | grep httpd
//启用httpd
ansible webservers -m service -a 'name=httpd state=started'
//在另一台终端进行查询
ps aux | grep httpd
systemctl status httpd
//允许开启自启
ansible webservers -m service -a 'name=httpd state=started enabled=yes'
//远程查询httpd
ansible webservers -m shell -a 'ps aux | grep httpd'


4.服务管理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"  #开机关闭
//创建一个 /data/shell文件夹
ansible webservers -m file -a 'path=/data/shell mode=755 state=directory'
//在opt下创建一个tar文件夹
ansible webservers -m file -a 'path=/opt/tar  mode=755 state=directory'
//把gz包放到其他终端
ansible webservers -m copy -a 'src=/soft/galera-3-25.3.37-1.el7.x86_64.rpm.tar.gz dest=/opt/tar'

5.文件模块file
模块参数详解:  
owner:修改属主
group:修改属组
mode:修改权限
path=:要修改文件的路径
recurse:递归的设置文件的属性,只对目录有效
        yes:表示使用递归设置
state:
touch:创建一个新的空文件
directory:创建一个新的目录,当目录存在时不会进行修改
#创建一个文件
[root@ansible-server ~]# ansible webservers1 -m file -a 'path=/tmp/88.txt mode=777 state=touch'
#创建一个目录
[root@ansible-server ~]# ansible webservers1 -m file -a 'path=/tmp/99 mode=777 state=directory' 
6.收集信息模块setup
[root@ansible-server ~]# ansible webservers1 -m setup  #收集所有信息
[root@ansible-server ~]# ansible webservers1 -m setup -a 'filter=ansible_all_ipv4_addresses' #只查询ipv4的地址
filter:过滤
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值