设为首页 收藏本站
查看: 1251|回复: 0

[经验分享] Ansible 常用模块介绍

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-7-7 09:35:36 | 显示全部楼层 |阅读模式
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
例如:
[iyunv@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:文件权限

命令演示:
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'
[iyunv@localhost srv]# ll
lrwxrwxrwx 1 root root      10 Jul  6 14:08 fstab -> /tmp/fstab




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

命令演示:
1
2
3
4
5
6
7
8
9
[iyunv@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
功能:在远程主机上执行命令
命令演示:
1
2
3
4
5
[iyunv@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
命令演示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[iyunv@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
}
[iyunv@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:重载
命令演示:
1
2
3
4
5
6
7
[iyunv@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"
}




功能:在远程主机执行主控端的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模块支持管道符,这是它与commands模块的最大区别
命令演示:
1
2
3
4
5
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




功能:在远程主机上安装软件包
参数:
name=:  包名,如果从远程服务器本地安装某个包,则可以写该包在远程主机上绝对的路径,如name=/srv/jdk/jdk-8u66-linux-x64.rpm
state=:状态,值为present,absent,lastest
     present、lasted安装
    absent:卸载
    lastest:安装最新版的包,相当于升级软件包
    removed:删除软件包
    installed:安装软件包
1
2
3
4
5
6
7
[iyunv@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": []
}





注意: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=yes  rsync_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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-240598-1-1.html 上篇帖子: ansbile常见错误解决方法 下篇帖子: 运维自动化之ansible的安装与使用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表