自动化运维工具----Ansible

**

Ansible

**
Ansible 是一款使用 Python 开发的自动化管理工具,由 Michael DeHaan 发起、开发、创建,先已被 Redhat 收购。Ansible 在 GitHub 上也是排名前 10 的 Python 项目,可以看到其火热程度。
官网对 Ansible 的定义是:Ansible is the simplest way to automate apps and IT infrastructure. 即 Ansible 是一个实现应用程序和 IT 基础组件自动化的东东,这里它们自信的用了 simplest,最简单便捷!
**

Ansible 的应用领域

**

应用部署
配置管理
任务流编排

Ansible 主要组件

Ansible:Ansible 的核心程序
Host Inventory:记录了每一个由 Ansible 管理的主机信息,信息包括 ssh 端口,root 帐号密码,ip 地址等等。
Playbooks:YAML 格式文件,多个任务定义在一个文件中,使用时可以统一调用,剧本用来定义那些主机需要调用那些模块来完成的功能。
Core Modules:Ansible 执行任何管理任务都不是由 Ansible 自己完成,而是由核心模块完成;Ansible 管理主机之前,先调用 core Modules 中的模块,然后指明管理 Host Inventory 中的主机,就可以完成管理主机。
Custom Modules:自定义模块,完成 Ansible 核心模块无法完成的功能,此模块支持任何语言编写。
Connection Plugins:连接插件,Ansible 和 Host 通信使用。

**

配置使用环境

**

[root@server1 rhel7]# ls
ansible-2.1.0.0-1.el7.noarch.rpm       python2-paramiko-1.16.1-1.el7.noarch.rpm
libtomcrypt-1.17-23.el7.x86_64.rpm     python-httplib2-0.7.7-3.el7.noarch.rpm
libtommath-0.42.0-4.el7.x86_64.rpm     python-keyczar-0.71c-2.el7.noarch.rpm
python2-crypto-2.6.1-9.el7.x86_64.rpm  sshpass-1.05-5.el7.x86_64.rpm
python2-ecdsa-0.13-4.el7.noarch.rpm
[root@server1 rhel7]# yum install  * -y

[root@server1 rhel7]# ansible --version
ansible 2.1.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides

**

配置

**

Ansible配置文件部分参数

nventory = /etc/ansible/hosts      #这个参数表示资源清单inventory文件的位置
    library = /usr/share/ansible        #指向存放Ansible模块的目录,支持多个目录方式,只要用冒号(:)隔开就可以
    forks = 5       #并发连接数,默认为5
    sudo_user = root        #设置默认执行命令的用户
    remote_port = 22        #指定连接被管节点的管理端口,默认为22端口,建议修改,能够更加安全
    host_key_checking = False       #设置是否检查SSH主机的密钥,值为True/False。关闭后第一次连接不会提示配置实例
    timeout = 60        #设置SSH连接的超时时间,单位为秒
    log_path = /var/log/ansible.log     #指定一个存储ansible日志的文件(默认不记录日志)

Ansible Inventory 内置参数(用于定义清单)
参数  用途
ansible_ssh_host    将要连接的远程主机名ssh地址
ansible_ssh_port    ssh端口
ansible_ssh_user    ssh 用户名
ansible_ssh_pass    ssh 密码
ansible_sudo    sudo 用户
ansible_sudo_pass   sudo 密码


**修改[root@server1 rhel7]# vim  /etc/ansible/hosts** 


# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses
#   - A hostname/ip can be a member of multiple groups
***********************

添加一个servers组用来测试
[servers]

172.25.15.250
172.25.15.[21:22]

[servers:vars]  ***定义组的属性
ansible_ssh_user='root'
ansible_ssh_pass='westos'


测试 修改ansible配置文件 打开 host_key
# uncomment this to disable SSH key host checking
host_key_checking = False
结果如下
这里 ansible 命令后面跟组名,也可以指定某个主机,-m 表示使用哪个模块。
需要注意的是, ping 操作并不是平常所说的 ping,而是进行 ssh 连通性检查。
[root@server1 rhel7]# ansible  servers -m ping
172.25.15.22 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
172.25.15.21 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
172.25.15.250 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

