天天看点

ansible常用模块介绍

Ansible常用模块介绍

一、Ansible常用模块

   1、Command 模块

   2、shell模块

   3、copy模块

   4、cron模块

   5、fetch模块

   6、file模块

   7、filesystem模块

   8、hostname模块

   9、pip模块

   10、yum模块

   11、service 模块

   12、user 模块

   13、script模块

   14、Template模块

1、Command 模块

    作用:在远程主机运行命令

         示例:ansible web -a "ls /root" #省略模块不写默认为command模块

    注意:该模块不支持一些特殊符号如`"<"', `">"', `"|"',`";"' and `"&"等如需利用这些符号需使用shell模块。

2、shell模块

    作用:在远程主机在shell进程下运行命令,支持shell特性,如管道等。

         示例:ansible web -m shell -a "echo 123456|passwd --stdin wang"

3、copy模块

    作用:在远程主机执行赋值操作文件

         示例:

    (1)        src=  dest=

    (2)        content=   dest= #content生成字符到目标文件

    其他 owner,group, mode (mode#修改权限)

        [root@centos7_1~]# ansible web -m copy -a "src=/etc/fstab dest=/apps/test"

         [root@centos7_1~]# ansible web -m copy -a"content='hello world1' dest=/apps/test"

         [root@centos7_1 ~]# ansible web -m copy-a "content='hello world1' mode=777 dest=/apps/test1"

4、cron模块

         作用:主要是用来对定时任务进行调度

         参数:

    name=               任务的描述

    minute=           分

    day=                   天

    weekday=          周

    hour=                 时

    Month                月

    job=                    需要执行的命令,必须状态为present

    state=                状态

            present:创建

            absent:删除       

5、fetch模块

    作用:fetches a file from remotenodes         (获取远程节点文件)

6、file模块

    作用:sets attributes of files( 设置文件的属性)

    用法:

    (1)    创建链接文件:*path=  src= state=link

    (2)    修改属性 : path=  owner= mode= grup=

    (3)    创建目录:path= state=directory

 7、filesystem模块

    作用:Makes file system on block device   能够在块设备上创建文件系统(慎用)

8、hostname模块

    作用:管理主机名称

9、pip模块

     作用:Manages Python librarydependencies.  #管理Python库依赖项。

10、yum模块

    作用:Manages packages with the`yum' package manager  #使用`yum'软件包管理器管理软件包

        name=    程序包名称,可以带版本号

       state=        状态

                present,latest   目前/最新版本

                installed  安装

       conf_file:指定yum配置文件

          示例:[root@centos7_1 ~]# ansibleweb -m yum -a "name=httpd state=latest"

11、service 模块

    作用:管理服务

         参数

       name=       服务名称

                started       启动

                stopped      停止

                            restarted    重启

                            enabled=    [yes|no] 是否随系统启动

                            runlevel=             运行级别

    示例:ansible web -m service -a"name=httpd state=started enabled=yes runlevel=5" #记得针对Centos7就

    不要使用这个模块了。

12、user 模块

    作用:管理用户帐号

    参数:

                  name=                 用户名

                  state=                  状态

                  system=   [yes|no]    是否为系统用户

                  shell=  默认shell类型       指定shell

                  uid=                     指定UID

                  home=                 指定家目录

group=                 指定属组

groups=               指定附加组

comment=          描述信息

13、script模块

执行脚本 (执行的是本地的脚本将其复制到远程在执行)

                   -a “/PATH/TO/SCRIPT_FILE” 会在远程自动给予权限并运行

14、Template模块

    基于模板方式生成一个文件复制到远程主机(template使用Jinjia2格式作为文件模版,进行文档内变量的替换的模块。它的每次使用都会被ansible标记为”changed”状态。)下一章节由详细应用介绍。

                   backup=     备份

                   src=            源文件

                   dest=          目标路径

                   owner=      属主

                   group=       主组

                   mode=       权限

继续阅读