Ansible常用模块

1. cron模块


作用:创建删除修改计划任务(使用name识别计划任务)

# 创建一个计划任务 "0 5,2 * * ls -alh > /dev/null"
- cron: name="check dirs" minute="0" hour="5,2" job="ls -alh > /dev/null"
# 删除一个计划任务
- cron: name="check dirs" state=absent

例:

  • 创建一个数据库备份任务
ansible testdb -s -m cron -a 'name="数据库主库备份" minute="0" hour="3" job="/bin/bash -x -v /u1/scripts/dbbak/mydumper.sh >> /var/log/$(data -I).log 2>&1"'
  • 删除一个数据库备份任务
ansible testdb -s -m cron -a 'name="数据库主库备份" state=absent'

2. copy模块


最常用的方法就是将文件从本地拷贝到受控节点(Such as:/etc/rc.local or /etc/hosts 或者是常用的文件推送)

EXAMPLES:
- copy: src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode=0644
# 注释:
	# src:本地文件路径
	# dest:需要推送的文件路径
# The same example as above, but using a symbolic mode equivalent to 0644
- copy: src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode="u=rw,g=r,o=r"

# Another symbolic mode example, adding some permissions and removing others
- copy: src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode="u+rw,g-wx,o-rwx"

# Copy a new "ntp.conf file into place, backing up the original if it differs from the copied version
 - copy: src=/mine/ntp.conf dest=/etc/ntp.conf owner=root group=root mode=644 backup=yes

例:

  • 推送备份脚本到线上服务器
# 注:
#backup:如果客户端存在mydumper.sh这个文件则先备份再,没有直接拷贝
ansible testdb -s -m copy -a "src=/etc/ansible/template/DB_ALL/files/mydumper.sh dest=/u1/scripts/dbbak/ backup=yes"

3. file模块


**作用:**创建文件或者文件夹

# touch the same file, but add/remove some permissions
- file: path=/etc/foo.conf state=touch mode="u+rw,g-wx,o-rwx"

# create a directory if it doesn't exist
 - file: path=/etc/some_directory state=directory mode=0755

例:

  • 创建一个文件夹
ansible testdb -s -m file -a "path=/etc/some_directory state=directory mode=0755"
#path:创建文件夹的绝对路径
#state:指定状态(可以为directory,file,absent,touch)
#mode:指定创建文件的权限

4. template模块


**用途:**主要用于传输j2文件(后面在playbook中会有介绍)

此处省略


5. shell模块


**作用:**好多作用…(__) 嘻嘻……

  • 官方解释:(自己的解释)
#可以用来执行大多数的linux命令,并且支持管道符(好像下面没有这个.......这就尴尬了)
# Execute the command in remote shell; stdout goes to the specified file on the remote.
- shell: somescript.sh >> somelog.txt

# Change the working directory to somedir/ before executing the command.
- shell: somescript.sh >> somelog.txt chdir=somedir/

# You can also use the 'args' form to provide the options. This command will change the working directory to somedir/ and will only run when somedir/somelog.txt doesn't exist.
- shell: somescript.sh >> somelog.txt
  args:
    chdir: somedir/
    creates: somelog.txt

# Run a command that uses non-posix shell-isms (in this example /bin/sh doesn't handle redirection and wildcards together but bash does)
- shell: cat < /tmp/*txt
  args:
    executable: /bin/bash

例:

  • 执行linux命令
#检测testdb组文件的MD5值
ansible testdb -s -m shell -a "md5sum /data/release/6.0.sql"

#执行shell脚本
ansible testdb -f 10 -s -m shell -a "bash sql_6.0.sh > /tmp/a.log chdir=/data/release executable=/bin/bash"
#chdir:cd到脚本所在文件夹
#executable:指定脚本运行环境

未完待续…

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值