ansible提供了众多模块,我们可以在ansible主机上运行ansible-doc -l命令查看ansible所有支持的模块。通过ansible-doc -s MODULE_NAME  命令可以查看指定模块的所有参数


查看所有模块

root@host1:/etc/ansible/roles/tomcat8_install/tasks# ansible-doc  -l
less 436
Copyright (C) 1984-2009 Mark Nudelman
less comes with NO WARRANTY, to the extent permitted by law.
For information about the terms of redistribution,
see the file named README in the less distribution.
Homepage: http://www.greenwoodsoftware.com/less
a10_server                      Manage A10 Networks AX/SoftAX/Thunder/vThunder devices                                     
a10_service_group               Manage A10 Networks devices' service groups                                                
a10_virtual_server              Manage A10 Networks devices' virtual servers                                               
acl                             Sets and retrieves file ACL information.                                                   
add_host                        add a host (and alternatively a group) to the ansible-playbook in-memory inventory         
airbrake_deployment             Notify airbrake about app deployments                                                      
alternatives                    Manages alternative programs for common commands                                           
apache2_module                  enables/disables a module of the Apache2 webserver                                         
apk                             Manages apk packages                                                                       
apt                             Manages apt-packages                                                                       
apt_key                         Add or remove an apt key                                                                   
apt_repository                  Add and remove APT repositories                                                            
apt_rpm                         apt_rpm package manager                                                                    
assemble                        Assembles a configuration file from fragments                                              
assert                          Fail with custom message                                                                   
at                              Schedule the execution of a command or script file via the at command.                     
authorized_key                  Adds or removes an SSH authorized key                                                      
azure                           create or terminate a virtual machine in azure                                             
bigip_facts                     Collect facts from F5 BIG-IP devices                                                       
bigip_gtm_wide_ip               Manages F5 BIG-IP GTM wide ip                                                              
bigip_monitor_http              Manages F5 BIG-IP LTM


查看模块参数:

ansible-doc -s MODULE_NAME
例如:
[root@localhost ansible]# ansible-doc -s file
- name: Sets attributes of files
  action: file
      follow                 # This flag indicates that filesystem links, if they exist, should be followed.
      force                  # force the creation of the symlinks in two cases: the source file does not exist (but will appear later);
                               the destination exists and is a file (so, we need to unlink the "path" file
                               and create symlink to the "src" file in place of it).
      group                  # name of the group that should own the file/directory, as would be fed to `chown'
      mode                   # mode the file or directory should be. For those used to `/usr/bin/chmod' remember that modes are actually
                               octal numbers (like 0644). Leaving off the leading zero will likely have
                               unexpected results. As of version 1.8, the mode may be specified as a
                               symbolic mode (for example, `u+rwx' or `u=rw,g=r,o=r').
      owner                  # name of the user that should own the file/directory, as would be fed to `chown'
      path=                  # path to the file being managed.  Aliases: `dest', `name'
      recurse                # recursively set the specified file attributes (applies only to state=directory)
      selevel                # level part of the SELinux file context. This is the MLS/MCS attribute, sometimes known as the `range'.
                               `_default' feature works as for `seuser'.
      serole                 # role part of SELinux file context, `_default' feature works as for `seuser'.
      setype                 # type part of SELinux file context, `_default' feature works as for `seuser'.
      seuser                 # user part of SELinux file context. Will default to system policy, if applicable. If set to `_default', it
                               will use the `user' portion of the policy if available
      src                    # path of the file to link to (applies only to `state=link'). Will accept absolute, relative and nonexisting
                               paths. Relative paths are not expanded.
      state                  # If `directory', all immediate subdirectories will be created if they do not exist, since 1.7 they will be
                               created with the supplied permissions. If `file', the file will NOT be
                               created if it does not exist, see the [copy] or [template] module if you
                               want that behavior.  If `link', the symbolic link will be created or
                               changed. Use `hard' for hardlinks. If `absent', directories will be
                               recursively deleted, and files or symlinks will be unlinked. If `touch' (new
                               in 1.4), an empty file will be created if the `path' does not exist, while
                               an existing file or directory will receive updated file access and
                               modification times (similar to the way `touch` works from the command line)



模块介绍:


copy模块:http://docs.ansible.com/ansible/copy_module.html

功能:复制文件到远程主机的指定路径下:

参数:

src:本地文件的路径,如果源是一个目录,会将目录中所有的文件都copy过去

dest:远程主机的绝对路径

owner:文件属主

group:文件属组

mode:文件权限


命令演示:

ansible all -m copy -a 'src=/etc/fstab dest=/tmp/fstab owner=root mode=0644'

file模块:http://docs.ansible.com/ansible/file_module.html

功能:设置文件属性、创建符号链接、创建目录等

参数:

path:指明文件路径,可以使用name或dest来代替

owner:文件属主

group:文件属组

mode:文件权限

创建文件的符号链接:

src:指明源文件

dest:指明符号链接文件路径


命令演示:

ansible pms -m file -a 'src=/tmp/fstab dest=/srv/fstab state=link'
[root@localhost srv]# ll
lrwxrwxrwx 1 root root      10 Jul  6 14:08 fstab -> /tmp/fstab


ping模块:http://docs.ansible.com/ansible/ping_module.html

功能:测试被管理主机的连通性

命令演示:

[root@localhost ansible]# ansible all -m ping
172.16.206.134 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
10.10.10.202 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}



