Ansible Facts (Part 1)

Salle J Ingle
locusinnovations.com
5 min readFeb 8, 2018

Dynamically Discovered System Variables

In my last post, Out with Opsworks, In with Terraform, I talked a bit about migrating to a Packer + Ansible + Terraform portion of the CI/CD workflow for a client that I am working on. I’ll be blogging about various aspects of the processes and technologies as a way to share information I think others might find helpful and as a reference for me to look back on.

The Ansible setup module is automatically called by playbooks to gather what is called ‘Facts’ about remote hosts. These facts are system variables available to a host which can be used in playbooks.

Here is a quick example of the plethora of info that is gathered on a system and the command executed (locally in this example) to gather these facts:

[root@ip-10-X-X-X ~]# ansible -m setup localhost
[WARNING]: Could not match supplied host pattern, ignoring: all
[WARNING]: provided hosts list is empty, only localhost is available
localhost | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"10.X.X.X"
],
"ansible_all_ipv6_addresses": [
"fe80::867:3cff:febb:3b9e"
],
"ansible_apparmor": {
"status": "disabled"
},
"ansible_architecture": "x86_64",
"ansible_bios_date": "08/24/2006",
"ansible_bios_version": "4.2.amazon",
"ansible_cmdline": {
"KEYTABLE": "us",
"LANG": "en_US.UTF-8",
"console": "ttyS0",
"nvme_core.io_timeout": "4294967295",
"root": "LABEL=/",
"selinux": "0"
},
"ansible_date_time": {
"date": "2018-02-08",
"day": "08",
"epoch": "1518117592",
"hour": "19",
"iso8601": "2018-02-08T19:19:52Z",
"iso8601_basic": "20180208T191952389404",
"iso8601_basic_short": "20180208T191952",
"iso8601_micro": "2018-02-08T19:19:52.389465Z",
"minute": "19",
"month": "02",
"second": "52",
"time": "19:19:52",
"tz": "UTC",
"tz_offset": "+0000",
"weekday": "Thursday",
"weekday_number": "4",
"weeknumber": "06",
"year": "2018"
},
"ansible_default_ipv4": {
"address": "10.X.X.X",
"alias": "eth0",
"broadcast": "10.X.X.255",
"gateway": "10.X.X.1",
"interface": "eth0",
"macaddress": "0a:67:3c:XX:XX:XX",
"mtu": 9001,
"netmask": "255.255.255.0",
"network": "10.X.X.0",
"type": "ether"
},
"ansible_default_ipv6": {},
"ansible_device_links": {
"ids": {},
"labels": {
"xvda1": [
"\\x2f"
]
},
"masters": {},
"uuids": {
"xvda1": [
"8cd9967e-f9c0-438f-bebd-a0a7c5886ebc"
]
}
},
"ansible_devices": {
"xvda": {
"holders": [],
"host": "",
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"model": null,
"partitions": {
"xvda1": {
"holders": [],
"links": {
"ids": [],
"labels": [
"\\x2f"
],
"masters": [],
"uuids": [
"8cd9967e-f9c0-438f-bebd-a0a7c5886ebc"
]
},
"sectors": "41938911",
"sectorsize": 512,
"size": "20.00 GB",
"start": "4096",
"uuid": "8cd9967e-f9c0-438f-bebd-a0a7c5886ebc"
}
},
"removable": "0",
"rotational": "0",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "noop",
"sectors": "41943040",
"sectorsize": "512",
"size": "20.00 GB",
"support_discard": "0",
"vendor": null,
"virtual": 1
}
},
"ansible_distribution": "Amazon",
"ansible_distribution_file_parsed": true,
"ansible_distribution_file_path": "/etc/system-release",
"ansible_distribution_file_variety": "Amazon",
"ansible_distribution_major_version": "NA",
"ansible_distribution_release": "NA",
"ansible_distribution_version": "2017.09",
"ansible_dns": {
"nameservers": [
"10.X.0.2"
],
"options": {
"attempts": "5",
"timeout": "2"
}
},
"ansible_domain": "",
"ansible_effective_group_id": 0,
"ansible_effective_user_id": 0,
"ansible_env": {
"AWS_AUTO_SCALING_HOME": "/opt/aws/apitools/as",
"AWS_CLOUDWATCH_HOME": "/opt/aws/apitools/mon",
"AWS_ELB_HOME": "/opt/aws/apitools/elb",
"AWS_PATH": "/opt/aws",
"EC2_AMITOOL_HOME": "/opt/aws/amitools/ec2",
"EC2_HOME": "/opt/aws/apitools/ec2",
"HISTCONTROL": "ignoredups",
"HISTSIZE": "1000",
"HOME": "/root",
"HOSTNAME": "ip-10-X-X-X",
"JAVA_HOME": "/usr/lib/jvm/jre",
"LANG": "en_US.UTF-8",
"LESSOPEN": "||/usr/bin/lesspipe.sh %s",
"LESS_TERMCAP_mb": "\u001b[01;31m",
"LESS_TERMCAP_md": "\u001b[01;38;5;208m",
"LESS_TERMCAP_me": "\u001b[0m",
"LESS_TERMCAP_se": "\u001b[0m",
"LESS_TERMCAP_ue": "\u001b[0m",
"LESS_TERMCAP_us": "\u001b[04;38;5;111m",
"LOGNAME": "root",
"LS_COLORS": "rs=0:di=38;5;27:ln=38;5;51:mh=44;38;5;15:pi=40;38;5;11:so=38;5;13:do=38;5;5:bd=48;5;232;38;5;11:cd=48;5;232;38;5;3:or=48;5;232;38;5;9:mi=05;48;5;232;38;5;15:su=48;5;196;38;5;15:sg=48;5;11;38;5;16:ca=48;5;196;38;5;226:tw=48;5;10;38;5;16:ow=48;5;10;38;5;21:st=48;5;21;38;5;15:ex=38;5;34:*.tar=38;5;9:*.tgz=38;5;9:*.arc=38;5;9:*.arj=38;5;9:*.taz=38;5;9:*.lha=38;5;9:*.lz4=38;5;9:*.lzh=38;5;9:*.lzma=38;5;9:*.tlz=38;5;9:*.txz=38;5;9:*.tzo=38;5;9:*.t7z=38;5;9:*.zip=38;5;9:*.z=38;5;9:*.Z=38;5;9:*.dz=38;5;9:*.gz=38;5;9:*.lrz=38;5;9:*.lz=38;5;9:*.lzo=38;5;9:*.xz=38;5;9:*.bz2=38;5;9:*.bz=38;5;9:*.tbz=38;5;9:*.tbz2=38;5;9:*.tz=38;5;9:*.deb=38;5;9:*.rpm=38;5;9:*.jar=38;5;9:*.war=38;5;9:*.ear=38;5;9:*.sar=38;5;9:*.rar=38;5;9:*.alz=38;5;9:*.ace=38;5;9:*.zoo=38;5;9:*.cpio=38;5;9:*.7z=38;5;9:*.rz=38;5;9:*.cab=38;5;9:*.jpg=38;5;13:*.jpeg=38;5;13:*.gif=38;5;13:*.bmp=38;5;13:*.pbm=38;5;13:*.pgm=38;5;13:*.ppm=38;5;13:*.tga=38;5;13:*.xbm=38;5;13:*.xpm=38;5;13:*.tif=38;5;13:*.tiff=38;5;13:*.png=38;5;13:*.svg=38;5;13:*.svgz=38;5;13:*.mng=38;5;13:*.pcx=38;5;13:*.mov=38;5;13:*.mpg=38;5;13:*.mpeg=38;5;13:*.m2v=38;5;13:*.mkv=38;5;13:*.webm=38;5;13:*.ogm=38;5;13:*.mp4=38;5;13:*.m4v=38;5;13:*.mp4v=38;5;13:*.vob=38;5;13:*.qt=38;5;13:*.nuv=38;5;13:*.wmv=38;5;13:*.asf=38;5;13:*.rm=38;5;13:*.rmvb=38;5;13:*.flc=38;5;13:*.avi=38;5;13:*.fli=38;5;13:*.flv=38;5;13:*.gl=38;5;13:*.dl=38;5;13:*.xcf=38;5;13:*.xwd=38;5;13:*.yuv=38;5;13:*.cgm=38;5;13:*.emf=38;5;13:*.axv=38;5;13:*.anx=38;5;13:*.ogv=38;5;13:*.ogx=38;5;13:*.aac=38;5;45:*.au=38;5;45:*.flac=38;5;45:*.mid=38;5;45:*.midi=38;5;45:*.mka=38;5;45:*.mp3=38;5;45:*.mpc=38;5;45:*.ogg=38;5;45:*.ra=38;5;45:*.wav=38;5;45:*.axa=38;5;45:*.oga=38;5;45:*.spx=38;5;45:*.xspf=38;5;45:",
"MAIL": "/var/spool/mail/root",
"PATH": "/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin:/root/bin",
"PWD": "/root",
"SHELL": "/bin/bash",
"SHLVL": "3",
"SUDO_COMMAND": "/bin/bash",
"SUDO_GID": "500",
"SUDO_UID": "500",
"SUDO_USER": "ec2-user",
"TERM": "xterm-256color",
"USER": "root",
"USERNAME": "root",
"_": "/usr/bin/python2.6"
},
"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 [fixed]",
"rx_checksumming": "on [fixed]",
"rx_fcs": "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",
"tx_checksum_fcoe_crc": "off [fixed]",
"tx_checksum_ip_generic": "off [fixed]",
"tx_checksum_ipv4": "on [fixed]",
"tx_checksum_ipv6": "off [requested on]",
"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": "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": "off [requested on]",
"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": "off [fixed]",
"tx_vlan_stag_hw_insert": "off [fixed]",
"udp_fragmentation_offload": "off [fixed]",
"vlan_challenged": "off [fixed]"
},
"hw_timestamp_filters": [],
"ipv4": {
"address": "10.X.X.X",
"broadcast": "10.X.X.255",
"netmask": "255.255.255.0",
"network": "10.X.X.0"
},
"ipv6": [
{
"address": "fe80::867:3cff:XXXX:XXXX",
"prefix": "64",
"scope": "link"
}
],
"macaddress": "0a:67:3c:XX:XX:XX",
"module": "xen_netfront",
"mtu": 9001,
"pciid": "vif-0",
"promisc": false,
"timestamping": [
"rx_software",
"software"
],
"type": "ether"
},
"ansible_fips": false,
"ansible_form_factor": "Other",
"ansible_fqdn": "ip-10-X-X-X",
"ansible_hostname": "ip-10-X-X-X",
"ansible_interfaces": [
"lo",
"eth0"
],
"ansible_kernel": "4.9.77-31.58.amzn1.x86_64",
"ansible_lo": {
"active": true,
"device": "lo",
"features": {
"busy_poll": "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_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",
"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_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_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": "on",
"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": [
"rx_software",
"software"
],
"type": "loopback"
},
"ansible_local": {},
"ansible_lsb": {},
"ansible_lvm": {
"lvs": {},
"pvs": {},
"vgs": {}
},
"ansible_machine": "x86_64",
"ansible_machine_id": "c19e6394a6f5b93647023b535a7b8620",
"ansible_memfree_mb": 1709,
"ansible_memory_mb": {
"nocache": {
"free": 1866,
"used": 135
},
"real": {
"free": 1709,
"total": 2001,
"used": 292
},
"swap": {
"cached": 0,
"free": 4095,
"total": 4095,
"used": 0
}
},
"ansible_memtotal_mb": 2001,
"ansible_mounts": [
{
"block_available": 3730987,
"block_size": 4096,
"block_total": 5127322,
"block_used": 1396335,
"device": "/dev/xvda1",
"fstype": "ext4",
"inode_available": 1263854,
"inode_total": 1310720,
"inode_used": 46866,
"mount": "/",
"options": "rw,noatime,data=ordered",
"size_available": 15282122752,
"size_total": 21001510912,
"uuid": "8cd9967e-f9c0-438f-bebd-a0a7c5886ebc"
}
],
"ansible_nodename": "ip-10-X-X-X",
"ansible_os_family": "RedHat",
"ansible_pkg_mgr": "yum",
"ansible_processor": [
"0",
"GenuineIntel",
"Intel(R) Xeon(R) CPU E5-2676 v3 @ 2.40GHz"
],
"ansible_processor_cores": 1,
"ansible_processor_count": 1,
"ansible_processor_threads_per_core": 1,
"ansible_processor_vcpus": 1,
"ansible_product_name": "HVM domU",
"ansible_product_serial": "ec246331-4071-6747-58c5-988913a980b5",
"ansible_product_uuid": "EC246331-4071-6747-58C5-988913A980B5",
"ansible_product_version": "4.2.amazon",
"ansible_python": {
"executable": "/usr/bin/python2.6",
"has_sslcontext": false,
"type": "CPython",
"version": {
"major": 2,
"micro": 9,
"minor": 6,
"releaselevel": "final",
"serial": 0
},
"version_info": [
2,
6,
9,
"final",
0
]
},
"ansible_python_version": "2.6.9",
"ansible_real_group_id": 0,
"ansible_real_user_id": 0,
"ansible_selinux": {
"status": "Missing selinux Python library"
},
"ansible_selinux_python_present": false,
"ansible_service_mgr": "upstart",
"ansible_ssh_host_key_dsa_public": "AAAAB3NzaC1kc3MAAACBAM/Yx5nfkhy1wKonnEr/ErdXpmEwzYydVyg6lDDADzzcQE0mQJpAo37hQJn1SBZCoGdzbJiJHpzNVKucenGHFipN0PPOP/ssogLHUWT2g3g9XcYkNK2IBNO+AZulJB80XzR1YtXX032gQ5CTVBRlyAqtx5rtisCWxcZMdHD4oAyjAAAAFQDhtxaaYUQTMbEjW+pzCytaKJigiQAAAIEAoO8G32CvmNcsLzrhZ4lnHma+3NmqI1Zl+4H5sbb+k3EOXVBWqqCYGFi8bhPGgaVMeoQFaO0w8694Gn758ozNzP/aiGArbMm7zfKLLmOAY6+dINJ17eJ3iDKNNIbgcPaO5Y9T6xzY7vMTeocPV2/ijdpmYItu/Z+WtaynHsx7CL4AAACADtR2PCj5Yw354cOwVWr7Eu86pQX+dRkldE+DdeTxlBPaLFYzk/9ok009ZSNvr2qOij2RlcH45EXhesr9DN+bbrunBFsKC5oY3r10uA05aAqbafC4JSFZQaNOlksLmSKInRa9AYOz+CwDFIkGXXXXXXXXXXXXXXXXXXXX=",
"ansible_ssh_host_key_ecdsa_public": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBK2DZvj8UBOPyoy5nR6kA3EhyINITS8AXHJBmhVeJrBIFLaGDPrroAXyerW91vcgastn4JApSHclPQ9Mnk1YS3g=",
"ansible_ssh_host_key_ed25519_public": "AAAAC3NzaC1lZDI1NTE5AAAAIPaOG/Tbr5xe2K5pBK9ti/Fdx/cI6RTDXbj+/QjcT14J",
"ansible_ssh_host_key_rsa_public": "AAAAB3NzaC1yc2EAAAADAQABAAABAQCx3yd3Ag2Ufk0zVSnO5QyOdXIXCHP0Z1u7sEAmLnO/SR/aZoV5dV0RmxEcaqDL2slQrJWJGf1tsdkEvlfD28+2cMG7+f4ocelRC4kSf7h+Y6U5hijmlYaetuGDI3lJydnEpYKT4p5OLGmqxLTtcpRV7ghs/rlE9WlIaBknwHNp/6ngoXWalN7hjgAcKVCWpG1LQt3MMn4UojtK3g9iJl8DstTuTp1KoD3nTB/PaYaVpuqJKWPecOAJKcIbYJ6OMOFVy3PUvJ6rHcl8UYRwfHZV3AhFmx9RGks+MeZv2+8Fyc7R+1gGmIiw/4fhcRSSGAxBYnOoxnXXXXXXXXXXXXXXX",
"ansible_swapfree_mb": 4095,
"ansible_swaptotal_mb": 4095,
"ansible_system": "Linux",
"ansible_system_capabilities": [
"cap_chown",
"cap_dac_override",
"cap_dac_read_search",
"cap_fowner",
"cap_fsetid",
"cap_kill",
"cap_setgid",
"cap_setuid",
"cap_setpcap",
"cap_linux_immutable",
"cap_net_bind_service",
"cap_net_broadcast",
"cap_net_admin",
"cap_net_raw",
"cap_ipc_lock",
"cap_ipc_owner",
"cap_sys_module",
"cap_sys_rawio",
"cap_sys_chroot",
"cap_sys_ptrace",
"cap_sys_pacct",
"cap_sys_admin",
"cap_sys_boot",
"cap_sys_nice",
"cap_sys_resource",
"cap_sys_time",
"cap_sys_tty_config",
"cap_mknod",
"cap_lease",
"cap_audit_write",
"cap_audit_control",
"cap_setfcap",
"cap_mac_override",
"cap_mac_admin",
"34",
"35",
"36",
"37+ep"
],
"ansible_system_capabilities_enforced": "True",
"ansible_system_vendor": "Xen",
"ansible_uptime_seconds": 8658,
"ansible_user_dir": "/root",
"ansible_user_gecos": "root",
"ansible_user_gid": 0,
"ansible_user_id": "root",
"ansible_user_shell": "/bin/bash",
"ansible_user_uid": 0,
"ansible_userspace_architecture": "x86_64",
"ansible_userspace_bits": "64",
"ansible_virtualization_role": "guest",
"ansible_virtualization_type": "xen",
"gather_subset": [
"all"
],
"module_setup": true
},
"changed": false
}

So what can you do with these facts? And what if you need to add system environment variables as ansible facts to use in your playbooks and templates? Or leverage output from shell commands? Read on for a few examples in Part 2 on creating and using custom facts!

--

--

Salle J Ingle
locusinnovations.com

AWS Solutions Architect trying to keep up with the singularity while striving to maintain a work-life balance. https://locusinnovations.com