Playbook剧本之facts变量

泪湿孤枕 提交于 2019-12-04 20:56:47

facts变量是Ansible用于采集被控端硬件、系统、服务、资源信息等的一个功能,Playbook执行时第一步就是facts采集信息。

toc

查询facts变量

## 查看web主机所有facts变量(太多,就不展示了)
[root@Ansible project]# ansible web -m setup
## 用filter查看指定信息
[root@Ansible project]# ansible web -m setup -a 'filter=*eth0*'
web1 | SUCCESS => {
    "ansible_facts": {
        "ansible_eth0": {
            "active": true, 
            "device": "eth0", 
            "features": {
                "busy_poll": "off [fixed]", 
                "fcoe_mtu": "off [fixed]", 
                "generic_receive_offload": "on", 
                "generic_segmentation_offload": "on", 
                "highdma": "off [fixed]", 
                "hw_tc_offload": "off [fixed]", 
                "l2_fwd_offload": "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_udp_tunnel_port_offload": "off [fixed]", 
                "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_csum_segmentation": "off [fixed]", 
                "tx_gre_segmentation": "off [fixed]", 
                "tx_gso_partial": "off [fixed]", 
                "tx_gso_robust": "off [fixed]", 
                "tx_ipip_segmentation": "off [fixed]", 
                "tx_lockless": "off [fixed]", 
                "tx_nocache_copy": "off", 
                "tx_scatter_gather": "on", 
                "tx_scatter_gather_fraglist": "off [fixed]", 
                "tx_sctp_segmentation": "off [fixed]", 
                "tx_sit_segmentation": "off [fixed]", 
                "tx_tcp6_segmentation": "off [fixed]", 
                "tx_tcp_ecn_segmentation": "off [fixed]", 
                "tx_tcp_mangleid_segmentation": "off", 
                "tx_tcp_segmentation": "on", 
                "tx_udp_tnl_csum_segmentation": "off [fixed]", 
                "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]"
            }, 
            "hw_timestamp_filters": [], 
            "ipv4": {
                "address": "192.168.1.2", 
                "broadcast": "192.168.1.255", 
                "netmask": "255.255.255.0", 
                "network": "192.168.1.0"
            }, 
            "ipv6": [
                {
                    "address": "fe80::61a2:3896:f282:44e4", 
                    "prefix": "64", 
                    "scope": "link"
                }
            ], 
            "macaddress": "00:0c:29:98:a3:d9", 
            "module": "e1000", 
            "mtu": 1500, 
            "pciid": "0000:02:01.0", 
            "promisc": false, 
            "speed": 1000, 
            "timestamping": [
                "tx_software", 
                "rx_software", 
                "software"
            ], 
            "type": "ether"
        }
    }, 
    "changed": false
}

使用和关闭facts变量

可以在Playbook直接使用这些facts变量

## 受控端的系统
'{{ ansible_distribution }}'
## 受控端eth0的ip
'{{ ansible_eth0.ipv4.address }}'
## 受控端主机名
'{{ ansible_hostname }}'
## 受控端内存大小
'{{ ansible_memtotal_mb }}'

Playbook剧本每次执行facts采集都变量,这会耗费资源和时间。主机少的时候影响不大,当主机多的会严重耗费性能。所以不需要的时候可以关闭

- hosts: web
  gather_facts: no      ## 关闭facts变量

受控端自定义facts变量

## 受控端创建目录
[root@web ~]# mkdir /etc/ansible/facts.d/ -p
## 目录里创建文件后缀为.fact
[root@web ~]# vim /etc/ansible/facts.d/username.fact
[user]
name_one=dasha
name_two=ersha
## 在控制端查看自定义facts变量
[root@Ansible project]# ansible web -m setup -a "filter=ansible_local"
web1 | SUCCESS => {
    "ansible_facts": {
        "ansible_local": {
            "username": {
                "user": {
                    "name_one": "dasha", 
                    "name_two": "ersha"
                }
            }
        }
    }, 
    "changed": false
}
## 调用这些变量
'{{ ansible_local.username.user.name_one }}'
'{{ ansible_local.username.user.name_two }}'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!