自动化运维工具:ansible常见模块

常用模块

1、copy模块

从本地copy文件分发到目标节点主机路径

参数说明:
src= 源文件路径 source
dest= 目标路径 destination
注意src= 路径后面带/ 表示带里面的所有内容复制到目标目录下,不带/是目录递归复制过去
content= 自行填充的文件内容
owner 属主
group 属组
mode权限

示例:

# 上传到目标主机
[root@ansible ~]# ansible webservers  -m copy  -a "src=/root/shell_dir dest=/tmp"

[root@ansible ~]# ansible  webservers -m copy  -a "src=/etc/passwd dest=/tmp mode=777"
[root@ansible ~]# ansible  webservers -m copy  -a "content='hello,world\n' dest=/tmp/sc.txt  mode=644"
# 拷贝文件夹
[root@ansible ansible]# time ansible all -m copy -a "src=/boot dest=/huhai_daozhu" 
[root@ansible lianxi]# ansible all -m copy  -a "src=/lianxi dest=/root mode=644"

2、fetch模块

从远程主机拉取文件到本地

说明:fetch使用很简单,src和dest,dest只要指定一个接收目录,默认会在后面加上远程主机及src的路径
拉取完成后,会自动根据主机的ip地址新建文件夹,存放对应文件,以示区别。

fetch 模块暂时不支持拉取文件夹,只能拉取文件

[root@ansible ~]# mkdir /test
[root@ansible ~]# ansible webservers -m fetch  -a 'src=/etc/passwd  dest=/test mode=644'
192.168.1.163 | CHANGED => {
    "changed": true, 
    "checksum": "009b38c639c768b22ae606d4350a1409171eafaa", 
    "dest": "/test/192.168.1.163/etc/passwd", 
    "md5sum": "b4ac78d39b559beb6a547c2e1e89a8a1", 
    "remote_checksum": "009b38c639c768b22ae606d4350a1409171eafaa", 
    "remote_md5sum": null
}

3、command模块

在远程主机上执行命令,属于裸执行,非键值对显示;不进行shell解析;

不能识别管道符号,ifconfig|grep 这是一个命令了
这就是因为command模块不是shell解析属于裸执行导致的

为了能达成以上类似shell中的解析,ansible有一个shell模块;

[root@ansible log]# ansible all -m command -a "ifconfig"

4、shell模块

由于commnad只能执行裸命令(即系统环境中有支持的命令),至于管道之类的功能不支持,
shell模块可以做到
[root@ansible log]# ansible all -m shell -a "ifconfig|grep 192"
	192.168.0.80 | SUCCESS | rc=0 >>inet 192.168.0.80  netmask 255.255.255.0  broadcast 192.168.0.255
	192.168.0.117 | SUCCESS | rc=0 >>inet 192.168.0.117  netmask 255.255.255.0  broadcast 192.168.0.255

5、file模块

设置文件属性(创建文件)也可以删除文件

常用参数:
path目标路径
state directory为目录,link为软件链接
absent 删除文件和文件夹
directory 新建目录
touch 新建空文件
link 软连接文件
hard 硬链接文件
group 目录属组
owner 属主
其他参数通过ansible-doc -s file 获取

# 创建目录
[root@ansible ~]# ansible all -m file -a "path=/tmp/sanchuang state=directory"	       
# 创建一个空文件
[root@ansible ~]# ansible all -m file -a "path=/tmp/sanchuang/feng.txt state=touch"	
# 创建链接文件(软链接)
[root@ansible ~]# ansible all -m file -a "src=/tmp/sc.txt path=/tmp/sanchuang/sc.link  state=link"

有些工作,可以使用file模块也可以使用shell模块,但是在新建文件或者文件夹的时候,推荐使用file模块

6、cron模块

通过cron模块对目标主机生成计划任务

常用参数:
除了分(minute)时(hour)日(day)月(month)周(week)外
name: 本次计划任务的名称
state: present 生成(默认) |absent 删除 (基于name)

