ansible 获取系统信息的一些范例

 

主机名:echo `ansible 193.168.120.101 -m setup -a "filter=ansible_hostname" | grep hostname` | awk -F '[:]' '{print $2}' | sed -e '/"/s/"//g' 

内存:echo `ansible 193.168.120.101 -m setup -a "filter=ansible_memtotal_mb" | grep ansible_memtotal_mb` | awk -F '[:]' '{print $2}'

常用信息

 

ansible_all_ipv4_addresses:仅显示ipv4的信息

ansible_devices:仅显示磁盘设备信息

ansible_distribution:显示是什么系统,例:centos,suse等

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个数(只显示总的个数)

ansible_python_version:显示python版本

 

关于palybook当中变量的引用,例如

[root@v72 ansible]# ansible 192.168.156.71 -m setup -a "filter=ansible_eno16777736"
192.168.156.71 | SUCCESS => {
    "ansible_facts": {
        "ansible_eno16777736": {
            "active": true, 
            "device": "eno16777736", 
            "features": {
                "busy_poll": "off [fixed]", 
                "fcoe_mtu": "off [fixed]", 
                "generic_receive_offload": "on", 
                "generic_segmentation_offload": "on", 
                "highdma": "off [fixed]", 
                "large_receive_offload": "off [fixed]", 
                "loopback": "off [fixed]", 
                "netns_local": "off [fixed]", 
                "ntuple_filters": "off [fixed]", 
                "receive_hashing": "off [fixed]", 
                "rx_all": "off", 
                "rx_checksumming": "off", 
                "rx_fcs": "off", 
                "rx_vlan_filter": "on [fixed]", 
                "rx_vlan_offload": "on", 
                "rx_vlan_stag_filter": "off [fixed]", 
                "rx_vlan_stag_hw_parse": "off [fixed]", 
                "scatter_gather": "on", 
                "tcp_segmentation_offload": "on", 
                "tx_checksum_fcoe_crc": "off [fixed]", 
                "tx_checksum_ip_generic": "on", 
                "tx_checksum_ipv4": "off [fixed]", 
                "tx_checksum_ipv6": "off [fixed]", 
                "tx_checksum_sctp": "off [fixed]", 
                "tx_checksumming": "on", 
                "tx_fcoe_segmentation": "off [fixed]", 
                "tx_gre_segmentation": "off [fixed]", 
                "tx_gso_robust": "off [fixed]", 
                "tx_ipip_segmentation": "off [fixed]", 
                "tx_lockless": "off [fixed]", 
                "tx_mpls_segmentation": "off [fixed]", 
                "tx_nocache_copy": "on", 
                "tx_scatter_gather": "on", 
                "tx_scatter_gather_fraglist": "off [fixed]", 
                "tx_sit_segmentation": "off [fixed]", 
                "tx_tcp6_segmentation": "off [fixed]", 
                "tx_tcp_ecn_segmentation": "off [fixed]", 
                "tx_tcp_segmentation": "on", 
                "tx_udp_tnl_segmentation": "off [fixed]", 
                "tx_vlan_offload": "on [fixed]", 
                "tx_vlan_stag_hw_insert": "off [fixed]", 
                "udp_fragmentation_offload": "off [fixed]", 
                "vlan_challenged": "off [fixed]"
            }, 
            "ipv4": {
                "address": "192.168.156.71", 
                "broadcast": "192.168.156.255", 
                "netmask": "255.255.255.0", 
                "network": "192.168.156.0"
            }, 
            "ipv6": [
                {
                    "address": "fe80::20c:29ff:fe56:6ea6", 
                    "prefix": "64", 
                    "scope": "link"
                }
            ], 
            "macaddress": "00:0c:29:56:6e:a6", 
            "module": "e1000", 
            "mtu": 1500, 
            "pciid": "0000:02:01.0", 
            "promisc": false, 
            "speed": 1000, 
            "type": "ether"
        }
    }, 
    "changed": false
}

 

如果想在playbook取出IPV4的值,可以这样引用:

 

[root@v72 ansible]# cat 5.yml 
- hosts: 192.168.156.71
  tasks:
  - debug: 'msg="ipv4 address: {{ansible_eno16777736.ipv4.address}}"'
  - debug: 'msg="macaddress: {{ansible_eno16777736.macaddress}}"'
[root@v72 ansible]# ansible-playbook 5.yml 

PLAY [192.168.156.71] *************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************
Saturday 22 September 2018  20:45:38 +0800 (0:00:00.073)       0:00:00.073 **** 
ok: [192.168.156.71]

TASK [debug] **********************************************************************************************************************************************************
Saturday 22 September 2018  20:45:39 +0800 (0:00:00.829)       0:00:00.902 **** 
ok: [192.168.156.71] => {
    "msg": "ipv4 address: 192.168.156.71"
}

TASK [debug] **********************************************************************************************************************************************************
Saturday 22 September 2018  20:45:39 +0800 (0:00:00.029)       0:00:00.932 **** 
ok: [192.168.156.71] => {
    "msg": "macaddress: 00:0c:29:56:6e:a6"
}

PLAY RECAP ************************************************************************************************************************************************************
192.168.156.71             : ok=3    changed=0    unreachable=0    failed=0   

Saturday 22 September 2018  20:45:39 +0800 (0:00:00.027)       0:00:00.960 **** 
=============================================================================== 
Gathering Facts --------------------------------------------------------- 0.83s
debug ------------------------------------------------------------------- 0.03s
debug ------------------------------------------------------------------- 0.03s
[root@v72 ansible]# 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值