天天看点

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

<code>root@host1:</code><code>/etc/ansible/roles/tomcat8_install/tasks</code><code># ansible-doc  -l</code>

<code>less</code> <code>436</code>

<code>Copyright (C) 1984-2009 Mark Nudelman</code>

<code>less</code> <code>comes with NO WARRANTY, to the extent permitted by law.</code>

<code>For information about the terms of redistribution,</code>

<code>see the </code><code>file</code> <code>named README </code><code>in</code> <code>the </code><code>less</code> <code>distribution.</code>

<code>Homepage: http:</code><code>//www</code><code>.greenwoodsoftware.com</code><code>/less</code>

<code>a10_server                      Manage A10 Networks AX</code><code>/SoftAX/Thunder/vThunder</code> <code>devices                                     </code>

<code>a10_service_group               Manage A10 Networks devices' service </code><code>groups</code>                                                

<code>a10_virtual_server              Manage A10 Networks devices' virtual servers                                               </code>

<code>acl                             Sets and retrieves </code><code>file</code> <code>ACL information.                                                   </code>

<code>add_host                        add a host (and alternatively a group) to the ansible-playbook </code><code>in</code><code>-memory inventory         </code>

<code>airbrake_deployment             Notify airbrake about app deployments                                                      </code>

<code>alternatives                    Manages alternative programs </code><code>for</code> <code>common commands                                           </code>

<code>apache2_module                  enables</code><code>/disables</code> <code>a module of the Apache2 webserver                                         </code>

<code>apk                             Manages apk packages                                                                       </code>

<code>apt                             Manages apt-packages                                                                       </code>

<code>apt_key                         Add or remove an apt key                                                                   </code>

<code>apt_repository                  Add and remove APT repositories                                                            </code>

<code>apt_rpm                         apt_rpm package manager                                                                    </code>

<code>assemble                        Assembles a configuration </code><code>file</code> <code>from fragments                                              </code>

<code>assert                          Fail with custom message                                                                   </code>

<code>at                              Schedule the execution of a </code><code>command</code> <code>or script </code><code>file</code> <code>via the at </code><code>command</code><code>.                     </code>

<code>authorized_key                  Adds or removes an SSH authorized key                                                      </code>

<code>azure                           create or terminate a virtual machine </code><code>in</code> <code>azure                                             </code>

<code>bigip_facts                     Collect facts from F5 BIG-IP devices                                                       </code>

<code>bigip_gtm_wide_ip               Manages F5 BIG-IP GTM wide ip                                                              </code>

<code>bigip_monitor_http              Manages F5 BIG-IP LTM</code>

查看模块参数:

29

30

31

32

33

34

<code>ansible-doc -s MODULE_NAME</code>

<code>例如:</code>

<code>[root@localhost ansible]</code><code># ansible-doc -s file</code>

<code>- name: Sets attributes of files</code>

<code>  </code><code>action: </code><code>file</code>

<code>      </code><code>follow                 </code><code># This flag indicates that filesystem links, if they exist, should be followed.</code>

<code>      </code><code>force                  </code><code># force the creation of the symlinks in two cases: the source file does not exist (but will appear later);</code>

<code>                               </code><code>the destination exists and is a </code><code>file</code> <code>(so, we need to unlink the </code><code>"path"</code> <code>file</code>

<code>                               </code><code>and create </code><code>symlink</code> <code>to the </code><code>"src"</code> <code>file</code> <code>in</code> <code>place of it).</code>

<code>      </code><code>group                  </code><code># name of the group that should own the file/directory, as would be fed to `chown'</code>

<code>      </code><code>mode                   </code><code># mode the file or directory should be. For those used to `/usr/bin/chmod' remember that modes are actually</code>

<code>                               </code><code>octal numbers (like 0644). Leaving off the leading zero will likely have</code>

<code>                               </code><code>unexpected results. As of version 1.8, the mode may be specified as a</code>

