1、常用模块

1.1 ping

检测ansible连接的主机是否在线

  1. [root@hchost ansible]# ansible hw -m ping
  2. [WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
  3. 123.60.14.255 | SUCCESS => {
  4. "ansible_facts": {
  5. "discovered_interpreter_python": "/usr/bin/python"
  6. },
  7. "changed": false,
  8. "ping": "pong"
  9. }
  10. 124.71.193.36 | SUCCESS => {
  11. "ansible_facts": {
  12. "discovered_interpreter_python": "/usr/bin/python"
  13. },
  14. "changed": false,
  15. "ping": "pong"
  16. }
  17. 121.37.162.7 | SUCCESS => {
  18. "ansible_facts": {
  19. "discovered_interpreter_python": "/usr/bin/python"
  20. },
  21. "changed": false,
  22. "ping": "pong"
  23. }

1.2 group

该模块用于添加或者删除一个group

ARG:
    gid            #set group id
    name=        #set group name
    state        #set group state;the values is "present" or "absent"
    system        #if "yes",indicates that the group create is system group

example:
    1.ansible all -m group -a "gid=3000 name=mygroup state=present system=no"
    #创建一个gid为3000 名称为mygroup的组

    2.ansible all -m group -a "gid=3000 name=mygroup state=absent system=no"
    #删除一个gid为3000 名称为mygroup的组

注意:带等号为必填

1.3 user

#Manage user accounts,管理用户账户
ARG:
    comment        # 描述信息
    expires        # 过期时间
    group            # 基本组
    groups        # 附加组
    home            # 家目录路径
    password    # 密码
    shell            # 用户的shell
    state            # present or absent 创建还是删除
    system        # 系统用户
    uid                # 用户id
    move_home    # 两次创建一个用户,家目录不是同一路径,是否需要直接移动家目录
    generate_ssh_key    # 创建用户时,是否同时生成一对秘钥
    create_home                # 当创建用户或者家目录不存在时,是否为用户创建家目录

example:
    ansible all -m user -a "name=aa uid=5555 state=present shell=/sbin/nologin create_home=no"

1.4 copy

用于将文件复制到远程主机,同时支持给定内容生成文件和修改权限

#copy files to remote locations
ARG:
    backup        # 当文件内容发生改变后,在覆盖之前把源文件备份,备份文件包含时间信息
    content        # 和"src"不能同时使用,指定文件内容
    dest            # 将源文件复制到的远程主机的绝对路径
    directory_mode    # 递归设定目录的权限,默认为系统默认权限
    src                # 本机路径,如果复制目录,有"/"则表示复制目录下所有文件,否则递归复制整个目录
    remote_src    # 从远程主机复制到另一个远程主机
    group            # 设定远程主机文件夹的组名
    owner            # copy之后的属主,属组,权限
    mode      # 指定远程主机文件及文件夹的权限
  force   # 当目标主机包含该文件,但内容不同时,设为"yes",表示强制覆盖;设为"no",表示目标主机的目标位置不存在该文件才复制。默认为"yes"
    others  # 所有的 file 模块中的选项可以在这里使用

example:
    1.ansible all -m copy -a "src=/etc/passwd dest=/tmp/passwd.ansible mode=644"

    2.ansible all -m copy -a 'content="hello world\n" dest=/tmp/hello.txt mode=600'
    #用content生成的内容复制到目标主机名称为hello.txt

    3.ansible all -m copy -a 'src=/home/linxi dest=/tmp/'
    #复制改目录到指定目录下

#copy 可以创建文件

1.5 fetch

实现将远程服务器的文件(不支持直接拉取目录,可以先将目录打包 成文件)拉取到ansible管理主机


 ARG: 
  dest:用来存放文件的目录
  src: 在远程拉取的文件,并且必须是一个file,不能是目录
    fail_on_missing        #由于任何原因无法读取远程文件,则任务将失败

example:
    ansible all -m fetch -a 'dest=/tmp/backup.ansible? src=/etc/passwd'

#fetch复制文件时,会在本地创建目录,将多个主机上的文件都放在改目录下

1.6 command

#Execute commands on target 在目标主机上执行指令
 ARG:
    chdir         #切换目录执行command

example:
    ansible all -m command -a 'whoami'

#command模块不能解析shell语句

1.7 shell

#Execute shell commands on targets在目标主机上执行shell指令
 ARG:
     chdir        #切换目录执行command
     executable    #使用其他的shell解析

 example:
     ansible all -m shell -a 'echo "123465"|passwd --stdin root'

1.8 file

用于设置文件的属性,比如创建文件、创建链接文件、删除文件等

`group`  # 定义文件/目录的属组。后面可以加上**mode**:定义文件/目录的权限
`owner`  # 定义文件/目录的属主。后面==必须==跟上**path**:定义文件/目录的路径
`recurse`  # 递归设置文件的属性,只对目录有效,后面跟上**src**:被链接的源文件路径,只应用于**state=link**的情况
`dest`   # 被链接到的路径,只应用于**state=link**的情况
`src`        # 在目标主机上创建符号链接
`state`  # 状态,有以下选项:
    > `directory`:如果目录不存在,就创建目录
    > `file`:            即使文件不存在,也不会被创建
    > `link`:            创建软链接
    > `hard`:            创建硬链接
    > `touch`:        如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
    > `absent`:        删除目录、文件或者取消链接文件

example:
  # 在目标主机上创建目录
    ansible all -m file -a "path=/root/ansible state=directory mode=644 owner=root"
    # 在目标主机创建链接
    ansible all -m file -a "src=/etc/fstab path=/root/fatab.link state=link"

###不建议创建文件###

# 创建普通文件:
ansible all -m file -a "name=/tmp/passwd state=touch"
# 创建目录:
ansible all -m file -a "name=/tmp/web state=directory"
# 删除文件:
ansible all -m file -a "name=/tmp/passwd state=absent"
# 创建软连接:
ansible all -m file -a "src=/etc/passwd dest=/tmp/passwd state=link"
# 创建新文件,顺便设置文件属性:
ansible all -m file -a "name=/tmp/passwd mode=600 owner=lutixia state=touch"
# 修改文件属性:
ansible all -m file -a "name=/tmp/passwd mode=644
owner=root"
# 创建链接文件
ansible all -m file -a 'path=/data/bbb.jpg src=aaa.jpg state=link'

1.9 cron

*      *       *    *    * user-name command to be executed
*    *    *    *    *
-    -    -    -    -
|    |    |    |    |
|    |    |    |    +----- 星期中星期几 (0 - 7) (星期天 为0)
|    |    |    +---------- 月份 (1 - 12) 
|    |    +--------------- 一个月中的第几天 (1 - 31)
|    +-------------------- 小时 (0 - 23)
+------------------------- 分钟 (0 - 59)

f1 f2 f3 f4 f5 program
# 其中 f1 是表示分钟,f2 表示小时,f3 表示一个月份中的第几日,f4 表示月份,f5 表示一个星期中的第几天。program 表示要执行的程序。
# 当 f1 为 * 时表示每分钟都要执行 program,f2 为 * 时表示每小时都要执行程序,其馀类推
# 当 f1 为 a-b 时表示从第 a 分钟到第 b 分钟这段时间内要执行,f2 为 a-b 时表示从第 a 到第 b 小时都要执行,其馀类推
# 当 f1 为 */n 时表示每 n 分钟个时间间隔执行一次,f2 为 */n 表示每 n 小时个时间间隔执行一次,其馀类推
# 当 f1 为 a, b, c,... 时表示第 a, b, c,... 分钟要执行,f2 为 a, b, c,... 时表示第 a, b, c...个小时要执行,其馀类推
`day=`                     # 日应该运行的工作( 1-31, *, */2, )
`hour=`                 # 小时 ( 0-23, *, */2, )
`minute=`             # 分钟( 0-59, *, */2, )
`month=`                 # 月( 1-12, *, /2, )
`weekday=`             # 周 ( 0-6 for Sunday-Saturday,, )
`job=`                     # 指明运行的命令是什么
`name=`                 # 定时任务描述
`reboot`                 # 任务在重启时运行,不建议使用,建议使用special_time
`special_time`  # 特殊的时间范围,参数:reboot(重启时),annually(每年),monthly(每月),weekly(每周),daily(每天),hourly(每小时)
`state`                 # 指定状态,present表示添加定时任务,也是默认设置,==**absent表示删除定时任务**==
`user`                     # 以哪个用户的身份执行

 example:
     ansible all -m cron -a 'name="sync time" minute=*/1 job="/usr/sbin/ntpdate 192.168.10.11 &> /dev/null" state=present'

# 创建计划任务:
# 每隔一分钟执行一次echo
ansible all -m cron -a 'minute=* job="echo hc" name=test'
或者:
ansible all -m cron -a 'job="echo hc" name=test'
# 创建nginx的日志切割计划任务(切割的脚本也先创建好):
ansible all -m cron -a "special_time=daily job='bash /data/sh/nginx_log_logrotate.sh' name='nginx log logrotate'"
# 停止计划任务,crontab -l 查看是注释了该任务 
ansible all -m cron -a 'job="echo hc" name=test disabled=true'
# 删除计划任务:
ansible all -m cron -a 'job="echo hc" name=test state=absent'

10. yum

实现在远程主机上通过 yum源管理软件包

#Manages packages with the `yum' package manager 使用yum管理程序包
ARG:
    name                            # 软件包名称
    state                            # install(present or latest),remove(absten or removed)
  disable                        # 禁用某个安装仓库
    enable                        # 启用某个安装仓库
    disable_gpg_check    # 是否使用gpgcheck

`name=`                      # 所安装的包的名称
`state=`                  # present--->安装,latest--->安装最新的, absent---> 卸载软件
`update_cache`    # 强制更新yum的缓存
`conf_file`       # 指定远程yum安装时所依赖的配置文件(安装本地已有的包)。
`disable_pgp_check` # 是否禁止GPG checking,只用于`present`or `latest`。
`disablerepo`     # 临时禁止使用yum库。 只用于安装或更新时。
`enablerepo`      # 临时使用的yum库。只用于安装或更新时。 

example:
    ansible all -m yum -a "name=nginx state=present"

11. service

用于服务程序的管理

#Manage services
ARG:
    enable        # 开机自启
    name            # 服务名称
    pattern        # 服务不支持status,无法获取服务状态信息时,可以使用pattern过滤关键字
    runlevel    # 启动级别
    state            # 设置服务是启动还是停止,reloaded, restarted, started, stopped

`arguments` # 命令行提供额外的参数
`enabled`     # 设置开机启动。
`name=`         # 服务名称
`runlevel`     # 开机启动的级别,一般不用指定。
`sleep`         # 在重启服务的过程中,是否等待。如在服务关闭以后等待2秒再启动。(定义在剧本中。)
`state`            # 有四种状态,分别为:
    `started`        --->启动服务, 
    `stopped`        --->停止服务, 
    `restarted` --->重启服务, 
    `reloaded`    --->重载配置 

example:
    ansible all -m service -a "name=nginx state=started "

12. script

用于将本机的脚本在被管理端的机器上运行

example:
    ansible all -m script -a "/root/a.sh"        #直接指定本地脚本

13. lineinfile

相当于sed,可以修改文件内容

`path`:                     指定要操作的文件
`line`:                     指定文本内容
`regexp`:                 使用成则表达式匹配多行,当替换时只有最后匹配的那一行会被替换,当删除时所有匹配行都删除
`state`:                     删除(absent) 默认值为(present)
`backrefs`:             开启后项引用(yes) / 开启时如果正则没有匹配到行则不对文本进行修改,默认不开启时会将line对应内容写到文尾
`insertafter`:         可以在将文本插入到"指定行"之后,可以设置insertafter的值为EOF(插入到文尾)或者正则表达式(匹配到时插入到匹配行后,没有匹配到时则插入到文尾,当有backrefs参数时忽略此参数)
`insertbefore`:     在匹配行的前面插入信息
`backup`:                 是否在修改文件前对文件进行备份
`create`:                 当要操作文件不存在时,是否创建对应文件

# 替换文件内容,line是替换后的
[root@hchost ~]# ansible tx -m lineinfile -a "path=/etc/zabbix_agentd.conf regexp='Hostname=hc' line='# Hostname=hc'"

14. selinux

ansible hw -m selinux -a "state=disabled" -i hosts

[WARNING]: SELinux state change will take effect next reboot
192.168.0.10 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "configfile": "/etc/selinux/config",
    "msg": "Config SELinux state changed from 'enforcing' to 'disabled'",
    "policy": "targeted",
    "reboot_required": true,
    "state": "disabled"
}

15. setup

该模块主要用于收集信息,是通过调用facts组件来实现的。
facts组件是Ansible用于采集被管机器设备信息的一个功能,我们可以使用setup模块查机器的所有facts信息,可以使用filter来查看指定信息。整个facts信息被包装在一个JSON格式的数据结构中,ansible_facts是最上层的值。

[root@hchost ~]# ansible hw10 -m setup -a filter="*mem*"
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
124.71.193.36 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 143, 
        "ansible_memory_mb": {
            "nocache": {
                "free": 1236, 
                "used": 6584
            }, 
            "real": {
                "free": 143, 
                "total": 7820, 
                "used": 7677
            }, 
            "swap": {
                "cached": 0, 
                "free": 0, 
                "total": 0, 
                "used": 0
            }
        }, 
        "ansible_memtotal_mb": 7820, 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

保存我们所筛选的信息至我们的主机上,同时,文件名为我们被管制的主机的IP

ansible hw10 -m setup -a filter="*mem*" --tree /tmp/facts