Playbook script variables of the facts

facts Ansible variable is used to collect a host of hardware features, systems, services, information and other resources, the first step is to gather information Playbook facts execution.

heel

Queries facts variables

## 查看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
}

Use facts and variables close

You can use these facts variables directly in the Playbook

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

Playbook every script execution collect facts are variables, which consumes resources and time. The less time the host has little effect when the host takes more seriously performance. It can be turned off when not needed

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

Controlled end custom variables 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 }}'

Guess you like

Origin www.cnblogs.com/songguoyou/p/11883322.html