<code>                               </code><code>symbolic mode (</code><code>for</code> <code>example, `u+rwx</code><code>' or `u=rw,g=r,o=r'</code><code>).</code>

<code>      </code><code>owner                  </code><code># name of the user that should own the file/directory, as would be fed to `chown'</code>

<code>      </code><code>path=                  </code><code># path to the file being managed.  Aliases: `dest', `name'</code>

<code>      </code><code>recurse                </code><code># recursively set the specified file attributes (applies only to state=directory)</code>

<code>      </code><code>selevel                </code><code># level part of the SELinux file context. This is the MLS/MCS attribute, sometimes known as the `range'.</code>

<code>                               </code><code>`_default</code><code>' feature works as for `seuser'</code><code>.</code>

<code>      </code><code>serole                 </code><code># role part of SELinux file context, `_default' feature works as for `seuser'.</code>

<code>      </code><code>setype                 </code><code># type part of SELinux file context, `_default' feature works as for `seuser'.</code>

<code>      </code><code>seuser                 </code><code># user part of SELinux file context. Will default to system policy, if applicable. If set to `_default', it</code>

<code>                               </code><code>will use the `user' portion of the policy </code><code>if</code> <code>available</code>

<code>      </code><code>src                    </code><code># path of the file to link to (applies only to `state=link'). Will accept absolute, relative and nonexisting</code>

<code>                               </code><code>paths. Relative paths are not expanded.</code>

<code>      </code><code>state                  </code><code># If `directory', all immediate subdirectories will be created if they do not exist, since 1.7 they will be</code>

<code>                               </code><code>created with the supplied permissions. If `</code><code>file</code><code>', the </code><code>file</code> <code>will NOT be</code>

<code>                               </code><code>created </code><code>if</code> <code>it does not exist, see the [copy] or [template] module </code><code>if</code> <code>you</code>

<code>                               </code><code>want that behavior.  If `link', the symbolic link will be created or</code>

<code>                               </code><code>changed. Use `hard</code><code>' for hardlinks. If `absent'</code><code>, directories will be</code>

<code>                               </code><code>recursively deleted, and files or symlinks will be unlinked. If `</code><code>touch</code><code>' (new</code>

<code>                               </code><code>in</code> <code>1.4), an empty </code><code>file</code> <code>will be created </code><code>if</code> <code>the `path' does not exist, </code><code>while</code>

<code>                               </code><code>an existing </code><code>file</code> <code>or directory will receive updated </code><code>file</code> <code>access and</code>

<code>                               </code><code>modification </code><code>times</code> <code>(similar to the way `</code><code>touch</code><code>` works from the </code><code>command</code> <code>line)</code>

模块介绍:

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

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

参数:

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

dest:远程主机的绝对路径

owner:文件属主

group:文件属组

mode:文件权限

命令演示:

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

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

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

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

创建文件的符号链接:

src:指明源文件

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

<code>ansible pms -m </code><code>file</code> <code>-a </code><code>'src=/tmp/fstab dest=/srv/fstab state=link'</code>

<code>[root@localhost srv]</code><code># ll</code>

<code>lrwxrwxrwx 1 root root      10 Jul  6 14:08 fstab -&gt; </code><code>/tmp/fstab</code>

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

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

<code>[root@localhost ansible]</code><code># ansible all -m ping</code>

<code>172.16.206.134 | SUCCESS =&gt; {</code>

<code>    </code><code>"changed"</code><code>: </code><code>false</code><code>, </code>

<code>    </code><code>"ping"</code><code>: </code><code>"pong"</code>

<code>}</code>

<code>10.10.10.202 | SUCCESS =&gt; {</code>

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

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

<code>[root@localhost ansible]</code><code># ansible all -m command -a 'hostname'      </code>

<code>172.16.206.134 | SUCCESS | rc=0 &gt;&gt;</code>

<code>localhost.localdomain</code>

<code>10.10.10.202 | SUCCESS | rc=0 &gt;&gt;</code>

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

例如:

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

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

name:账户名

state:

          present:创建

          absent:删除   

group:指定用户的基本组

uid:指定uid

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

<code>[root@localhost ansible]</code><code># ansible pms -m user -a 'name=test state=present uid=306 group=root  system=yes'          </code>

<code>    </code><code>"changed"</code><code>: </code><code>true</code><code>, </code>

<code>    </code><code>"comment"</code><code>: </code><code>""</code><code>, </code>

<code>    </code><code>"createhome"</code><code>: </code><code>true</code><code>, </code>

<code>    </code><code>"group"</code><code>: 0, </code>

<code>    </code><code>"home"</code><code>: </code><code>"/home/test"</code><code>, </code>

<code>    </code><code>"name"</code><code>: </code><code>"test"</code><code>, </code>

<code>    </code><code>"shell"</code><code>: </code><code>"/bin/bash"</code><code>, </code>

<code>    </code><code>"state"</code><code>: </code><code>"present"</code><code>, </code>

<code>    </code><code>"system"</code><code>: </code><code>true</code><code>, </code>

<code>    </code><code>"uid"</code><code>: 306</code>

<code>[root@localhost srv]</code><code># id test</code>

<code>uid=306(</code><code>test</code><code>) gid=0(root) </code><code>groups</code><code>=0(root)</code>

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

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

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

name=:服务名

state=: 服务状态

    started:启动

    restarted:重启

    stopped:停止

    reloaded:重载

<code>[root@localhost ansible]</code><code># ansible pms -m service -a 'name=zabbix-agent state=started enabled=yes'</code>

<code>    </code><code>"enabled"</code><code>: </code><code>true</code><code>, </code>

<code>    </code><code>"name"</code><code>: </code><code>"zabbix-agent"</code><code>, </code>

<code>    </code><code>"state"</code><code>: </code><code>"started"</code>

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

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

<code>root@host1:</code><code>/tmp</code><code># ansible pms -m script -a '/tmp/echo.sh'</code>

<code>    </code><code>"rc"</code><code>: 0, </code>

<code>    </code><code>"stderr"</code><code>: </code><code>""</code><code>, </code>

<code>    </code><code>"stdout"</code><code>: </code><code>""</code><code>, </code>

<code>    </code><code>"stdout_lines"</code><code>: []</code>

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

<code>ansible pms -m shell -a </code><code>'ps -ef | grep tomcat'</code>        

<code>root      18569      1  0 13:09 pts</code><code>/0</code>    <code>00:00:36 </code><code>/usr/java/jdk1</code><code>.8.0_66</code><code>/bin/java</code> <code>-Djava.util.logging.config.</code><code>file</code><code>=</code><code>/tmp/catalina_base/conf/logging</code><code>.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -server -XX:PermSize=256M -XX:MaxPermSize=1024m -Xms512M -Xmx1024M -XX:MaxNewSize=256m -Djava.endorsed.</code><code>dirs</code><code>=</code><code>/usr/local/apache-tomcat-8</code><code>.0.29</code><code>/endorsed</code> <code>-classpath </code><code>/usr/local/apache-tomcat-8</code><code>.0.29</code><code>/bin/bootstrap</code><code>.jar:</code><code>/usr/local/apache-tomcat-8</code><code>.0.29</code><code>/bin/tomcat-juli</code><code>.jar -Dcatalina.base=</code><code>/tmp/catalina_base</code> <code>-Dcatalina.home=</code><code>/usr/local/apache-tomcat-8</code><code>.0.29 -Djava.io.tmpdir=</code><code>/tmp/catalina_base/temp</code> <code>org.apache.catalina.startup.Bootstrap start</code>

<code>root      19314  19313  0 17:24 pts</code><code>/1</code>    <code>00:00:00 </code><code>/bin/sh</code> <code>-c </code><code>ps</code> <code>-ef | </code><code>grep</code> <code>tomcat</code>

<code>root      19316  19314  0 17:24 pts</code><code>/1</code>    <code>00:00:00 </code><code>grep</code> <code>tomcat</code>

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:安装软件包

<code>[root@localhost tmp]</code><code># ansible pms -m yum -a 'name=/srv/jdk/jdk-8u66-linux-x64.rpm  state=present'  </code>

<code>    </code><code>"msg"</code><code>: </code><code>""</code><code>, </code>

<code>    </code><code>"results"</code><code>: []</code>

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文件

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

上面的命令表示将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

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

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

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

url:下载地址

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

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

<code>root@host1:</code><code>/srv</code><code># ansible pms -m get_url -a 'url=ftp://ftp.cheyaoshicorp.com/pub/临时文件/derby.init.sh dest=/tmp'      </code>

<code>    </code><code>"checksum_dest"</code><code>: null, </code>

<code>    </code><code>"checksum_src"</code><code>: </code><code>"770a432e9847e594e0154e31c906062585d571e0"</code><code>, </code>

<code>    </code><code>"dest"</code><code>: </code><code>"/tmp/derby.init.sh"</code><code>, </code>

<code>    </code><code>"gid"</code><code>: 0, </code>

<code>    </code><code>"group"</code><code>: </code><code>"root"</code><code>, </code>

<code>    </code><code>"md5sum"</code><code>: </code><code>"4564411c7e614859965c9ab5d76df22b"</code><code>, </code>

<code>    </code><code>"mode"</code><code>: </code><code>"0644"</code><code>, </code>

<code>    </code><code>"msg"</code><code>: </code><code>"OK (3934 bytes)"</code><code>, </code>

<code>    </code><code>"owner"</code><code>: </code><code>"root"</code><code>, </code>

<code>    </code><code>"size"</code><code>: 3934, </code>

<code>    </code><code>"src"</code><code>: </code><code>"/tmp/tmp5nqAsJ"</code><code>, </code>

<code>    </code><code>"state"</code><code>: </code><code>"file"</code><code>, </code>

<code>    </code><code>"uid"</code><code>: 0, </code>

<code>    </code><code>"url"</code><code>: </code><code>"ftp://ftp.cheyaoshicorp.com/pub/临时文件/derby.init.sh"</code>

本文转自 曾哥最爱 51CTO博客,原文链接:http://blog.51cto.com/zengestudy/1808752,如需转载请自行联系原作者