command模块:http://docs.ansible.com/ansible/command_module.html

功能:在远程主机上执行命令

命令演示:

[root@localhost ansible]# ansible all -m command -a 'hostname'      
172.16.206.134 | SUCCESS | rc=0 >>
localhost.localdomain
10.10.10.202 | SUCCESS | rc=0 >>
localhost.localdomain

注意:command模块不支持管道符,这也是command模块和shell模块的区别。

例如:



user模块:http://docs.ansible.com/ansible/user_module.html

功能:在远程主机上创建或者删除用户

参数:

name:账户名

state:

          present:创建

          absent:删除   

group:指定用户的基本组

uid:指定uid

system:创建系统用户 值为yes 或者no

命令演示:

[root@localhost ansible]# ansible pms -m user -a 'name=test state=present uid=306 group=root  system=yes'          
172.16.206.134 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "createhome": true, 
    "group": 0, 
    "home": "/home/test", 
    "name": "test", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": true, 
    "uid": 306
}
[root@localhost srv]# id test
uid=306(test) gid=0(root) groups=0(root)


service模块:http://docs.ansible.com/ansible/service_module.html

功能:管理远程主机上的服务状态

参数:

enabled=:是否开机自动启动,取值为yes或者no。enabled=yes,表示服务开启启动

name=:服务名

state=: 服务状态

    started:启动

    restarted:重启

    stopped:停止

    reloaded:重载

命令演示:

[root@localhost ansible]# ansible pms -m service -a 'name=zabbix-agent state=started enabled=yes'
172.16.206.134 | SUCCESS => {
    "changed": true, 
    "enabled": true, 
    "name": "zabbix-agent", 
    "state": "started"
}



script模块:http://docs.ansible.com/ansible/script_module.html

功能:在远程主机执行主控端的shell/python脚本

root@host1:/tmp# ansible pms -m script -a '/tmp/echo.sh'
172.16.206.134 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "", 
    "stdout": "", 
    "stdout_lines": []
}



shell模块:http://docs.ansible.com/ansible/shell_module.html

shell模块支持管道符,这是它与commands模块的最大区别

命令演示:

ansible pms -m shell -a 'ps -ef | grep tomcat'        
172.16.206.134 | SUCCESS | rc=0 >>
root      18569      1  0 13:09 pts/0    00:00:36 /usr/java/jdk1.8.0_66/bin/java -Djava.util.logging.config.file=/tmp/catalina_base/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -server -XX:PermSize=256M -XX:MaxPermSize=1024m -Xms512M -Xmx1024M -XX:MaxNewSize=256m -Djava.endorsed.dirs=/usr/local/apache-tomcat-8.0.29/endorsed -classpath /usr/local/apache-tomcat-8.0.29/bin/bootstrap.jar:/usr/local/apache-tomcat-8.0.29/bin/tomcat-juli.jar -Dcatalina.base=/tmp/catalina_base -Dcatalina.home=/usr/local/apache-tomcat-8.0.29 -Djava.io.tmpdir=/tmp/catalina_base/temp org.apache.catalina.startup.Bootstrap start
root      19314  19313  0 17:24 pts/1    00:00:00 /bin/sh -c ps -ef | grep tomcat
root      19316  19314  0 17:24 pts/1    00:00:00 grep tomcat