测试远程command  
[root@server1 rhel7]# ansible servers -m command -a 'whoami'
172.25.15.21 | SUCCESS | rc=0 >>
root
172.25.15.22 | SUCCESS | rc=0 >>
root
172.25.15.250 | SUCCESS | rc=0 >>
root
这里使用 command 模块在远程主机上运行 whoami,-a 用于指定模块参数。也可以使用 shell 模块:
[root@server1 rhel7]# ansible servers -m shell -a 'whoami'
172.25.15.22 | SUCCESS | rc=0 >>
root
172.25.15.21 | SUCCESS | rc=0 >>
root
172.25.15.250 | SUCCESS | rc=0 >>
root


[root@server1 ~]# ansible servers -m command -a 'ss -ntl'
172.25.15.22 | SUCCESS | rc=0 >>
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128          *:22                       *:*                  
LISTEN     0      100    127.0.0.1:25                       *:*                  
LISTEN     0      128         :::22                      :::*                  
LISTEN     0      100        ::1:25                      :::*                  

172.25.15.21 | SUCCESS | rc=0 >>
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128          *:22                       *:*                  
LISTEN     0      100    127.0.0.1:25                       *:*                  
LISTEN     0      128         :::22                      :::*                  
LISTEN     0      100        ::1:25                      :::*                  

实验环境配置无密码登陆

[root@server2 ~]# ssh-keygen 
[root@server2 ~]# ssh-copy-id [email protected]

**

command的使用

**

命令模块接受命令名称,后面是空格分隔的列表参数。给定的命令将在所有选定的节点上执行。它不会通过shell进行处理,比如$HOME和操作如"<"">""|"";""&" 工作(需要使用(shell)模块实现这些功能)。注意,该命令不支持| 管道命令。
  下面来看一看该模块下常用的几个命令:
    chdir       # 在执行命令之前,先切换到该目录
    executable # 切换shell来执行命令,需要使用命令的绝对路径
    free_form   # 要执行的Linux指令,一般使用Ansible的-a参数代替。
    creates  # 一个文件名,当这个文件存在,则该命令不执行,可以
    用来做判断
    removes # 一个文件名,这个文件不存在,则该命令不执行


测试

[root@server1 ~]# ansible servers -m command -a 'chdir=/mnt/ ls' 
172.25.15.21 | SUCCESS | rc=0 >>
cmdline-jmxclient-0.10.3.jar
zabbix
zabbix-api
zabbix-api.sh
zabbix-java-gateway-3.4.6-1.el7.x86_64.rpm
zabbix_java_gateway.conf

172.25.15.22 | SUCCESS | rc=0 >>
rhel7
zabbix

**

shell模块的使用

**

shell模块可以在远程主机上调用shell解释器运行命令,支持shell的各种功能,例如管道等。

[root@server1 ~]# ansible servers -m shell -a 'cat /etc/passwd |grep "root"'
172.25.15.22 | SUCCESS | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

172.25.15.21 | SUCCESS | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

172.25.15.250 | SUCCESS | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

**

copy模块

**

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

src    #被复制到远程主机的本地文件。可以是绝对路径,也可以是相对路径。如果路径是一个目录,则会递归复制,用法类似于"rsync"
content   #用于替换"src",可以直接指定文件的值
dest    #必选项,将源文件复制到的远程主机的绝对路径
backup   #当文件内容发生改变后,在覆盖之前把源文件备份,备份文件包含时间信息
directory_mode    #递归设定目录的权限,默认为系统默认权限
force    #当目标主机包含该文件,但内容不同时,设为"yes",表示强制覆盖;设为"no",表示目标主机的目标位置不存在该文件才复制。默认为"yes"
others    #所有的 file 模块中的选项可以在这里使用


测试

[root@server1 mnt]# ansible servers -m copy -a 'src=/mnt/file1 dest=/mnt/file1' 
172.25.15.22 | SUCCESS => {
    "changed": false, 
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 
    "dest": "/mnt/file1", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "path": "/mnt/file1", 
    "size": 0, 
    "state": "file", 
    "uid": 0
}
172.25.15.21 | SUCCESS => {
    "changed": true, 
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 
    "dest": "/mnt/file1", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e", 
    "mode": "0644", 
    "owner": "root", 
    "size": 0, 
    "src": "/root/.ansible/tmp/ansible-tmp-1534386247.74-117390907672439/source", 
    "state": "file", 
    "uid": 0
}
再远程主机查看

