2019-04-24课堂笔记

作者:黄胜

Ansible的模块用法讲解

注释:以下所有模块的远程操作,源文件只要本地存在即可

1.cript模块

功能说明:在远程节点上运行本地脚本

例:[root@m01 /server/scripts]# cat setup.sh

pwd

ls /root

for n in {1..100}

do

  echo $n >>/tmp/oldboy.log

done

执行:

ansible oldboy  -m script -a "/server/scripts/setup.sh"

2.copy模块

功能说明:复制文件到远程主机

复制本地文件到远端:ansible oldboy -m copy -a "src=/server dest=/ mode=ugo+x group=root owner=root"

复制到远端并在远端备份:

ansible oldboy  -m copy -a "src=/server/scripts/setup dest=/server/scripts mode=ugo+x  group=root owner=root backup=yes"

3.file模块

功能说明:设置文件属性

创建目录:mkdir /tmp/oldboy_dir

ansible oldboy -m file -a "dest=/tmp/oldboy_dir state=directory"

递归设置权限:

ansible oldboy -m file -a "dest=/tmp/oldboy_dir state=directory mode=644 recurse=yes"

创建文件:touch /tmp/oldboy_file

ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch"

删除文件:rm -f /tmp/oldboy_file

ansible oldboy -m file -a "dest=/tmp/oldboy_file state=absent"

创建链接文件:ln -s /etc/hosts /tmp/link_file

ansible oldboy -m file -a "src=/etc/hosts dest=/tmp/link_file state=link"

ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch owner=oldboy group=oldboy mode=000"

ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch owner=oldboy group=oldboy mode=ugo=rwx"

4.yum模块

功能说明:yum包管理模块

ansible oldboy -m yum -a "name=nginx state=installed"

[root@nfs01 oldboy_dir]# rpm -qa nginx

nginx-1.10.2-1.el6.x86_64

###不要用yum卸载,用rpm -e卸载。

5.service模块

功能说明:启动停止服务

cron模块

功能说明:管理定时任务条目信息模块

创建定时任务:

ansible oldboy -m cron -a "name='sync time' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'"

结果:

#Ansible: sync time

00 00 * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1

添加如下定时任务:

05 03 * * * /bin/sh /server/scripts/backup.sh /server/scripts/list >/dev/null 2>&1

命令如下:

ansible oldboy -m cron -a "name='backup data' minute=05 hour=03 job='/bin/sh /server/scripts/backup.sh /server/scripts/list >/dev/null 2>&1'"

结果:

#Ansible: backup data

05 03 * * * /bin/sh /server/scripts/backup.sh /server/scripts/list >/dev/null 2>&1

删除定时任务:

ansible oldboy -m cron -a "name='backup data' state=absent"


你可能感兴趣的:(2019-04-24课堂笔记)