yum模块:http://docs.ansible.com/ansible/yum_module.html

功能:在远程主机上安装软件包

参数:

name=:  包名,如果从远程服务器本地安装某个包,则可以写该包在远程主机上绝对的路径,如name=/srv/jdk/jdk-8u66-linux-x64.rpm

state=:状态,值为present,absent,lastest

    present、lasted安装

    absent:卸载

    lastest:安装最新版的包,相当于升级软件包

    removed:删除软件包

    installed:安装软件包

[root@localhost tmp]# ansible pms -m yum -a 'name=/srv/jdk/jdk-8u66-linux-x64.rpm  state=present'  
172.16.206.134 | SUCCESS => {
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": []
}



synchronize模块:http://docs.ansible.com/ansible/synchronize_module.html

注意:ansible主机和远程主机上都需要安装rsync

功能:将ansible主机上的源目录下的文件同步到远程主机上

参数:

src:ansible主机上的源路径

dest:远程主机上的目标路径

delete:delete=yes时,删除目标路径下,源路径中不存在的目录或者文件

compress:是否开启压缩功能,默认为开启

mode:同步模式,默认为push,设置mode=pull,改成pull模式

archive:默认开启了这个参数,该参数默认开启了recursive, links, perms, times, owner,group和-D参数。如果你将该参数设置为no,那么你将停止很多参数,比如会导致如下目的递归失败,导致无法拉取

rsync_opts:增加rsync的额外选项,例如

rsync_opts="--exclude=fstab" 表示同步时文件时,排除fstab文件,即不同步fstab文件。如果有delete=yes选项,而目标路径下有一个源路径下不存在的文件,如文件名为fstab,那么

rsync_opts="--exclude=fstab"表示不删除目标路径下的fstab文件

ansible pms  -m synchronize -a 'src=/tmp/test/ dest=/tmp/aaa/ delete=yes  rsync_opts="--exclude=fstab"'

上面的命令表示将ansible主机上/tmp/test/目录下的所有文件(除了fstab)同步到远程主机的/tmp/aaa/目录下。并删除/tmp/aaa/目录下,在/tmp/test/上不存在的文件或者目录



unarchive模块:http://docs.ansible.com/ansible/unarchive_module.html

功能:解压缩,这个模块有两种用法:

1、将ansible主机上的压缩包在本地解压缩后传到远程主机上,这种情况下,copy=yes

2、将远程主机上的某个压缩包解压缩到指定路径下。这种情况下,需要设置copy=no


参数:

copy:默认为yes,当copy=yes,那么拷贝的文件是从ansible主机复制到远程主机上的,如果设置为copy=no,那么会在远程主机上寻找src源文件

src:源路径,可以是ansible主机上的路径,也可以是远程主机上的路径,如果是远程主机上的路径,则需要设置copy=no

dest:远程主机上的目标路径

mode:设置解压缩后的文件权限


命令演示:

ansible pms -m unarchive -a 'src=/srv/tomcat8/apache-tomcat-8.0.29.tar.gz dest=/usr/local copy=no mode=0755'


get_url模块:http://docs.ansible.com/ansible/get_url_module.html

功能:从http、https、ftp下载文件到远程主机

参数:

url:下载地址

dest:远程主机上的目标径路

mode:设置下载到远程主机后的文件的权限

命令演示:

root@host1:/srv# ansible pms -m get_url -a 'url=ftp://ftp.cheyaoshicorp.com/pub/临时文件/derby.init.sh dest=/tmp'      
172.16.206.134 | SUCCESS => {
    "changed": true, 
    "checksum_dest": null, 
    "checksum_src": "770a432e9847e594e0154e31c906062585d571e0", 
    "dest": "/tmp/derby.init.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "4564411c7e614859965c9ab5d76df22b", 
    "mode": "0644", 
    "msg": "OK (3934 bytes)", 
    "owner": "root", 
    "size": 3934, 
    "src": "/tmp/tmp5nqAsJ", 
    "state": "file", 
    "uid": 0, 
    "url": "ftp://ftp.cheyaoshicorp.com/pub/临时文件/derby.init.sh"