[root@server2 mnt]# ll  file1
-rw-r--r-- 1 root root 0 816 10:24 file1


复制文件并且修改权限
[root@server1 mnt]# ansible servers -m copy -a 'src=/mnt/file2 dest=/mnt/file2  mode=777 ' 
172.25.15.21 | SUCCESS => {
    "changed": true, 
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 
    "dest": "/mnt/file2", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e", 
    "mode": "0777", 
    "owner": "root", 
    "size": 0, 
    "src": "/root/.ansible/tmp/ansible-tmp-1534386413.23-276115183740525/source", 
    "state": "file", 
    "uid": 0
}
172.25.15.22 | SUCCESS => {
    "changed": true, 
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 
    "dest": "/mnt/file2", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "path": "/mnt/file2", 
    "size": 0, 
    "state": "file", 
    "uid": 0
}

远程主机查看
[root@server1 mnt]# ansible servers -m shell -a 'ls -l /mnt/'
172.25.15.21 | SUCCESS | rc=0 >>
total 784
-rwxr-xr-x  1 root root    284 Aug 14 17:31 1.sh
-rwxr-xr-x  1 root root    447 Aug 14 17:34 2.sh

-rw-r--r--  1 root root      0 Aug 16 10:24 file1
-rwxrwxrwx  1 root root      0 Aug 16 10:26 file2


172.25.15.22 | SUCCESS | rc=0 >>
total 8
-rw-r--r-- 1 root root    0 Aug 16 10:23 file1
-rwxrwxrwx 1 root root    0 Aug 16 10:26 file2

**

file模块

**

  该模块主要用于设置文件的属性,比如创建文件、创建链接文件、删除文件等。
  下面是一些常见的命令:

    force  #需要在两种情况下强制创建软链接,一种是源文件不存在,但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no
    group  #定义文件/目录的属组。后面可以加上mode:定义文件/目录的权限
    owner  #定义文件/目录的属主。后面必须跟上path:定义文件/目录的路径
    recurse  #递归设置文件的属性,只对目录有效,后面跟上src:被链接的源文件路径,只应用于state=link的情况
    dest  #被链接到的路径,只应用于state=link的情况
    state  #状态,有以下选项:

    directory:如果目录不存在,就创建目录
    file:即使文件不存在,也不会被创建
    link:创建软链接
    hard:创建硬链接
    touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
    absent:删除目录、文件或者取消链接文件

测试  创建的为目录!!!
[root@server1 mnt]# ansible servers -m file -a 'path=/mnt/file3 state=directory'
172.25.15.21 | SUCCESS => {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/mnt/file3", 
    "size": 6, 
    "state": "directory", 
    "uid": 0
}
172.25.15.22 | SUCCESS => {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/mnt/file3", 
    "size": 6, 
    "state": "directory", 
    "uid": 0
}

[root@server1 mnt]# ansible servers -m shell -a 'ls -l /mnt/'
172.25.15.22 | SUCCESS | rc=0 >>
total 8
-rw-r--r-- 1 root root    0 Aug 16 10:23 file1
-rwxrwxrwx 1 root root    0 Aug 16 10:26 file2
drwxr-xr-x 2 root root    6 Aug 16 10:33 file3
drwxrwxr-x 2 root root 4096 Aug 16 09:17 rhel7
drwxrwxr-x 5 root root 4096 Aug 15 16:29 zabbix

172.25.15.21 | SUCCESS | rc=0 >>
total 784

-rw-r--r--  1 root root      0 Aug 16 10:24 file1
-rwxrwxrwx  1 root root      0 Aug 16 10:26 file2
drwxr-xr-x  2 root root      6 Aug 16 10:33 file3


