Ansible之采集系统信息&自定义模块

本文详细介绍了如何使用 Ansible 的 setup 模块采集系统信息,特别是 facts 组件的使用,以及如何通过 variables、debug 模块、register 和 json 数据引用来处理模块的输出。此外,还讲解了通过 .fact 文件和 python 脚本自定义采集数据,以及自定义模块的编写、启动和测试方法。
摘要由CSDN通过智能技术生成

setup 模块采集系统信息

Ansible之常用模块中我们简单的介绍了这个模块,但这个模块的使用跟其他模块不太一样,主要是与自定义模块配合使用的,所以放在这里来讲。

facts 组件

在讲 setup 模块之前我们先来讲讲 facts 组件:这个组件是用来收集被管理节点信息的,使用 setup 模块可以获取这些信息,facts 收集的数据是 json 格式的,其内任意一项都可以当作变量被直接应用(如在 playbook、Jinja2 模板中)。

ansible_manager ~ > ansible webservers -m setup
192.168.159.131 | SUCCESS => {
   
    "ansible_facts": {
   
        "ansible_all_ipv4_addresses": [
            "192.168.159.131"
        ], 
        "ansible_all_ipv6_addresses": [
            "fe80::e68f:2f9d:64c4:eaf"
        ], 
        "ansible_apparmor": {
   
            "status": "disabled"
        }, 
        "ansible_architecture": "x86_64", 
        "ansible_bios_date": "07/29/2019", 
        "ansible_bios_version": "6.00", 
        "ansible_cmdline": {
   
            "BOOT_IMAGE": "/vmlinuz-3.10.0-1062.12.1.el7.x86_64", 
            "LANG": "zh_CN.UTF-8", 
            "quiet": true, 
            "rhgb": true, 
            "ro": true, 
            "root": "UUID=9102522f-d3d9-40a4-b8ab-ea22947588db"
        }, 
        "ansible_date_time": {
   
            "date": "2020-03-26", 
            "day": "26", 
            "epoch": "1585221115", 
            "hour": "19", 
            "iso8601": "2020-03-26T11:11:55Z", 
            "iso8601_basic": "20200326T191155513355", 
            "iso8601_basic_short": "20200326T191155", 
            "iso8601_micro": "2020-03-26T11:11:55.513445Z", 
            "minute": "11", 
            "month": "03", 
            "second": "55", 
            "time": "19:11:55", 
            "tz": "CST", 
            "tz_offset": "+0800", 
            "weekday": "星期四", 
            "weekday_number": "4", 
            "weeknumber": "12", 
            "year": "2020"
        }, 
        "ansible_default_ipv4": {
   
            "address": "192.168.159.131", 
            "alias": "ens33", 
            "broadcast": "192.168.159.255", 
            "gateway": "192.168.159.2", 
            "interface": "ens33", 
            "macaddress": "00:0c:29:1e:c8:6c", 
            "mtu": 1500, 
            "netmask": "255.255.255.0", 
            "network": "192.168.159.0", 
            "type": "ether"
        }, 
        "ansible_default_ipv6": {
   }, 
        "ansible_device_links": {
   
            "ids": {
   
                "sr0": [
                    "ata-VMware_Virtual_IDE_CDROM_Drive_10000000000000000001"
                ]
            }, 
            "labels": {
   
                "sr0": [
                    "CentOS\\x207\\x20x86_64"
                ]
            }, 
            "masters": {
   }, 
            "uuids": {
   
                "sda1": [
                    "35ae303e-6bb5-4109-a5bc-9369f9f0a55d"
                ], 
                "sda2": [
                    "9102522f-d3d9-40a4-b8ab-ea22947588db"
                ], 
                "sda3": [
                    "f65b116d-2e5c-467c-8189-4d80543e8ae4"
                ], 
                "sr0": [
                    "2017-09-05-14-14-50-00"
                ]
            }
        }, 
        "ansible_devices": {
   
            "sda": {
   
                "holders": [], 
                "host": "", 
                "links": {
   
                    "ids": [], 
                    "labels": [], 
                    "masters": [], 
                    "uuids": []
                }, 
                "model": "VMware Virtual S", 
                "partitions": {
   
                    "sda1": {
   
                        "holders": [], 
                        "links": {
   
                            "ids": [], 
                            "labels": [], 
                            "masters": [], 
                            "uuids": [
                                "35ae303e-6bb5-4109-a5bc-9369f9f0a55d"
                            ]
                        }, 
                        "sectors": "409600", 
                        "sectorsize": 512, 
                        "size": "200.00 MB", 
                        "start": "2048", 
                        "uuid": "35ae303e-6bb5-4109-a5bc-9369f9f0a55d"
                    }, 
                    "sda2": {
   
                        "holders": [], 
                        "links": {
   
                            "ids": [], 
                            "labels": [], 
                            "masters": [], 
                            "uuids": [
                                "9102522f-d3d9-40a4-b8ab-ea22947588db"
                            ]
                        }, 
                        "sectors": "22648832", 
                        "sectorsize": 512, 
                        "size": "10.80 GB", 
                        "start": "411648", 
                        "uuid": "9102522f-d3d9-40a4-b8ab-ea22947588db"
                    }, 
                    "sda3": {
   
                        "holders": [], 
                        "links": {
   
                            "ids": [], 
                            "labels": [], 
                            "masters": [], 
                            "uuids": [
                                "f65b116d-2e5c-467c-8189-4d80543e8ae4"
                            ]
                        }, 
                        "sectors": "4194304", 
                        "sectorsize": 512, 
                        "size": "2.00 GB", 
                        "start": "23060480", 
                        "uuid": "f65b116d-2e5c-467c-8189-4d80543e8ae4"
                    }
                }, 
                "removable": "0", 
                "rotational": "1", 
                "sas_address": null, 
                "sas_device_handle": null, 
                "scheduler_mode": "deadline", 
                "sectors": "41943040", 
                "sectorsize": "512", 
                "size": "20.00 GB", 
                "support_discard": "0", 
                "vendor": "VMware,", 
                "virtual": 1
            }, 
            "sr0": {
   
                "holders": [], 
                "host": "", 
                "links": {
   
                    "ids": [
                        "ata-VMware_Virtual_IDE_CDROM_Drive_10000000000000000001"
                    ], 
                    "labels": [
                        "CentOS\\x207\\x20x86_64"
                    ], 
                    "masters": [], 
                    "uuids": [
                        "2017-09-05-14-14-50-00"
                    ]
                }, 
                "model": "VMware IDE CDR10", 
                "partitions": {
   }, 
                "removable": "1", 
                "rotational": "1", 
                "sas_address": null, 
                "sas_device_handle": null, 
                "scheduler_mode": "deadline", 
                "sectors": "1622016", 
                "sectorsize": "2048", 
                "size": "792.00 MB", 
                "support_discard": "0", 
                "vendor": "NECVMWar", 
                "virtual"
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值