Ansible(自动化运维工具常用模块)

Ansible常用模块

根据ansible官方文档,ansible的功能模块分为21类模块

一、ping模块

测试管理主机的连通性

[root@ansible ~]# ansible all -m ping
192.168.64.131 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.64.128 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.64.129 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

二、file模块

file模块主要用于远程主机上的文件操作

  • force:需要在两种情况下强制创建软链接,一种是源文件不存在但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no 

  • group:定义文件/目录的属组 

  • mode:定义文件/目录的权限

  • owner:定义文件/目录的属主

  • path:必选项,定义文件/目录的路径

  • recurse:递归的设置文件的属性,只对目录有效

  • src:要被链接的源文件的路径,只应用于state=link的情况

  • dest:被链接到的路径,只应用于state=link的情况 

  • state:  
  1. directory:如果目录不存在,创建目录
  2. file:即使文件不存在,也不会被创建
  3. link:创建软链接
  4. hard:创建硬链接
  5. touch:如果文件不存在,则会创建一个新的文件,果文件或目录已存在,则更新其最后修改时间 
  6. absent:删除目录、文件或者取消链接文件
[root@ansible ~]# ansible web -m file -a "src=/etc/hosts dest=/tmp/hosts state=link"  #web主机组的hosts文件链接到tmp
[root@ansible ~]# ansible web -m file -a "path=/tmp/hosts state=absent"      #取消连接文件hosts

[root@ansible ~]# ansible web -m file -a "path=/tmp/ansible_test.txt state=touch"   #创建文件
[root@ansible ~]# ansible web -m file -a "path=/tmp/ansible state=directory"    #创建目录

 三、setup模块

setup模块,主要用于获取主机信息

参数有:fact_path     用于局部可看事实的路径

     filter    如果提供了,只返回匹配这个shell样式(fnmatch)通配符的事实。

     gather_subset   如果提供了,则限制收集到给定子集的额外事实

    gather_timeout  为单独的事实收集设置默认的超时时间

[root@ansible ~]# ansible web -m setup -a 'filter=ansible_*_mb'     #查看主机内存信息

 四、copy模块

复制文件到远程主机

  • backup:在覆盖之前将原文件备份,备份文件包含时间信息。有两个选项:yes|no 

  • content:用于替代"src",可以直接设定指定文件的值 

  • dest:必选项。要将源文件复制到的远程主机的绝对路径,如果源文件是一个目录,那么该路径也必须是个目录 

  • directory_mode:递归的设定目录的权限,默认为系统默认权限

  • force:如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes

  • others:所有的file模块里的选项都可以在这里使用

  • src:要复制到远程主机的文件在本地的地址,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用"/"来结尾,则只复制目录里的内容,如果没有使用"/"来结尾,则包含目录在内的整个内容全部复制,类似于rsync。

[root@ansible ~]# ansible web -m copy -a "src=/root/php-7.2.4.tar.gz dest=/tmp"    #复制文件到远程主机目录
[root@ansible ~]# ansible web -m copy -a "src=/root/php-7.2.4.tar.gz dest=/tmp owner=www group=www mode=777"  #复制文件到远程主机目录,并修改文件的属性

五、service模块

  • arguments:给命令行提供一些选项 

  • enabled:是否开机启动 yes|no

  • name:必选项,服务名称 

  • pattern:定义一个模式,如果通过status指令来查看服务的状态时,没有响应,就会通过ps指令在进程中根据该模式进行查找,如果匹配到,则认为该服务依然在运行

  • runlevel:运行级别

  • sleep:如果执行了restarted,在则stop和start之间沉睡几秒钟

  • state:对当前服务执行启动,停止、重启、重新加载等操作(started,stopped,restarted,reloaded)

用于管理服务

    [root@ansible ~]#ansible web -m service -a "name=httpd state=started enabled=yes"
    [root@ansible ~]# asnible web -m service -a "name=nginx pattern=/usr/bin/nginx state=started"
    [root@ansible ~]# ansible web -m service -a "name=network state=restarted args=eth0"

六、yum模块

使用yum包管理器来管理软件包

  • config_file:yum的配置文件 

  • disable_gpg_check:关闭gpg_check 

  • disablerepo:不启用某个源 

  • enablerepo:启用某个源

  • name:要进行操作的软件包的名字,也可以传递一个url或者一个本地的rpm包的路径 

  • state:状态(present,absent,latest)

[root@ansible ~]# ansible web -m yum -a "name=htop state=installed"  #yum安装一个进程查看器

