21-Ansible常用模块-setup模块

一、概述

setup 模块用于收集远程主机的一些基本信息。

二、常用参数

filter参数: 用于进行条件过滤。如果设置,仅返回匹配过滤条件的信息。

三、示例192.168.0.160主机的信息

  • 获取 192.168.0.160主机的信息。
[root@Ansible ~]# ansible 192.168.0.160 -m setup
192.168.0.160 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.0.160"
        ],
        "ansible_all_ipv6_addresses": [
            "fe80::628e:1a76:1258:8c2a"
        ],
        "ansible_apparmor": {
            "status": "disabled"
        },
        "ansible_architecture": "x86_64",
        "ansible_bios_date": "07/22/2020",
        ......
        "ansible_virtualization_role": "guest",
        "ansible_virtualization_type": "VMware",
        "discovered_interpreter_python": "/usr/libexec/platform-python",
        "gather_subset": [
            "all"
        ],
        "module_setup": true
    },
    "changed": false
}

四、过滤获得指定的信息

  • 用命令获取group1主机的 IPV4 地址。
[root@Ansible ~]# ansible group1 -m setup -a "filter=ansible_all_ipv4_addresses"
192.168.0.160 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.0.160"
        ],
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}
192.168.0.161 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.0.161"
        ],
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}
  • 获取 ansible-demo3 主机的内存信息
[root@Ansible ~]# ansible group1 -m setup -a "filter=ansible_memory_mb"         
192.168.0.160 | SUCCESS => {
    "ansible_facts": {
        "ansible_memory_mb": {
            "nocache": {
                "free": 963,
                "used": 837
            },
            "real": {
                "free": 89,
                "total": 1800,
                "used": 1711
            },
            "swap": {
                "cached": 2,
                "free": 2074,
                "total": 2087,
                "used": 13
            }
        },
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}
192.168.0.161 | SUCCESS => {
    "ansible_facts": {
        "ansible_memory_mb": {
            "nocache": {
                "free": 1639,
                "used": 2112
            },
            "real": {
                "free": 149,
                "total": 3751,
                "used": 3602
            },
            "swap": {
                "cached": 1,
                "free": 2080,
                "total": 2087,
                "used": 7
            }
        },
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}
  • 通过通配符实现模糊匹配,比如以”mb”关键字结尾的信息
[root@Ansible ~]# ansible group1 -m setup -a "filter=*mb"              
192.168.0.161 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 149,
        "ansible_memory_mb": {
            "nocache": {
                "free": 1639,
                "used": 2112
            },
            "real": {
                "free": 149,
                "total": 3751,
                "used": 3602
            },
            "swap": {
                "cached": 1,
                "free": 2080,
                "total": 2087,
                "used": 7
            }
        },
        "ansible_memtotal_mb": 3751,
        "ansible_swapfree_mb": 2080,
        "ansible_swaptotal_mb": 2087,
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}
192.168.0.160 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 89,
        "ansible_memory_mb": {
            "nocache": {
                "free": 963,
                "used": 837
            },
            "real": {
                "free": 89,
                "total": 1800,
                "used": 1711
            },
            "swap": {
                "cached": 2,
                "free": 2074,
                "total": 2087,
                "used": 13
            }
        },
        "ansible_memtotal_mb": 1800,
        "ansible_swapfree_mb": 2074,
        "ansible_swaptotal_mb": 2087,
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}
[root@Ansible ~]# 

四、其他常用信息列出如下:

ansible_all_ipv4_addresses:             仅显示ipv4的信息。
ansible_devices:                        仅显示磁盘设备信息。
ansible_distribution:                   显示是什么系统,例:centos,suse等。
ansible_distribution_major_version:     显示是系统主版本。
ansible_distribution_version:           仅显示系统版本。
ansible_machine:                        显示系统类型,例:32位,还是64位。
ansible_eth0:                           仅显示eth0的信息。
ansible_hostname:                       仅显示主机名。
ansible_kernel:                         仅显示内核版本。
ansible_lvm:                            显示lvm相关信息。
ansible_memtotal_mb:                    显示系统总内存。
ansible_memfree_mb:                     显示可用系统内存。
ansible_memory_mb:                      详细显示内存情况。
ansible_swaptotal_mb:                   显示总的swap内存。
ansible_swapfree_mb:                    显示swap内存的可用内存。
ansible_mounts:                         显示系统磁盘挂载情况。
ansible_processor:                      显示cpu个数(具体显示每个cpu的型号)。
ansible_processor_vcpus:                显示cpu个数(只显示总的个数)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值