删除文件
[root@server1 mnt]# ansible servers -m file -a 'path=/mnt/file1 state=absent'
172.25.15.21 | SUCCESS => {
    "changed": true, 
    "path": "/mnt/file1", 
    "state": "absent"
}
172.25.15.22 | SUCCESS => {
    "changed": true, 
    "path": "/mnt/file1", 
    "state": "absent"
}
172.25.15.250 | SUCCESS => {
    "changed": true, 
    "path": "/mnt/file1", 
    "state": "absent"
}

[root@server1 mnt]# ansible servers -m shell -a 'ls -l /mnt/'
172.25.15.21 | SUCCESS | rc=0 >>
total 784
-rwxrwxrwx  1 root root      0 Aug 16 10:26 file2
drwxr-xr-x  2 root root      6 Aug 16 10:33 file3
-rw-r--r--  1 root root      0 Aug 16 10:43 file4

172.25.15.22 | SUCCESS | rc=0 >>
total 8
-rwxrwxrwx 1 root root    0 Aug 16 10:26 file2
drwxr-xr-x 2 root root    6 Aug 16 10:33 file3
-rw-r--r-- 1 root root    0 Aug 16 10:43 file4
drwxrwxr-x 2 root root 4096 Aug 16 09:17 rhel7
drwxrwxr-x 5 root root 4096 Aug 15 16:29 zabbix

**

fetch 模块

**

  该模块用于从远程某主机获取(复制)文件到本地。
  有两个选项:

    dest:用来存放文件的目录
    src:在远程拉取的文件,并且必须是一个file,不能是目录
测试
[root@server1 mnt]# ansible 172.25.15.21 -m fetch -a 'src=/mnt/test1 dest=/mnt'  
172.25.15.21 | SUCCESS => {
    "changed": true, 
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 
    "dest": "/mnt/172.25.15.21/mnt/test1", 
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e", 
    "remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 
    "remote_md5sum": null
}
文件保存格式如下
[root@server1 mnt]# ls
172.25.15.21  file2  file3  file4  rhel7  zabbix
[root@server1 mnt]# cd  172.25.15.21/
[root@server1 172.25.15.21]# ls
mnt
[root@server1 172.25.15.21]# cd mnt/
[root@server1 mnt]# ls
test1

**

cron 模块

**

  该模块适用于管理cron计划任务的。
  其使用的语法跟我们的crontab文件中的语法一致,同时,可以指定以下选项:

    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 # 以哪个用户的身份执行


[root@server1 mnt]# ansible servers -m cron -a 'name="show time" minute=*/5 job="/bin/date  &> /dev/null"'
172.25.15.21 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "show time"
    ]
}
172.25.15.22 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "show time"
    ]
}
172.25.15.250 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "show time"
    ]
}

[root@server1 mnt]# ansible servers -m shell -a 'crontab -l'
172.25.15.21 | SUCCESS | rc=0 >>
#Ansible: show time
*/5 * * * * /bin/date  &> /dev/null

172.25.15.22 | SUCCESS | rc=0 >>
#Ansible: show time
*/5 * * * * /bin/date  &> /dev/null

172.25.15.250 | SUCCESS | rc=0 >>
#Ansible: show time
*/5 * * * * /bin/date  &> /dev/null


删除定时任务
[root@server1 mnt]# ansible servers -m cron -a 'name="show time" minute=*/5 job="/bin/date  &> /dev/null"  state=absent '
172.25.15.21 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": []
}
172.25.15.22 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": []
}
172.25.15.250 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": []
}
[root@server1 mnt]# ansible servers -m shell -a 'crontab -l'
172.25.15.21 | SUCCESS | rc=0 >>
172.25.15.22 | SUCCESS | rc=0 >>
172.25.15.250 | SUCCESS | rc=0 >>

**

service 模块

**

该模块用于服务程序的管理。
  其主要选项如下:

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

[root@server1 mnt]# ansible 172.25.15.21 -m service -a 'name=mariadb state=started enabled=true' 
172.25.15.21 | SUCCESS => {
    "changed": true, 
    "enabled": true, 
    "name": "mariadb", 
    "state": "started"
}
看到3306端口打开 成功
[root@server1 mnt]# ansible 172.25.15.21 -m shell -a 'ss -ntl'
172.25.15.21 | SUCCESS | rc=0 >>
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      50           *:3306                     *:*                  
LISTEN     0      128          *:22                       *:*                  
LISTEN     0      100    127.0.0.1:25                       *:*                  