[root@ansible ~]# ansible web -m yum -a 'name="@Development tools" state=present'  #安装开发工具包

[root@ansible ~]# ansible web -m yum -a "name=nginx state=installed"  #yum安装nginx

七、scrip模块

scrip模块用与运行脚本

  chdir:   在运行脚本之前,将cd放到远程节点上的这个目录中
  creates:   一个文件名,当它已经存在时,这个步骤将不运行。
  decrypt:   此选项控制使用保管库对源文件进行自动解密
  free_form:  本地脚本文件的路径,后跟可选参数。 没有实际命名为“自由格式”的参数; 看例子!
  removes:    一个文件名,当它不存在时,这个步骤将不运行

[root@ansible ~]# ansible web -m script -a "/root/test.sh"  #直接运行测试脚本

[root@ansible ~]# ansible web -m script -a "/root/php_auto_install.sh"    #执行写好的php脚本

八、shell模块

执行shell命令,生产环境常用的一个模块

[root@ansible ~]# ansible web -m shell -a "df -h|awk NR==2"
192.168.64.129 | SUCCESS | rc=0 >>
/dev/mapper/centos-root   17G  4.1G   13G   24% /

192.168.64.131 | SUCCESS | rc=0 >>
/dev/mapper/centos-root   17G  2.6G   15G   16% /

[root@ansible ~]# ansible web -m shell -a "free -h"
192.168.64.131 | SUCCESS | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:           976M        157M        448M         10M        370M        596M
Swap:          2.0G        1.9M        2.0G

192.168.64.129 | SUCCESS | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:           976M        221M        306M        6.7M        447M        539M
Swap:          2.0G         64K        2.0G

[root@ansible ~]# ansible web -m shell -a "date"
192.168.64.131 | SUCCESS | rc=0 >>
2018年 06月 01日 星期五 15:30:13 CST

192.168.64.129 | SUCCESS | rc=0 >>
2018年 06月 01日 星期五 03:30:13 EDT

九、command模块

command是一个命令模块,ansible默认使用模块,和shell模块的区别在于不能使用管道符和变量。

[root@ansible ~]# ansible web -a "date"
192.168.64.131 | SUCCESS | rc=0 >>
2018年 06月 01日 星期五 15:34:07 CST

192.168.64.129 | SUCCESS | rc=0 >>
2018年 06月 01日 星期五 03:34:07 EDT

[root@ansible ~]# ansible web -a "df -h"
[root@ansible ~]# ansible web -a "free -h"
192.168.64.131 | SUCCESS | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:           976M        164M        336M         10M        474M        588M
Swap:          2.0G        1.9M        2.0G

192.168.64.129 | SUCCESS | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:           976M        179M        364M        6.6M        432M        588M
Swap:          2.0G        200K        2.0G

十、cron模块

用于管理计划任务

  • backup:对远程主机上的原任务计划内容修改之前做备份 

  • cron_file:如果指定该选项,则用该文件替换远程主机上的cron.d目录下的用户的任务计划 

  • day:日(1-31,*,*/2,……) 

  • hour:小时(0-23,*,*/2,……)  

  • minute:分钟(0-59,*,*/2,……) 

  • month:月(1-12,*,*/2,……) 

  • weekday:周(0-7,*,……)

  • job:要执行的任务,依赖于state=present 

  • name:该任务的描述 

  • special_time:指定什么时候执行,参数:reboot,yearly,annually,monthly,weekly,daily,hourly 

  • state:确认该任务计划是创建还是删除 

  • user:以哪个用户的身份执行

添加一个23点59分执行的shell脚本
[root@ansible ~]# ansible web -m cron -a 'name="cron test date" minute=59 hour=23 job="/bin/bash /root/test.sh &>/dev/null"'
查看结果

  [root@ngixn_lb-2 soft]# crontab -l
  #Ansible: cron test date
  59 23 * * * /bin/bash /root/test.sh &>/dev/null

十一、user模块和group模块

  • home:指定用户的家目录,需要与createhome配合使用

  • groups:指定用户的属组

  • uid:指定用的uid

  • password:指定用户的密码

  • name:指定用户名

  • createhome:是否创建家目录 yes|no

  • system:是否为系统用户

  • remove:当state=absent时,remove=yes则表示连同家目录一起删除,等价于userdel -r

  • state:是创建还是删除

  • shell:指定用户的shell环境

