设为首页 加入收藏

TOP

Ansible常用模块(二)
2023-07-23 13:30:41 】 浏览:84
Tags:Ansible 常用模
r上的tar包解压传到指定的节点主机路径上。

ansible mysql -m unarchive -a "src=/root/nginx-1.12.2.tar.gz dest=/root/ copy=yes"
ansible mysql -m shell -a "ls -ld /root/nginx-*"

Script模块

用于在被管理机器上面执行shell脚本的模块,脚本无需在被管理机器上面存在,执行结果会在server端上输出。

常用参数

  • free_form:必须参数,指定需要执行的脚本,脚本位于 ansible 管理主机本地,并没有具体的一个参数名叫 free_form,具体解释请参考 command 模块。
  • chdir:此参数的作用就是指定一个远程主机中的目录,在执行对应的脚本之前,会先进入到 chdir 参数指定的目录中。
  • creates:使用此参数指定一个远程主机中的文件,当指定的文件存在时,就不执行对应脚本,可参考 command 模块中的解释。
  • removes:使用此参数指定一个远程主机中的文件,当指定的文件不存在时,就不执行对应脚本,可参考 command 模块中的解释。

1、如果ansible主机中的 /root/test.sh 脚本将在httpd主机中执行,执行此脚本之前,会先进入到httpd主机中的/opt目录。

echo -e '#!/bin/bash\necho `hostname`\necho `pwd`' > /root/test.sh
ansible httpd -m script -a 'chdir=/opt /root/test.sh'

2、如果httpd主机中的/bucunzai的文件存在,ansible主机中的/root/test.sh脚本将不会在httpd主机中执行。

ansible httpd -m script -a "creates=/bucunzai /root/test.sh"

3、如果httpd主机中的/root/test文件不存在,ansible主机中的/root/test.sh脚本将不会在httpd主机中执行。

ansible httpd -m script -a "removes=/test /root/test.sh"

File模块

用于对文件的处理,创建,删除,权限控制等。

相关选项:

  • path:在节点主机上创建的目录或文件名
  • recurse:递归
  • state:类型如下
    • state=directory:创建目录
    • state=touch:创建文件
    • state=mode:设置文件或目录权限
    • state=owner:设置文件或目录属主信息
    • state=group:设置文件或目录属组信息
    • state=absent:删除文件或目录
    • state=link:创建软链接
    • state=hard:创建硬链接

给节点1创建目录及文件;在此之前先将节点上的/root/路径下的目录及文件清空

ansible httpd -m shell -a "rm -rf /root/*"

创建目录

[root@server ~]# ansible httpd -m file -a "path=/root/lemon state=directory"

查看一下结果

ansible httpd -m shell -a "ls -l /root/"

创建文件

ansible httpd -m file -a "path=/root/hehe.txt state=touch"

查看一下结果

ansible httpd -m shell -a "ls -l /root/"

给hehe.txt文件创建一个软连接(src表示源文件,dest表示目标文件)

ansible httpd -m file -a "src=/root/hehe.txt dest=/hehe state=link"

查看结果

ansible httpd -m shell -a "ls -l /hehe"

删除目录

ansible httpd -m file -a "path=/root/lemon state=absent"

删除文件(包括软连接)

ansible httpd -m file -a "path=/hehe state=absent"
ansible httpd -m file -a "path=/root/hehe.txt state=absent"

查看一下结果

ansible httpd -m shell -a "ls -l /root/"
ansible httpd -m shell -a "ls -l /hehe"

创建文件时同时设置权限、属主数组等信息

ansible httpd -m file -a 'path=/root/test state=directory mode=700 owner=tom group=tom'

ansible httpd -m shell -a 'ls -lh /root'       //验证

使用递归

ansible httpd -m shell -a 'touch /root/test/{1..3}.txt && ls -lh test'

ansible httpd -m file -a 'path=/root/test mode=700 owner=tom group=tom recurse=yes'

ansible httpd -m shell -a 'ls -lh /root/test'       //验证

Fetch模块

从节点主机上拉取文件到本机,拉取下来的内容会保留目录结构,一般情况用在收集被管理机器的日志文件等。

只能fetch文件,不能fetch目录,如果想拉目录,先tar/zip打包 再拉到本机即可。

ansible 192.168.1.2 -m fetch -a 'src=/var/log/cron dest=/root'
  • src=/var/log/cron:节点主机的路径文件
  • dest=/root/:server的路径

cron模块

cron模块

相关选项:

  • job // 指定需要执行的任务
  • minute // 分钟
  • hour //小时
  • day // 天
  • month //月
  • weekday // 周
  • name // 对计划任务进行描述
  • state // 指定状态,默认为添加定时任务
    • absetn // 删除计划任务
    • prsent // 添加定时任务
// 创建一个每隔五分钟同步一次时间的计划任务
ansible httpd -m cron -a "minute='*/5' job='/usr/sbin/ntpdate 172.16.0.1 &> /dev/null' name='Synctime'"

解释:
minute:分钟
job:命令
name:计划任务的名字

// 查看一下结果
ansible httpd -m shell -a "crontab -l"

删除计划任务

ansible httpd -m cron -a "state=absent
首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇shell #!/bin/bash: No such file.. 下一篇深度解析 slab 内存池回收内存以..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目