Ansible试试是在受管主机上自动检测到变量,事实上含有主机相关信息,可以像play中的常规变量、条件、循环或依赖于主机中收集的值的任何其他语句那样使用。
可以通过查看为受管主机收集事实,运行一个收集事实并使用debug模块显示
cat debug.yml
---
- name : fact dump
hosts : all
tasks :
- name : print all facts
debug :
var : ansible_facts
ansible-playbook debug.yml 运行输出如下
PLAY [fact dump] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [servera.lab.example.com]
TASK [print all facts] *********************************************************
ok: [servera.lab.example.com] => {
"ansible_facts": {
"_facts_gathered": true,
"all_ipv4_addresses": [
"172.25.250.10"
],
"all_ipv6_addresses": [
"fe80::e6c5:468e:edb6:9b52"
],
"ansible_local": {},
"apparmor": {
"status": "disabled"
},
"architecture": "x86_64",
"bios_date": "04/01/2014",
"bios_version": "1.11.1-4.module+el8.1.0+4066+0f1aadab",
"cmdline": {
"BOOT_IMAGE": "(hd0,msdos1)/boot/vmlinuz-4.18.0-80.el8.x86_64",
"console": "ttyS0,115200n8",
"crashkernel": "auto",
"no_timer_check": true,
"ro": true,
"root": "UUID=884f47c9-a69d-4c5b-915d-6b7c9c74c923"
},
"date_time": {
"date": "2022-04-26",
"day": "26",
"epoch": "1650921692",
"hour": "05",
"iso8601": "2022-04-25T21:21:32Z",
"iso8601_basic": "20220426T052132956247",
"iso8601_basic_short": "20220426T052132",
"iso8601_micro": "2022-04-25T21:21:32.956529Z",
"minute": "21",
"month": "04",
"second": "32",
"time": "05:21:32",
"tz": "CST",
"tz_offset": "+0800",
"weekday": "Tuesday",
"weekday_number": "2",
"weeknumber": "17",
"year": "2022"
},
"default_ipv4": {
"address": "172.25.250.10",
"alias": "enp1s0",
"broadcast": "172.25.250.255",
"gateway": "172.25.250.254",
"interface": "enp1s0",
"macaddress": "52:54:00:00:fa:0a",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "172.25.250.0",
"type": "ether"
},
"default_ipv6": {},
"device_links": {
"ids": {},
"labels": {},
"masters": {},
"uuids": {
"vda1": [
"884f47c9-a69d-4c5b-915d-6b7c9c74c923"
]
}
},
"devices": {
"vda": {
"holders": [],
"host": "SCSI storage controller: Red Hat, Inc. Virtio block device (rev 01)",
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"model": null,
"partitions": {
"vda1": {
"holders": [],
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": [
"884f47c9-a69d-4c5b-915d-6b7c9c74c923"
]
},
"sectors": "20969439",
"sectorsize": 512,
"size": "10.00 GB",
"start": "2048",
"uuid": "884f47c9-a69d-4c5b-915d-6b7c9c74c923"
}
},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "mq-deadline",
"sectors": "20971520",
"sectorsize": "512",
"size": "10.00 GB",
"support_discard": "0",
"vendor": "0x1af4",
"virtual": 1
},
"vdb": {
"holders": [],
"host": "SCSI storage controller: Red Hat, Inc. Virtio block device (rev 01)",
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "mq-deadline",
"sectors": "10485760",
"sectorsize": "512",
"size": "5.00 GB",
"support_discard": "0",
"vendor": "0x1af4",
"virtual": 1
}
},
"distribution": "RedHat",
"distribution_file_parsed": true,
"distribution_file_path": "/etc/redhat-release",
"distribution_file_search_string": "Red Hat",
"distribution_file_variety": "RedHat",
"distribution_major_version": "8",
"distribution_release": "Ootpa",
"distribution_version": "8.0",
"dns": {
"nameservers": [
"172.25.250.254"
],
"search": [
"lab.example.com",
"example.com"
]
},
"domain": "lab.example.com",
"effective_group_id": 1000,
"effective_user_id": 1000,
"enp1s0": {
"active": true,
"device": "enp1s0",
"features": {
"esp_hw_offload": "off [fixed]",
"esp_tx_csum_hw_offload": "off [fixed]",
"fcoe_mtu": "off [fixed]",
"generic_receive_offload": "on",
"generic_segmentation_offload": "on",
"highdma": "on [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 [fixed]",
"rx_checksumming": "on [fixed]",
"rx_fcs": "off [fixed]",
"rx_gro_hw": "off [fixed]",
"rx_udp_tunnel_port_offload": "off [fixed]",
"rx_vlan_filter": "on [fixed]",
"rx_vlan_offload": "off [fixed]",
"rx_vlan_stag_filter": "off [fixed]",
"rx_vlan_stag_hw_parse": "off [fixed]",
"scatter_gather": "on",
"tcp_segmentation_offload": "on",
"tls_hw_record": "off [fixed]",
"tls_hw_tx_offload": "off [fixed]",
"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_esp_segmentation": "off [fixed]",
"tx_fcoe_segmentation": "off [fixed]",
"tx_gre_csum_segmentation": "off [fixed]",
"tx_gre_segmentation": "off [fixed]",
"tx_gso_partial": "off [fixed]",
"tx_gso_robust": "on [fixed]",
"tx_ipxip4_segmentation": "off [fixed]",
"tx_ipxip6_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_tcp6_segmentation": "on",
"tx_tcp_ecn_segmentation": "on",
"tx_tcp_mangleid_segmentation": "off",
"tx_tcp_segmentation": "on",
"tx_udp_segmentation": "off [fixed]",
"tx_udp_tnl_csum_segmentation": "off [fixed]",
"tx_udp_tnl_segmentation": "off [fixed]",
"tx_vlan_offload": "off [fixed]",
"tx_vlan_stag_hw_insert": "off [fixed]",
"udp_fragmentation_offload": "off",
"vlan_challenged": "off [fixed]"
},
"hw_timestamp_filters": [],
"ipv4": {
"address": "172.25.250.10",
"broadcast": "172.25.250.255",
"netmask": "255.255.255.0",
"network": "172.25.250.0"
},
"ipv6": [
{
"address": "fe80::e6c5:468e:edb6:9b52",
"prefix": "64",
"scope": "link"
}
],
"macaddress": "52:54:00:00:fa:0a",
"module": "virtio_net",
"mtu": 1500,
"pciid": "virtio1",
"promisc": false,
"speed": -1,
"timestamping": [
"tx_software",
"rx_software",
"software"
],
"type": "ether"
},
"enp2s0": {
"active": true,
"device": "enp2s0",
"features": {
"esp_hw_offload": "off [fixed]",
"esp_tx_csum_hw_offload": "off [fixed]",
"fcoe_mtu": "off [fixed]",
"generic_receive_offload": "on",
"generic_segmentation_offload": "on",
"highdma": "on [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 [fixed]",
"rx_checksumming": "on [fixed]",
"rx_fcs": "off [fixed]",
"rx_gro_hw": "off [fixed]",
"rx_udp_tunnel_port_offload": "off [fixed]",
"rx_vlan_filter": "on [fixed]",
"rx_vlan_offload": "off [fixed]",
"rx_vlan_stag_filter": "off [fixed]",
"rx_vlan_stag_hw_parse": "off [fixed]",
"scatter_gather": "on",
"tcp_segmentation_offload": "on",
"tls_hw_record": "off [fixed]",
"tls_hw_tx_offload": "off [fixed]",
"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_esp_segmentation": "off [fixed]",
"tx_fcoe_segmentation": "off [fixed]",
"tx_gre_csum_segmentation": "off [fixed]",
"tx_gre_segmentation": "off [fixed]",
"tx_gso_partial": "off [fixed]",
"tx_gso_robust": "on [fixed]",
"tx_ipxip4_segmentation": "off [fixed]",
"tx_ipxip6_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_tcp6_segmentation": "on",
"tx_tcp_ecn_segmentation": "on",
"tx_tcp_mangleid_segmentation": "off",
"tx_tcp_segmentation": "on",
"tx_udp_segmentation": "off [fixed]",
"tx_udp_tnl_csum_segmentation": "off [fixed]",
"tx_udp_tnl_segmentation": "off [fixed]",
"tx_vlan_offload": "off [fixed]",
"tx_vlan_stag_hw_insert": "off [fixed]",
"udp_fragmentation_offload": "off",
"vlan_challenged": "off [fixed]"
},
"hw_timestamp_filters": [],
"macaddress": "52:54:00:01:fa:0a",
"module": "virtio_net",
"mtu": 1500,
"pciid": "virtio2",
"promisc": false,
"speed": -1,
"timestamping": [
"tx_software",
"rx_software",
"software"
],
"type": "ether"
},
"env": {
"DBUS_SESSION_BUS_ADDRESS": "unix:path=/run/user/1000/bus",
"HOME": "/home/student",
"LANG": "en_US.UTF-8",
"LESSOPEN": "||/usr/bin/lesspipe.sh %s",
"LOGNAME": "student",
"LS_COLORS": "rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.m4a=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.oga=01;36:*.opus=01;36:*.spx=01;36:*.xspf=01;36:",
"MAIL": "/var/mail/student",
"PATH": "/home/student/.local/bin:/home/student/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin",
"PWD": "/home/student",
"SELINUX_LEVEL_REQUESTED": "",
"SELINUX_ROLE_REQUESTED": "",
"SELINUX_USE_CURRENT_RANGE": "",
"SHELL": "/bin/bash",
"SHLVL": "2",
"SSH_CLIENT": "172.25.250.9 53964 22",
"SSH_CONNECTION": "172.25.250.9 53964 172.25.250.10 22",
"SSH_TTY": "/dev/pts/0",
"TERM": "linux",
"USER": "student",
"XDG_RUNTIME_DIR": "/run/user/1000",
"XDG_SESSION_ID": "9",
"_": "/usr/bin/python3"
},
"fibre_channel_wwn": [],
"fips": false,
"form_factor": "Other",
"fqdn": "servera.lab.example.com",
"gather_subset": [
"all"
],
"hostname": "servera",
"hostnqn": "",
"interfaces": [
"enp1s0",
"enp2s0",
"lo"
],
"is_chroot": true,
"iscsi_iqn": "",
"kernel": "4.18.0-80.el8.x86_64",
"lo": {
"active": true,
"device": "lo",
"features": {
"esp_hw_offload": "off [fixed]",
"esp_tx_csum_hw_offload": "off [fixed]",
"fcoe_mtu": "off [fixed]",
"generic_receive_offload": "on",
"generic_segmentation_offload": "on",
"highdma": "on [fixed]",
"hw_tc_offload": "off [fixed]",
"l2_fwd_offload": "off [fixed]",
"large_receive_offload": "off [fixed]",
"loopback": "on [fixed]",
"netns_local": "on [fixed]",
"ntuple_filters": "off [fixed]",
"receive_hashing": "off [fixed]",
"rx_all": "off [fixed]",
"rx_checksumming": "on [fixed]",
"rx_fcs": "off [fixed]",
"rx_gro_hw": "off [fixed]",
"rx_udp_tunnel_port_offload": "off [fixed]",
"rx_vlan_filter": "off [fixed]",
"rx_vlan_offload": "off [fixed]",
"rx_vlan_stag_filter": "off [fixed]",
"rx_vlan_stag_hw_parse": "off [fixed]",
"scatter_gather": "on",
"tcp_segmentation_offload": "on",
"tls_hw_record": "off [fixed]",
"tls_hw_tx_offload": "off [fixed]",
"tx_checksum_fcoe_crc": "off [fixed]",
"tx_checksum_ip_generic": "on [fixed]",
"tx_checksum_ipv4": "off [fixed]",
"tx_checksum_ipv6": "off [fixed]",
"tx_checksum_sctp": "on [fixed]",
"tx_checksumming": "on",
"tx_esp_segmentation": "off [fixed]",
"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_ipxip4_segmentation": "off [fixed]",
"tx_ipxip6_segmentation": "off [fixed]",
"tx_lockless": "on [fixed]",
"tx_nocache_copy": "off [fixed]",
"tx_scatter_gather": "on [fixed]",
"tx_scatter_gather_fraglist": "on [fixed]",
"tx_sctp_segmentation": "on",
"tx_tcp6_segmentation": "on",
"tx_tcp_ecn_segmentation": "on",
"tx_tcp_mangleid_segmentation": "on",
"tx_tcp_segmentation": "on",
"tx_udp_segmentation": "off [fixed]",
"tx_udp_tnl_csum_segmentation": "off [fixed]",
"tx_udp_tnl_segmentation": "off [fixed]",
"tx_vlan_offload": "off [fixed]",
"tx_vlan_stag_hw_insert": "off [fixed]",
"udp_fragmentation_offload": "off",
"vlan_challenged": "on [fixed]"
},
"hw_timestamp_filters": [],
"ipv4": {
"address": "127.0.0.1",
"broadcast": "host",
"netmask": "255.0.0.0",
"network": "127.0.0.0"
},
"ipv6": [
{
"address": "::1",
"prefix": "128",
"scope": "host"
}
],
"mtu": 65536,
"promisc": false,
"timestamping": [
"tx_software",
"rx_software",
"software"
],
"type": "loopback"
},
"lsb": {},
"machine": "x86_64",
"machine_id": "88394775ca9147439ae63aab9c500045",
"memfree_mb": 386,
"memory_mb": {
"nocache": {
"free": 617,
"used": 204
},
"real": {
"free": 386,
"total": 821,
"used": 435
},
"swap": {
"cached": 0,
"free": 0,
"total": 0,
"used": 0
}
},
"memtotal_mb": 821,
"module_setup": true,
"mounts": [
{
"block_available": 2222886,
"block_size": 4096,
"block_total": 2618619,
"block_used": 395733,
"device": "/dev/vda1",
"fstype": "xfs",
"inode_available": 5197101,
"inode_total": 5242304,
"inode_used": 45203,
"mount": "/",
"options": "rw,seclabel,relatime,attr2,inode64,noquota",
"size_available": 9104941056,
"size_total": 10725863424,
"uuid": "884f47c9-a69d-4c5b-915d-6b7c9c74c923"
}
],
"nodename": "servera.lab.example.com",
"os_family": "RedHat",
"pkg_mgr": "dnf",
"proc_cmdline": {
"BOOT_IMAGE": "(hd0,msdos1)/boot/vmlinuz-4.18.0-80.el8.x86_64",
"console": [
"tty0",
"ttyS0,115200n8"
],
"crashkernel": "auto",
"no_timer_check": true,
"ro": true,
"root": "UUID=884f47c9-a69d-4c5b-915d-6b7c9c74c923"
},
"processor": [
"0",
"GenuineIntel",
"QEMU Virtual CPU version 2.5+"
],
"processor_cores": 1,
"processor_count": 1,
"processor_threads_per_core": 1,
"processor_vcpus": 1,
"product_name": "KVM",
"product_serial": "NA",
"product_uuid": "NA",
"product_version": "RHEL-7.6.0 PC (Q35 + ICH9, 2009)",
"python": {
"executable": "/usr/bin/python3",
"has_sslcontext": true,
"type": "cpython",
"version": {
"major": 3,
"micro": 8,
"minor": 6,
"releaselevel": "final",
"serial": 0
},
"version_info": [
3,
6,
8,
"final",
0
]
},
"python_version": "3.6.8",
"real_group_id": 1000,
"real_user_id": 1000,
"selinux": {
"config_mode": "enforcing",
"mode": "enforcing",
"policyvers": 31,
"status": "enabled",
"type": "targeted"
},
"selinux_python_present": true,
"service_mgr": "systemd",
"ssh_host_key_ecdsa_public": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMzl8zujH2mt6wy+3tgViAh/kbAOg/o+5bbNMUqiDX+keObXAIODAEb9EoQLfAcR3ZlEKqYCsweGADkw52HmNCc=",
"ssh_host_key_ed25519_public": "AAAAC3NzaC1lZDI1NTE5AAAAICduOpoEOEGzWCiFyWpwOPPzo729ugmYJ+z+Xd2VYxTl",
"ssh_host_key_rsa_public": "AAAAB3NzaC1yc2EAAAADAQABAAABAQDLXT54GJfAGvnIbzAW8flDln+3QAgHrlut7VBIwdus6+Fwf/GKFvMz28VpL2mRJO/Don1LDcyojxWgmRjaI8nOYQxE58zxWH/rQ05sjD9rtVd529921E5RJjUaiydjW4FXf1EsCyKbt2mxEPKKA5Neg6WTQmGdljh4TqPtv9bqDbMjUUj+cwFV2OoY2RmqDtrxW6bggeZQMYMFmgYEaQDcGUc1n3+3gHHIZDOTj8ice4z/fKMUl2QKmcU619zy4A2jwHYawrFx7s+OkeKSVT6gfrcI7wk9+I2PqbINXXa/WsLNlFzDkmiD0JbOL9ZrjBW3WmoX4SIc2G2BH97ZFXqD",
"swapfree_mb": 0,
"swaptotal_mb": 0,
"system": "Linux",
"system_capabilities": [
""
],
"system_capabilities_enforced": "True",
"system_vendor": "Red Hat",
"uptime_seconds": 3408,
"user_dir": "/home/student",
"user_gecos": "Student User",
"user_gid": 1000,
"user_id": "student",
"user_shell": "/bin/bash",
"user_uid": 1000,
"userspace_architecture": "x86_64",
"userspace_bits": "64",
"virtualization_role": "guest",
"virtualization_type": "kvm"
}
}
PLAY RECAP *********************************************************************
servera.lab.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0