[root@ansible ~]# ansible web -m user -a "name=test password=$1$shj6o9in$53Vhb.kJ3kaT2yxOtEgAv0 system=yes"    #创建用户设置为系统用户,并设定密码
192.168.64.131 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 995, 
    "home": "/home/test", 
    "name": "test", 
    "password": "NOT_LOGGING_PASSWORD", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": true, 
    "uid": 997
}
192.168.64.129 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 992, 
    "home": "/home/test", 
    "name": "test", 
    "password": "NOT_LOGGING_PASSWORD", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": true, 
    "uid": 993
}
[root@ansible ~]# ansible web -m user -a "name=tiantian password=$1$shj6o9in$53Vhb.kJ3kaT2yxOtEgAv0 system=no"  #创建非系统用户  
192.168.64.131 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 1004, 
    "home": "/home/tiantian", 
    "name": "tiantian", 
    "password": "NOT_LOGGING_PASSWORD", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 1004
}
192.168.64.129 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 1006, 
    "home": "/home/tiantian", 
    "name": "tiantian", 
    "password": "NOT_LOGGING_PASSWORD", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 1006
}
[root@ansible ~]# ansible all -m group -a 'name=testgroup state=present'
 

十二、synchronize模块

用于rsync同步模块

  • archive: 归档,相当于同时开启recursive(递归)、links、perms、times、owner、group、-D选项都为yes ,默认该项为开启

  • checksum: 跳过检测sum值,默认关闭

  • compress:是否开启压缩

  • copy_links:复制链接文件,默认为no ,注意后面还有一个links参数

  • delete: 删除不存在的文件,默认no

  • dest:目录路径

  • dest_port:默认目录主机上的端口 ,默认是22,走的ssh协议

  • dirs:传速目录不进行递归,默认为no,即进行目录递归

  • rsync_opts:rsync参数部分

  • set_remote_user:主要用于/etc/ansible/hosts中定义或默认使用的用户与rsync使用的用户不同的情况

  • mode: push或pull 模块,push模的话,一般用于从本机向远程主机上传文件,pull 模式用于从远程主机上取文件

创建测试数据
[root@ansible ~]# mkdir -p /test_date/test{11..20} [root@ansible ~]# tree /test_date/ /test_date/ ├── test11 ├── test12 ├── test13 ├── test14 ├── test15 ├── test16 ├── test17 ├── test18 ├── test19 └── test20

[root@ansible ~]# ansible web -m synchronize -a "src=/test_date dest=/tmp/"
192.168.10.207 | SUCCESS => {
"changed": true,
"cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh=/usr/bin/ssh -S none -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null --out-format=<<CHANGED>>%i %n%L /test_date 192.168.10.207:/tmp/",
"msg": "cd+++++++++ test_date/\ncd+++++++++ test_date/test11/\ncd+++++++++ test_date/test12/\ncd+++++++++ test_date/test13/\ncd+++++++++ test_date/test14/\ncd+++++++++ test_date/test15/\ncd+++++++++ test_date/test16/\ncd+++++++++ test_date/test17/\ncd+++++++++ test_date/test18/\ncd+++++++++ test_date/test19/\ncd+++++++++ test_date/test20/\n",
"rc": 0,
"stdout_lines": [
"cd+++++++++ test_date/",
"cd+++++++++ test_date/test11/",
"cd+++++++++ test_date/test12/",
"cd+++++++++ test_date/test13/",
"cd+++++++++ test_date/test14/",
"cd+++++++++ test_date/test15/",
"cd+++++++++ test_date/test16/",
"cd+++++++++ test_date/test17/",
"cd+++++++++ test_date/test18/",
"cd+++++++++ test_date/test19/",
"cd+++++++++ test_date/test20/"
]
}
192.168.10.208 | SUCCESS => {
"changed": true,
"cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh=/usr/bin/ssh -S none -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null --out-format=<<CHANGED>>%i %n%L /test_date 192.168.10.208:/tmp/",
"msg": "cd+++++++++ test_date/\ncd+++++++++ test_date/test11/\ncd+++++++++ test_date/test12/\ncd+++++++++ test_date/test13/\ncd+++++++++ test_date/test14/\ncd+++++++++ test_date/test15/\ncd+++++++++ test_date/test16/\ncd+++++++++ test_date/test17/\ncd+++++++++ test_date/test18/\ncd+++++++++ test_date/test19/\ncd+++++++++ test_date/test20/\n",
"rc": 0,
"stdout_lines": [
"cd+++++++++ test_date/",
"cd+++++++++ test_date/test11/",
"cd+++++++++ test_date/test12/",
"cd+++++++++ test_date/test13/",
"cd+++++++++ test_date/test14/",
"cd+++++++++ test_date/test15/",
"cd+++++++++ test_date/test16/",
"cd+++++++++ test_date/test17/",
"cd+++++++++ test_date/test18/",
"cd+++++++++ test_date/test19/",
"cd+++++++++ test_date/test20/"
]
}

