Ansible的setup模块

Ansible的信息收集模块 de

在Ansible中,有三个可用的模块可以使用,它们分别是setup模块、ohai模块和facter模块。起初,我打算比较这三个模块,并考虑它们各自的用途。

当……一开始,当ohai、facter被安装在主机上时,通过setup模块获取信息的时候,ohai和facter的信息也会一起被获取过来。

再次阅读setup模块的文件时,我发现里面有普通的说明。

嗨,使用facter模块。

在中文方面,Ansible常用的setup模块可以直接使用而不需要任何特殊操作,但ohai和facter需要在管理主机上安装包(适用于常见的Linux发行版)。

在每个模块中可获取的信息

我尝试获取每个模块中可以获取的信息的第一层级(由于只有设置模块的第一层级才可以获取到facts信息,所以我们进入第二层级)。

确认可以在安装了ohai和facter的管理主机上执行setup模块,因此可以在setup模块的执行结果中包含ohai和facter的信息。

哦嗨,ohai和facter的信息分别带有”ohai_”和”facter_”的前缀,因此可以很容易地区分出是哪个模块的信息。

设置:
“ansible_all_ipv4_addresses”: [
“ansible_all_ipv6_addresses”: [
“ansible_apparmor”: {
“ansible_architecture”: “x86_64”,
“ansible_bios_date”: “12/01/2006”,
“ansible_bios_version”: “VirtualBox”,
“ansible_cmdline”: {
“ansible_date_time”: {
“ansible_default_ipv4”: {
“ansible_default_ipv6”: {},
“ansible_device_links”: {
“ansible_devices”: {
“ansible_distribution”: “Ubuntu”,
“ansible_distribution_file_parsed”: true,
“ansible_distribution_file_path”: “/etc/os-release”,
“ansible_distribution_file_variety”: “Debian”,
“ansible_distribution_major_version”: “20”,
“ansible_distribution_release”: “focal”,
“ansible_distribution_version”: “20.04”,
“ansible_dns”: {
“ansible_docker0”: {
“ansible_domain”: “”,
“ansible_effective_group_id”: 1000,
“ansible_effective_user_id”: 1000,
“ansible_enp0s3”: {
“ansible_enp0s8”: {
“ansible_env”: {
“ansible_fibre_channel_wwn”: [],
“ansible_fips”: false,
“ansible_form_factor”: “Other”,
“ansible_fqdn”: “vmubuntu01”,
“ansible_hostname”: “vmubuntu01”,
“ansible_hostnqn”: “”,
“ansible_interfaces”: [
“ansible_is_chroot”: false,
“ansible_iscsi_iqn”: “”,
“ansible_kernel”: “5.4.0-40-generic”,
“ansible_kernel_version”: “#44-Ubuntu SMP Tue Jun 23 00:01:04 UTC 2020”,
“ansible_lo”: {
“ansible_local”: {},
“ansible_lsb”: {
“ansible_machine”: “x86_64”,
“ansible_machine_id”: “389b93f59183469fb13afb4aae1b55a2”,
“ansible_memfree_mb”: 755,
“ansible_memory_mb”: {
“ansible_memtotal_mb”: 1987,
“ansible_mounts”: [
“ansible_nodename”: “vmubuntu01”,
“ansible_os_family”: “Debian”,
“ansible_pkg_mgr”: “apt”,
“ansible_proc_cmdline”: {
“ansible_processor”: [
“ansible_processor_cores”: 2,
“ansible_processor_count”: 1,
“ansible_processor_threads_per_core”: 1,
“ansible_processor_vcpus”: 2,
“ansible_product_name”: “VirtualBox”,
“ansible_product_serial”: “NA”,
“ansible_product_uuid”: “NA”,
“ansible_product_version”: “1.2”,
“ansible_python”: {
“ansible_python_version”: “3.8.2”,
“ansible_real_group_id”: 1000,
“ansible_real_user_id”: 1000,
“ansible_selinux”: {
“ansible_selinux_python_present”: false,
“ansible_service_mgr”: “systemd”,
“ansible_ssh_host_key_ecdsa_public”: “AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMOUpASBVedbKFxuOMF1V71hZu8n1SC3IZRt+ZNOc1+YOUTa+SLcM2uJb9LGNsohNu24BoLFCiZB7QXhyAWNevE=”,
“ansible_ssh_host_key_ed25519_public”: “AAAAC3NzaC1lZDI1NTE5AAAAIOApcjPxH1NxypOBfEwBVgtCRoUW4bvBXfGxHDUiqQt5”,
“ansible_ssh_host_key_rsa_public”: “AAAAB3NzaC1yc2EAAAADAQABAAABgQC/K+pHJ3DZd+Qysd5QVJ4qoz90rlyGUzj2BiRAKIDZZ080l0xRJ1iEkdTJE7bCrq33hIDo4YRbSYIUCp25MgMwGq2yDuhdk7AcYjrLHuss0pP2UmO24wjGet5V3n+EhO75oelCja99Ru7uenwKlfVHO2dVY1yiC5KaYU4YDaRT/7v5qQ+vPDaxjhRktPvpvJWCo0rP7qKxSxCF4a6pebIWUQyV+B6XX/39OzYUrqQ/A5+jJO0QGCO+qb//WbxhGu4CCBKq4+Vc8NNWAfh2c73A1HDAFDZdHMYy1lTeyehMt3751mdp4x0vOLp/95kep354upuO2wzzFCpkA/luw+CcuUq25KgoP9FkVhFv483ktBEWl3K18syr3okeqh0WIYKaAPMvrWWxKRAzMq4KtyWPnrQvb3eYqFzv0Ba1npKicnN1yU6fFkVo44Dr9eNP9V/lUje8KQASHFxa7VyDA+mx/ZPd8p6Z/6n1SFFugtNL1+5PNkeY44mbz35qZccq1ls=”,
“ansible_swapfree_mb”: 923,
“ansible_swaptotal_mb”: 923,
“ansible_system”: “Linux”,
“ansible_system_capabilities”: [
“ansible_system_capabilities_enforced”: “True”,
“ansible_system_vendor”: “innotek GmbH”,
“ansible_uptime_seconds”: 1694,
“ansible_user_dir”: “/home/vmware”,
“ansible_user_gecos”: “vmware,,,”,
“ansible_user_gid”: 1000,
“ansible_user_id”: “vmware”,
“ansible_user_shell”: “/bin/bash”,
“ansible_user_uid”: 1000,
“ansible_userspace_architecture”: “x86_64”,
“ansible_userspace_bits”: “64”,
“ansible_virtualization_role”: “guest”,
“ansible_virtualization_type”: “virtualbox”,
“facter_disks”: {
“facter_dmi”: {
“facter_facterversion”: “3.11.0”,
“facter_filesystems”: “ext2,ext3,ext4,squashfs,vfat”,
“facter_fips_enabled”: false,
“facter_identity”: {
“facter_is_virtual”: true,
“facter_kernel”: “Linux”,
“facter_kernelmajversion”: “5.4”,
“facter_kernelrelease”: “5.4.0-40-generic”,
“facter_kernelversion”: “5.4.0”,
“facter_load_averages”: {
“facter_memory”: {
“facter_mountpoints”: {
“facter_networking”: {
“facter_os”: {
“facter_partitions”: {
“facter_path”: “/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games”,
“facter_processors”: {
“facter_ruby”: {
“facter_ssh”: {
“facter_system_uptime”: {
“facter_timezone”: “JST”,
“facter_virtual”: “virtualbox”,
“gather_subset”: [
“module_setup”: true,
“ohai_block_device”: {
“ohai_chef_packages”: {
“ohai_cloud”: null,
“ohai_command”: {
“ohai_counters”: {
“ohai_cpu”: {
“ohai_dmi”: {
“ohai_docker”: {
“ohai_domain”: null,
“ohai_filesystem”: {
“ohai_fips”: {
“ohai_fqdn”: “vmubuntu01”,
“ohai_hostname”: “vmubuntu01”,
“ohai_hostnamectl”: {
“ohai_idletime”: “55 minutes 07 seconds”,
“ohai_idletime_seconds”: 3307,
“ohai_init_package”: “systemd”,
“ohai_ip6address”: “fe80::3ace:bd6d:7658:e333”,
“ohai_ipaddress”: “10.0.3.15”,
“ohai_kernel”: {
“ohai_keys”: {
“ohai_languages”: {
“ohai_lsb”: {
“ohai_macaddress”: “08:00:27:13:70:39”,
“ohai_machine_id”: “389b93f59183469fb13afb4aae1b55a2”,
“ohai_machinename”: “vmubuntu01”,
“ohai_memory”: {
“ohai_network”: {
“ohai_ohai_time”: 1594852696.2341497,
“ohai_os”: “linux”,
“ohai_os_version”: “5.4.0-40-generic”,
“ohai_packages”: {
“ohai_platform”: “ubuntu”,
“ohai_platform_family”: “debian”,
“ohai_platform_version”: “20.04”,
“ohai_root_group”: “root”,
“ohai_shells”: [
“ohai_sysconf”: {
“ohai_systemd_paths”: {
“ohai_time”: {
“ohai_uptime”: “28 minutes 16 seconds”,
“ohai_uptime_seconds”: 1696,
“ohai_virtualization”: {
哦,嗨
“block_device”:{
“changed”:false,
“chef_packages”:{
“cloud”:null,
“command”:{
“counters”:{
“cpu”:{
“dmi”:{
“docker”:{
“domain”:null,
“filesystem”:{
“fips”:{
“fqdn”:“vmubuntu01”,
“hostname”:“vmubuntu01”,
“hostnamectl”:{
“idletime”:“53分14秒”,
“idletime_seconds”:3194,
“init_package”:“systemd”,
“ip6address”:“fe80 :: 3ace:bd6d:7658:e333”,
“ipaddress”:“10.0.3.15”,
“kernel”:{
“keys”:{
“languages”:{
“lsb”:{
“macaddress”:“08:00:27:13:70:39”,
“machine_id”:“389b93f59183469fb13afb4aae1b55a2”,
“machinename”:“vmubuntu01”,
“memory”:{
“network”:{
“ohai_time”:1594852637.2567618,
“os”:“linux”,
“os_version”:“5.4.0-40-generic”,
“packages”:{
“platform”:“ubuntu”,
“platform_family”:“debian”,
“platform_version”:“20.04”,
“root_group”:“root”,
“shells”:[
“sysconf”:{
“systemd_paths”:{
“time”:{
“uptime”:“27分钟17秒”,
“uptime_seconds”:1637,
“virtualization”:{
“facter”
“changed”: false,
“disks”: {
“dmi”: {
“facterversion”: “3.11.0”,
“filesystems”: “ext2,ext3,ext4,squashfs,vfat”,
“fips_enabled”: false,
“identity”: {
“is_virtual”: true,
“kernel”: “Linux”,
“kernelmajversion”: “5.4”,
“kernelrelease”: “5.4.0-40-generic”,
“kernelversion”: “5.4.0”,
“load_averages”: {
“memory”: {
“mountpoints”: {
“networking”: {
“os”: {
“partitions”: {
“path”: “/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games”,
“processors”: {
“ruby”: {
“ssh”: {
“system_uptime”: {
“timezone”: “JST”,
“virtual”: “virtualbox”

請提供中文的本土化翻譯選項。

在调用常规变量时,可以通过{{ [变量名] }}来获取。
此外,通过使用“.”来分隔的方式,可以获取嵌套的信息。

如果您想获取enp0s3的IPv4地址信息,可以通过指定{{ ansible_enp0s3.ipv4.address }}来将地址信息作为变量调用。

"ansible_enp0s3": {
(略)
    "ipv4": {
        "address": "192.168.56.130",
        "broadcast": "192.168.56.255",
        "netmask": "255.255.255.0",
        "network": "192.168.56.0"
    },
192.168.56.130
广告
将在 10 秒后关闭
bannerAds