Facts 變數查詢

  1. ansible 使用 setup 模組可以列出 ansible_facts 變數,增加 filter=.. 參數可以過濾變數。
    [deyu1@kvm19 ansible]$ ansible dev -m setup -a "filter=ansible_hostname"
    kvm11 | SUCCESS => {
        "ansible_facts": {
            "ansible_hostname": "kvm11",
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": false
    }
    
  2. 如果不知變數完整名稱,可列出所有 ansible_facts 變數,使用管線處理過濾字串,例如:memory,得知總記憶體大小的變數為 ansible_memory_mb.real.total
    [deyu1@kvm19 ansible]$ ansible dev -m setup | grep memory -A16
            "ansible_memory_mb": {
                "nocache": {
                    "free": 1589,
                    "used": 240
                },
                "real": {
                    "free": 1341,
                    "total": 1829,
                    "used": 488
                },
                "swap": {
                    "cached": 0,
                    "free": 123,
                    "total": 123,
                    "used": 0
                }
            },
    
  3. 總記憶體大小有另一變數 ansible_memtotal_mb
    [deyu1@kvm19 ansible]$ ansible dev -m setup -a "filter=ansible_memtotal_mb"
    kvm11 | SUCCESS => {
        "ansible_facts": {
            "ansible_memtotal_mb": 1829,
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": false
    }
    
  4. 列出所有 ansible_facts 變數,使用管線處理過濾字串 bios,得知 bios 版本變數為 ansible_bios_version
    [deyu1@kvm19 ansible]$ ansible dev -m setup | grep bios
            "ansible_bios_date": "01/01/2007",
            "ansible_bios_version": "0.5.1",
    
  5. 如果變數內容太多,管線處理過濾字串不好判斷,可先將整個 ansible_facgts 變數存檔再查。
    [deyu1@kvm19 ansible]$ ansible dev -m setup > setup.txt
    
  6. 例如 ansible_devices 變數有很多資料,vda 大小變數為 ansible_devices.vda.size
    [deyu1@kvm19 ansible]$ grep ansible_devices setup.txt -A160
            "ansible_devices": {
                "dm-0": {
                    "holders": [],
                    "host": "",
    ...........................
                    "virtual": 1
                },
                "vda": {
                    "holders": [],
                    "host": "",
                    "links": {
                        "ids": [],
                        "labels": [],
                        "masters": [],
                        "uuids": []
                    },
                    "model": null,
                    "partitions": {
                        "vda1": {
                            "holders": [],
    ...........................
                    "sectors": "8388608",
                    "sectorsize": "512",
                    "size": "4.00 GB",
                    "support_discard": "0",
                    "vendor": "0x1af4",
                    "virtual": 1
                }
            },