使用示例:
ansible test -m file -a "src=/etc/fstab dest=/tmp/fstab state=link" //将/etc/fstab连接到/tmp/fstab
ansible test -m file -a "path=/tmp/fstab state=absent" //将/etc/fstab删除或者取消连接
ansible test -m file -a "path=/tmp/test state=touch"/tmp/test //如果文件不存在
三、copy模块
复制文件到远程主机,copy模块包含如下选项:
validate :The validation command to run before copying into place. The path to the file to validate is passed in via '%s' which must be present as in the visudo example below.
示例如下:
ansible test -m copy -a "src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode=0644"
ansible test -m copy -a "src=/mine/ntp.conf dest=/etc/ntp.conf owner=root group=root mode=644 backup=yes"
ansible test -m copy -a "src=/mine/sudoers dest=/etc/sudoers validate='visudo -cf %s'"
四、service模块
用于管理服务
该模块包含如下选项:
使用示例:
ansible test -m service -a "name=httpd state=started enabled=yes"
asnible test -m service -a "name=foo pattern=/usr/bin/foo state=started"
ansible test -m service -a "name=network state=restarted args=eth0"
五、cron模块
用于管理计划任务包含如下选项:
示例:
ansible test -m cron -a 'name="a job for reboot" special_time=reboot job="/some/job.sh"'
ansible test -m cron -a 'name="yum autoupdate" weekday="2" minute=0 hour=12 user="root
ansible test -m cron -a 'backup="True" name="test" minute="0" hour="5,2" job="ls -alh > /dev/null"'
ansilbe test -m cron -a 'cron_file=ansible_yum-autoupdate state=absent'
六、yum模块
使用yum包管理器来管理软件包,其选项有:
config_file:yum的配置文件
disable_gpg_check:关闭gpg_check
disablerepo:不启用某个源
enablerepo:启用某个源
name:要进行操作的软件包的名字,也可以传递一个url或者一个本地的rpm包的路径
state:状态
present 如果有就不进行安装
absent 删除
latest 更新
示例如下:
ansible test -m yum -a 'name=httpd state=latest'
ansible test -m yum -a 'name="@Development tools" state=present'
ansible test -m yum -a 'name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present'
七、user模块与group模块
user模块是请求的是useradd, userdel, usermod三个指令,goup模块请求的是groupadd, groupdel, groupmod 三个指令。
1、user模块
示例:
ansible test -m filesystem -a 'fstype=ext2 dev=/dev/sdb1 force=yes'
ansible test -m filesystem -a 'fstype=ext4 dev=/dev/sdb1 opts="-cc"'
十、mount模块
配置挂载点
选项:
dump
fstype:必选项,挂载文件的类型
name:必选项,挂载点
opts:传递给mount命令的参数
src:必选项,要挂载的文件
state:必选项
present:只处理fstab中的配置
absent:删除挂载点
mounted:自动创建挂载点并挂载之
umounted:卸载
示例:
name=/mnt/dvd src=/dev/sr0 fstype=iso9660 opts=ro state=present
name=/srv/disk src='LABEL=SOME_LABEL' state=present
name=/home src='UUID=b3e48f45-f933-4c8e-a700-22a159ec9077' opts=noatime state=present
ansible test -a 'dd if=/dev/zero of=/disk.img bs=4k count=1024'
ansible test -a 'losetup /dev/loop0 /disk.img'
ansible test -m filesystem 'fstype=ext4 force=yes opts=-F dev=/dev/loop0'
ansible test -m mount 'name=/mnt src=/dev/loop0 fstype=ext4 state=mounted opts=rw'
十一、get_url 模块
该模块主要用于从http、ftp、https服务器上下载文件(类似于wget),主要有如下选项:
示例如下:
ansible test -m fetch -a 'src=/etc/hostname dest=/root/remote_hostname'
十四、command
用户在指定节点上执行某命令
chdir:运行command命令前先cd到这个目录
creates:如果这个参数对应的文件存在,就不运行command
executable:将shell切换为command执行,这里的所有命令需要使用绝对路径
removes:如果这个参数对应的文件不存在,就不运行command
使用示例:
ansible test -m command -a 'hostname'
十五、script模块:
creates # 一个文件名,当这个文件存在,则该命令不执行
free_form= # 本地脚本路径
removes # 一个文件名,这个文件不存在,则该命令不执行
使用示例:
[iyunv@centos6-1 ~]# cat time.sh #在本地主机编写脚本
#!/bin/bash
date >/tmp/shijian
[iyunv@centos6-1 ~]# ansible test -m script -a 'time.sh' #在远程主机上执行
十六、hostname模块
主要用户来更改用户名
Name:主机名称
使用示例:
ansible server -m hostname -a “name=Ansible” //此处的Ansible可更改一个随机变量,进行区分不同主机
十七、template模块
template使用了Jinjia2格式作为文件模版,它的每次使用都会被ansible标记为”changed”状态。且template下的文件可以使用var里面定义的变量