关闭服务
[root@server1 mnt]# ansible 172.25.15.21 -m service -a 'name=mariadb state=stopped ' 
172.25.15.21 | SUCCESS => {
    "changed": true, 
    "name": "mariadb", 
    "state": "stopped"
}
[root@server1 mnt]# ansible 172.25.15.21 -m shell -a 'ss -ntl'
172.25.15.21 | SUCCESS | rc=0 >>
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128          *:22                       *:*                  
LISTEN     0      100    127.0.0.1:25                       *:*                  
LISTEN     0      128         :::22                      :::*                  
LISTEN     0      100        ::1:25                      :::*                 

**

user 模块

**

  该模块主要是用来管理用户账号。
  其主要选项如下:

    comment  # 用户的描述信息
    createhome  # 是否创建家目录
    force  # 在使用state=absent时, 行为与userdel –force一致.
    group  # 指定基本组
    groups  # 指定附加组,如果指定为(groups=)表示删除所有组
    home  # 指定用户家目录
    move_home  # 如果设置为home=时, 试图将用户主目录移动到指定的目录
    name  # 指定用户名
    non_unique  # 该选项允许改变非唯一的用户ID值
    password  # 指定用户密码
    remove  # 在使用state=absent时, 行为是与userdel –remove一致
    shell  # 指定默认shell
    state  # 设置帐号状态,不指定为创建,指定值为absent表示删除
    system  # 当创建一个用户,设置这个用户是系统用户。这个设置不能更改现有用户
    uid  # 指定用户的uid

创建用户
[root@server1 mnt]# ansible servers -m user -a 'name=bobo uid=11111'
172.25.15.21 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "createhome": true, 
    "group": 11111, 
    "home": "/home/bobo", 
    "name": "bobo", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 11111
}
172.25.15.22 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "createhome": true, 
    "group": 11111, 
    "home": "/home/bobo", 
    "name": "bobo", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 11111
}

[root@server1 mnt]# ansible servers -m shell -a 'cat /etc/passwd |grep bobo'
172.25.15.21 | SUCCESS | rc=0 >>
bobo:x:11111:11111::/home/bobo:/bin/bash

172.25.15.22 | SUCCESS | rc=0 >>
bobo:x:11111:11111::/home/bobo:/bin/bash

172.25.15.250 | SUCCESS | rc=0 >>
bobo:x:11111:11111::/home/bobo:/bin/bash

删除用户
[root@server1 mnt]# ansible servers -m user -a 'name=bobo state=absent'
172.25.15.22 | SUCCESS => {
    "changed": true, 
    "force": false, 
    "name": "bobo", 
    "remove": false, 
    "state": "absent"
}
172.25.15.21 | SUCCESS => {
    "changed": true, 
    "force": false, 
    "name": "bobo", 
    "remove": false, 
    "state": "absent"
}
172.25.15.250 | SUCCESS => {
    "changed": true, 
    "force": false, 
    "name": "bobo", 
    "remove": false, 
    "state": "absent"
}
[root@server1 mnt]# ansible servers -m shell -a 'cat /etc/passwd |grep bobo'
172.25.15.250 | FAILED | rc=1 >>

172.25.15.21 | FAILED | rc=1 >>

script 模块

  该模块用于将本机的脚本在被管理端的机器上运行。
  该模块直接指定脚本的路径即可,我们通过例子来看一看到底如何使用的:
  首先,我们写一个脚本,并给其加上执行权限:

写一个测试脚本

    #!/bin/bash

    date >> /tmp/disk_total.log
    df -lh >> /tmp/disk_total.log 


[root@server1 mnt]# ansible servers  -m script -a '/mnt/172.25.15.21/mnt/1.sh'
172.25.15.21 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "", 
    "stdout": "", 
    "stdout_lines": []
}
172.25.15.22 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "", 
    "stdout": "", 
    "stdout_lines": []
}
172.25.15.250 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "", 
    "stdout": "", 
    "stdout_lines": []