查看测试结果

[root@ansible ~]# ansible web -m shell -a "tree /tmp/test_date"
192.168.10.207 | SUCCESS | rc=0 >>
/tmp/test_date
├── test11
├── test12
├── test13
├── test14
├── test15
├── test16
├── test17
├── test18
├── test19
└── test20

10 directories, 0 files

192.168.10.208 | SUCCESS | rc=0 >>
/tmp/test_date
├── test11
├── test12
├── test13
├── test14
├── test15
├── test16
├── test17
├── test18
├── test19
└── test20

10 directories, 0 files

十三、get_url 模块

该模块主要用于从http、ftp、https服务器上下载文件(类似于wget)

  • sha256sum:下载完成后进行sha256 check;

  • timeout:下载超时时间,默认10s

  • url:下载的URL

  • url_password、url_username:主要用于需要用户名密码进行验证的情况

  • use_proxy:是事使用代理,代理需事先在环境变更中定义

   

测试下载一个NGINX源码包

[root@ansible ~]# ansible web -m get_url -a "url=http://nginx.org/download/nginx-1.13.10.tar.gz dest=/tmp/test"
192.168.10.207 | SUCCESS => {
"changed": true,
"checksum_dest": null,
"checksum_src": "1cb3cff21370aa71cc0f127ff26759f78bc08168",
"dest": "/tmp/test/nginx-1.13.10.tar.gz",
"gid": 0,
"group": "root",
"md5sum": "cf1f69bd5193b0a1945baecaa91f2709",
"mode": "0644",
"msg": "OK (1014863 bytes)",
"owner": "root",
"size": 1014863,
"src": "/tmp/tmp838ABE",
"state": "file",
"status_code": 200,
"uid": 0,
"url": "http://nginx.org/download/nginx-1.13.10.tar.gz"
}
192.168.10.208 | SUCCESS => {
"changed": true,
"checksum_dest": null,
"checksum_src": "1cb3cff21370aa71cc0f127ff26759f78bc08168",
"dest": "/tmp/test/nginx-1.13.10.tar.gz",
"gid": 0,
"group": "root",
"md5sum": "cf1f69bd5193b0a1945baecaa91f2709",
"mode": "0644",
"msg": "OK (1014863 bytes)",
"owner": "root",
"size": 1014863,
"src": "/tmp/tmplUlUyG",
"state": "file",
"status_code": 200,
"uid": 0,
"url": "http://nginx.org/download/nginx-1.13.10.tar.gz"
}

检查下载结果

[root@ansible ~]# ansible web -m shell -a "tree /tmp/test"
192.168.10.208 | SUCCESS | rc=0 >>
/tmp/test
└── nginx-1.13.10.tar.gz

0 directories, 1 file

192.168.10.207 | SUCCESS | rc=0 >>
/tmp/test
└── nginx-1.13.10.tar.gz

0 directories, 1 file

十四、unarchive模块

用于解压文件

  • copy:在解压文件之前,是否先将文件复制到远程主机,默认为yes。若为no,则要求目标主机上压缩包必须存在。

  • creates:指定一个文件名,当该文件存在时,则解压指令不执行

  • dest:远程主机上的一个路径,即文件解压的路径 

  • grop:解压后的目录或文件的属组

  • list_files:如果为yes,则会列出压缩包里的文件,默认为no,2.0版本新增的选项

  • mode:解决后文件的权限

  • src:如果copy为yes,则需要指定压缩文件的源路径 

  • owner:解压后文件或目录的属主