# 创建计划任务
[root@ansible ~]# ansible all -m cron -a "minute=*/3 job='/usr/sbin/ntpdate ntp1.aliyun.com'  name=update_time state=present"
# 目标机器上查看
	[root@www tmp]# crontab -l
	*/10 * * * * bash /root/iptables_ssh.sh
	#Ansible: None
	*/3 * * * * /usr/sbin/ntpdate ntp1.aliyun.com  name=update_time
# 删除计划任务
[root@ansible ~]# ansible all -m cron -a "name=update_time state=absent"
	
[root@ansible test]# ansible all -m cron -a "minute=0 hour=8-18  weekday=1-5 job='bash /root/iptables.sh' name=runiptables"

[root@ansible lianxi]# ansible all -m cron -a "minute=30 hour=17 day=14 month=7 job='bash /lianxi2/display.sh' name=sanchuang_shell"

练习:
编写一个脚本,实现2个node节点服务器备份/var/log目录到/backup目录下文件名格式2019-7-10-log.tar.gz
每天2:30备份

需求分析:
1.在ansible机器上写好脚本
2.把脚本推倒node服务器上
3.创建计划任务

[root@ansible test]# cat backup_log.sh 
#!/bin/bash

ctime=$(date +%F%H%M%S)
mkdir -p /backup
tar czf  /backup/${ctime}-log.tar.gz  /var/log

# 推过去
[root@ansible test]# ansible all -m copy -a  'src=/test/backup_log.sh  dest=/root/'
# 创建计划任务
[root@ansible test]# ansible all -m cron  -a "minute=30 hour=2 job='bash /root/backup_log.sh' name=create_backup_log"
# 删除计划任务
[root@ansible test]# ansible all -m cron -a "name=create_backup_log state=absent"

7、yum模块

故名思义就是yum安装软件包的模块;

常用参数说明:
enablerepo,disablerepo表示启用与禁用某repo库
name 安装包名
state (present’ orinstalled’, latest’)表示安装, (absent’ or `removed’) 表示删除

示例:通过安装epel扩展源并安装nginx

[root@ansible ~]# ansible all -m yum  -a "name=ntpdate state=installed"

使用yum模块同时安装多个软件,注意使用逗号隔开

[root@ansible lianxi]# ansible all -m yum -a "name=tree,lsof state=installed"

8、service模块

服务管理模块

常用参数:
name:服务名
state:服务状态
enabled: 是否开机启动 true|false
runlevel: 启动级别 (systemed方式忽略)
#停止nginx服务,但是设置开机启动
[root@ansible ~]# ansible  all -m service  -a "name=nginx state=stopped enabled=true"

# 启动和重新启动
[root@ansible ~]# ansible  all -m service  -a "name=nginx state=started enabled=true"
[root@ansible ~]# ansible  all -m service  -a "name=nginx state=restarted enabled=true"

如何查看服务当前的状态及是否开机启动?

[root@www tmp]# service nginx status
	Redirecting to /bin/systemctl status  nginx.service
	● nginx.service - The nginx HTTP and reverse proxy server
	   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)

		enabled 说明开机启动

	   Active: active (running) since 四 2018-09-06 15:57:21 CST; 3s ago
		running 说明现在是启动的   dead 表示没有启动

查看所有的服务

[root@node1 ssh]# systemctl list-units-files

9、script模块

把本地的脚本传到远端执行;前提是到远端可以执行,不要把Linux下的脚本同步到windows下执行~=copy+shell
script模块 只是在远程服务器上执行脚本,不上传脚本到远程服务器

[root@ansible ~]# ansible webservers -m script -a "/root/test.sh"
	
当管理的机器特别多的时候,如果要提升性能,建议使用多进程技术
-f  5 启动5个进程来进行操作
[root@ansible ~]# ansible webservers -f 5 -m script -a "/root/test.sh"

猜你喜欢

转载自blog.csdn.net/weixin_44321163/article/details/107897967