码迷,mamicode.com
首页 > 其他好文 > 详细

Ansible 常用模块介绍

时间:2016-07-06 18:38:02      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:ansible 常用模块

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"
}




未完待续













本文出自 “zengestudy” 博客,请务必保留此出处http://zengestudy.blog.51cto.com/1702365/1808752

Ansible 常用模块介绍

标签:ansible 常用模块

原文地址:http://zengestudy.blog.51cto.com/1702365/1808752

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!