查看运行结果
[root@server1 mnt]# ansible servers -m shell -a 'cat /tmp/disk_total.log'
172.25.15.21 | SUCCESS | rc=0 >>
Thu Aug 16 11:53:48 CST 2018
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   18G  1.6G   16G   9% /
devtmpfs               486M     0  486M   0% /dev
tmpfs                  497M     0  497M   0% /dev/shm
tmpfs                  497M   13M  484M   3% /run
tmpfs                  497M     0  497M   0% /sys/fs/cgroup
/dev/sda1              497M  132M  366M  27% /boot
/dev/mapper/rhel-home  2.0G   33M  2.0G   2% /home
tmpfs                  100M     0  100M   0% /run/user/0

172.25.15.22 | SUCCESS | rc=0 >>
Thu Aug 16 11:50:15 CST 2018
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   18G  1.6G   16G   9% /
devtmpfs               486M     0  486M   0% /dev
tmpfs                  497M     0  497M   0% /dev/shm
tmpfs                  497M   13M  484M   3% /run
tmpfs                  497M     0  497M   0% /sys/fs/cgroup
/dev/sda1              497M  132M  366M  27% /boot
/dev/mapper/rhel-home  2.0G   33M  2.0G   2% /home
tmpfs                  100M     0  100M   0% /run/user/0
Thu Aug 16 11:53:48 CST 2018
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   18G  1.6G   16G   9% /
devtmpfs               486M     0  486M   0% /dev
tmpfs                  497M  152K  497M   1% /dev/shm
tmpfs                  497M   13M  484M   3% /run
tmpfs                  497M     0  497M   0% /sys/fs/cgroup

**

setup 模块

**

  该模块主要用于收集信息,是通过调用facts组件来实现的。
  facts组件是Ansible用于采集被管机器设备信息的一个功能,我们可以使用setup模块查机器的所有facts信息,可以使用filter来查看指定信息。整个facts信息被包装在一个JSON格式的数据结构中,ansible_facts是最上层的值。
  facts就是变量,内建变量 。每个主机的各种信息,cpu颗数、内存大小等会存在facts中的某个变量中。调用后返回很多对应主机的信息,在后面的操作中可以根据不同的信息来做不同的操作。如redhat系列用yum安装,而debian系列用apt来安装软件。

查看内存信息

[root@server1 mnt]# ansible servers -m setup -a 'filter="*mem*"' 
172.25.15.21 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 648, 
        "ansible_memory_mb": {
            "nocache": {
                "free": 874, 
                "used": 118
            }, 
            "real": {
                "free": 648, 
                "total": 992, 
                "used": 344
            }, 
            "swap": {
                "cached": 0, 
                "free": 0, 
                "total": 0, 
                "used": 0
            }
        }, 
        "ansible_memtotal_mb": 992
    }, 
    "changed": false
}


通过shell查看是否一致
[root@server1 mnt]# ansible servers -m shell -a 'free -m'
172.25.15.22 | SUCCESS | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:            992         143         569          13         279         682
Swap:             0           0           0

172.25.15.21 | SUCCESS | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:            992          82         651          13         258         750
Swap:             0           0           0

172.25.15.250 | SUCCESS | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:           3834        2606         134         194        1093         753
Swap:          3071           1        3070




可以保存数据来分析
[root@server1 mnt]# ansible servers -m setup -a 'filter="*mem*"' --tree /mnt/facts
172.25.15.22 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 551, 
        "ansible_memory_mb": {
            "nocache": {
                "free": 787, 
                "used": 205
            }, 
            "real": {
                "free": 551, 
                "total": 992, 
                "used": 441
            }, 
            "swap": {
                "cached": 0, 
                "free": 0, 
                "total": 0, 
                "used": 0
            }
        }, 


[root@server1 facts]# cat  172.25.15.21
{"ansible_facts": {"ansible_memfree_mb": 645, "ansible_memory_mb": {"nocache": {"free": 872, "used": 120}, "real": {"free": 645, "total": 992, "used": 347}, "swap": {"cached": 0, "free": 0, "total": 0, "used": 0}}, "ansible_memtotal_mb": 992}, "changed": false}[root@server1 facts]# ^C
[root@server1 facts]# pwd
/mnt/facts

猜你喜欢

转载自blog.csdn.net/iaMay_____/article/details/81737517