[root@ansible soft]# ansible web -m unarchive -a "src=/tmp/test/nginx-1.13.10.tar.gz dest=/tmp/test/ copy=no mode=0755"
192.168.10.208 | SUCCESS => {
    "changed": true, 
    "dest": "/tmp/test/", 
    "extract_results": {
        "cmd": [
            "/usr/bin/gtar", 
            "--extract", 
            "-C", 
            "/tmp/test/", 
            "-z", 
            "-f", 
            "/tmp/test/nginx-1.13.10.tar.gz"
        ], 
        "err": "", 
        "out": "", 
        "rc": 0
    }, 
    "gid": 0, 
    "group": "root", 
    "handler": "TgzArchive", 
    "mode": "0755", 
    "owner": "root", 
    "size": 55, 
    "src": "/tmp/test/nginx-1.13.10.tar.gz", 
    "state": "directory", 
    "uid": 0
}
192.168.10.207 | SUCCESS => {
    "changed": true, 
    "dest": "/tmp/test/", 
    "extract_results": {
        "cmd": [
            "/usr/bin/gtar", 
            "--extract", 
            "-C", 
            "/tmp/test/", 
            "-z", 
            "-f", 
            "/tmp/test/nginx-1.13.10.tar.gz"
        ], 
        "err": "", 
        "out": "", 
        "rc": 0
    }, 
    "gid": 0, 
    "group": "root", 
    "handler": "TgzArchive", 
    "mode": "0755", 
    "owner": "root", 
    "size": 55, 
    "src": "/tmp/test/nginx-1.13.10.tar.gz", 
    "state": "directory", 
    "uid": 0
}

查看解压结果:

[root@ansible soft]# ansible web -m shell -a "ls -l /tmp/test"
192.168.10.208 | SUCCESS | rc=0 >>
总用量 992
drwxr-xr-x 8 www www 158 3月 20 23:58 nginx-1.13.10
-rw-r--r-- 1 root root 1014863 6月 5 23:43 nginx-1.13.10.tar.gz


192.168.10.207 | SUCCESS | rc=0 >>
总用量 992
drwxr-xr-x 8 www www 158 3月 20 23:58 nginx-1.13.10
-rw-r--r-- 1 root root 1014863 6月 5 23:43 nginx-1.13.10.tar.gz

 

总结常用命令:

[root@ansible ~]# ansible all -m ping    #测试主机网络连通性
[root@ansible ~]# ansible web -m file -a "src=/etc/hosts dest=/tmp/hosts state=link"  #web主机组的hosts文件链接到tmp
[root@ansible ~]# ansible web -m file -a "path=/tmp/hosts state=absent"      #取消连接文件hosts
[root@ansible ~]# ansible web -m file -a "path=/tmp/ansible_test.txt state=touch"   #创建文件
[root@ansible ~]# ansible web -m file -a "path=/tmp/ansible state=directory"    #创建目录
[root@ansible ~]# ansible web -m setup -a 'filter=ansible_*_mb'     #查看主机内存信息
[root@ansible ~]# ansible web -m copy -a "src=/root/php-7.2.4.tar.gz dest=/tmp"    #复制文件到远程主机目录
[root@ansible ~]# ansible web -m copy -a "src=/root/php-7.2.4.tar.gz dest=/tmp owner=www group=www mode=777"  #复制文件到远程主机目录,并修改文件的属性
[root@ansible ~]# ansible web -m service -a "name=httpd state=started enabled=yes"
[root@ansible ~]# asnible web -m service -a "name=nginx pattern=/usr/bin/nginx state=started"
[root@ansible ~]# ansible web -m service -a "name=network state=restarted args=eth0"
[root@ansible ~]# ansible web -m yum -a "name=htop state=installed"  #yum安装一个进程查看器
[root@ansible ~]# ansible web -m yum -a 'name="@Development tools" state=present'  #安装开发工具包
[root@ansible ~]# ansible web -m yum -a "name=nginx state=installed"  #yum安装nginx
[root@ansible ~]# ansible web -m script -a "/root/test.sh"  #直接运行测试脚本
[root@ansible ~]# ansible web -m script -a "/root/php_auto_install.sh"    #执行写好的php脚本
[root@ansible ~]# ansible web -m shell -a "df -h|awk NR==2"
[root@ansible ~]# ansible web -a "df -h"    #默认command模块
[root@ansible ~]# ansible web -a "free -h"  #默认command模块
[root@ansible ~]# ansible web -m cron -a 'name="cron test date" minute=59 hour=23 job="/bin/bash /root/test.sh 
[root@ansible ~]# ansible web -m user -a "name=test password=$1$shj6o9in$53Vhb.kJ3kaT2yxOtEgAv0 system=yes"    #创建用户设置为系统用户,并设定密码
[root@ansible ~]# ansible web -m synchronize -a "src=/test_date dest=/tmp/"    #rsync传输文件
[root@ansible ~]# ansible web -m get_url -a "url=http://nginx.org/download/nginx-1.13.10.tar.gz dest=/tmp/test"  #get_url下载软件源码包
[root@ansible ~]# ansible web -m unarchive -a "src=/tmp/test/nginx-1.13.10.tar.gz dest=/tmp/test/ copy=no mode=0755"  #解压远端主机压缩包
原文地址:https://www.cnblogs.com/51wansheng/p/9120318.html