lkjhgd 发表于 2016-7-7 09:35:36

Ansible 常用模块介绍

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


查看所有模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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





查看模块参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
ansible-doc -s MODULE_NAME
例如:
# 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 or 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:文件权限

命令演示:

1
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:指明符号链接文件路径

命令演示:

1
2
3
ansible pms -m file -a 'src=/tmp/fstab dest=/srv/fstab state=link'
# ll
lrwxrwxrwx 1 root root      10 Jul6 14:08 fstab -> /tmp/fstab





ping模块:http://docs.ansible.com/ansible/ping_module.html
功能:测试被管理主机的连通性

命令演示:

1
2
3
4
5
6
7
8
9
# 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
功能:在远程主机上执行命令
命令演示:

1
2
3
4
5
# 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:指定uidsystem:创建系统用户 值为yes 或者no命令演示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# ansible pms -m user -a 'name=test state=present uid=306 group=rootsystem=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
}
# 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:重载命令演示:
1
2
3
4
5
6
7
# 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脚本
1
2
3
4
5
6
7
8
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模块的最大区别命令演示:
1
2
3
4
5
ansible pms -m shell -a 'ps -ef | grep tomcat'      
172.16.206.134 | SUCCESS | rc=0 >>
root      18569      10 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      19314193130 17:24 pts/1    00:00:00 /bin/sh -c ps -ef | grep tomcat
root      19316193140 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.rpmstate=:状态,值为present,absent,lastest   present、lasted安装    absent:卸载    lastest:安装最新版的包,相当于升级软件包
    removed:删除软件包    installed:安装软件包
1
2
3
4
5
6
7
# ansible pms -m yum -a 'name=/srv/jdk/jdk-8u66-linux-x64.rpmstate=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时,删除目标路径下,源路径中不存在的目录或者文件
rsync_opts:增加rsync的额外选项,例如
rsync_opts="--exclude=fstab" 表示同步时文件时,排除fstab文件,即不同步fstab文件。如果有delete=yes选项,而目标路径下有一个源路径下不存在的文件,如文件名为fstab,那么rsync_opts="--exclude=fstab"表示不删除目标路径下的fstab文件

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




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



unarchive模块:http://docs.ansible.com/ansible/unarchive_module.html
功能:解压缩
src=/srv/tomcat8/apache-tomcat-8.0.29.tar.gz dest=/usr/local copy=no








页: [1]
查看完整版本: Ansible 常用模块介绍