Working with Ansible facts – retrieving facts

What are Ansible facts?

Ansible facts are pieces of information regarding the remote systems to which you have connected. It contains information like IP addresses, the OS installed, Ethernet devices, mac address, time/date related data, hardware information etc.

These are very useful for scenarios where you need to take conditional operations based on the remote machines state. For example, depending on the OS version used by the remote server you can install the different versions of a package.

Or you can just display some information related to each remote machine like how much RAM is available on each device.

How to Retrieve Ansible facts

By default, whenever you run a task on the remote server, all the facts about that systems are captured. 

Ansible provides a setup module to gather all the information regarding the remote servers. It can be run from the command line to see the data.

If you want to see all the variables captured by the setup module, you can use the following command.

ansible all -m setup

This will give a large dump of data like below.(You can scroll in the below box).

mdtutorials2@system01:~$ ansible all -m setup localhost | SUCCESS => { "ansible_facts": { "ansible_all_ipv4_addresses": [ "10.128.0.2" ], "ansible_all_ipv6_addresses": [ "fe80::4001:aff:fe80:2" ], "ansible_apparmor": { "status": "enabled" }, "ansible_architecture": "x86_64", "ansible_bios_date": "01/01/2011", "ansible_bios_version": "Google", "ansible_cmdline": { "BOOT_IMAGE": "/boot/vmlinuz-4.10.0-35-generic", "console": "ttyS0", "ro": true, "root": "UUID=b2e62f4f-d338-470e-9ae7-4fc0e014858c", "scsi_mod.use_blk_mq": "Y" }, "ansible_date_time": { "date": "2017-11-21", "day": "21", "epoch": "1511244526", "hour": "06", "iso8601": "2017-11-21T06:08:46Z", "iso8601_basic": "20171121T060846389900", "iso8601_basic_short": "20171121T060846", "iso8601_micro": "2017-11-21T06:08:46.389973Z", "minute": "08", "month": "11", "second": "46", "time": "06:08:46", "tz": "UTC", "tz_offset": "+0000", "weekday": "Tuesday", "weekday_number": "2", "weeknumber": "47", "year": "2017" }, "ansible_default_ipv4": { "address": "10.128.0.2", "alias": "ens4", "broadcast": "10.128.0.2", "gateway": "10.128.0.1", "interface": "ens4", "macaddress": "42:01:0a:80:00:02", "mtu": 1460, "netmask": "255.255.255.255", "network": "10.128.0.2", "type": "ether" }, "ansible_default_ipv6": {}, "ansible_device_links": { "ids": { "sda": [ "google-system01", "scsi-0Google_PersistentDisk_system01" ], "sda1": [ "google-system01-part1", "scsi-0Google_PersistentDisk_system01-part1" ] }, "labels": { "sda1": [ "cloudimg-rootfs" ] }, "masters": {}, "uuids": { "sda1": [ "b2e62f4f-d338-470e-9ae7-4fc0e014858c" ] } }, "ansible_devices": { "loop0": { "holders": [], "host": "", "links": { "ids": [], "labels": [], "masters": [], "uuids": [] }, "model": null, "partitions": {}, "removable": "0", "rotational": "1", "sas_address": null, "sas_device_handle": null, "scheduler_mode": "", "sectors": "0", "sectorsize": "512", "size": "0.00 Bytes", "support_discard": "0", "vendor": null, "virtual": 1 }, "loop1": { "holders": [], "host": "", "links": { "ids": [], "labels": [], "masters": [], "uuids": [] }, "model": null, "partitions": {}, "removable": "0", "rotational": "1", "sas_address": null, "sas_device_handle": null, "scheduler_mode": "", "sectors": "0", "sectorsize": "512", "size": "0.00 Bytes", "support_discard": "0", "vendor": null, "virtual": 1 }, "loop2": { "holders": [], "host": "", "links": { "ids": [], "labels": [], "masters": [], "uuids": [] }, "model": null, "partitions": {}, "removable": "0", "rotational": "1", "sas_address": null, "sas_device_handle": null, "scheduler_mode": "", "sectors": "0", "sectorsize": "512", "size": "0.00 Bytes", "support_discard": "0", "vendor": null, "virtual": 1 }, "loop3": { "holders": [], "host": "", "links": { "ids": [], "labels": [], "masters": [], "uuids": [] }, "model": null, "partitions": {}, "removable": "0", "rotational": "1", "sas_address": null, "sas_device_handle": null, "scheduler_mode": "", "sectors": "0", "sectorsize": "512", "size": "0.00 Bytes", "support_discard": "0", "vendor": null, "virtual": 1 }, "loop4": { "holders": [], "host": "", "links": { "ids": [], "labels": [], "masters": [], "uuids": [] }, "model": null, "partitions": {}, "removable": "0", "rotational": "1", "sas_address": null, "sas_device_handle": null, "scheduler_mode": "", "sectors": "0", "sectorsize": "512", "size": "0.00 Bytes", "support_discard": "0", "vendor": null, "virtual": 1 }, "loop5": { "holders": [], "host": "", "links": { "ids": [], "labels": [], "masters": [], "uuids": [] }, "model": null, "partitions": {}, "removable": "0", "rotational": "1", "sas_address": null, "sas_device_handle": null, "scheduler_mode": "", "sectors": "0", "sectorsize": "512", "size": "0.00 Bytes", "support_discard": "0", "vendor": null, "virtual": 1 }, "loop6": { "holders": [], "host": "", "links": { "ids": [], "labels": [], "masters": [], "uuids": [] }, "model": null, "partitions": {}, "removable": "0", "rotational": "1", "sas_address": null, "sas_device_handle": null, "scheduler_mode": "", "sectors": "0", "sectorsize": "512", "size": "0.00 Bytes", "support_discard": "0", "vendor": null, "virtual": 1 }, "loop7": { "holders": [], "host": "", "links": { "ids": [], "labels": [], "masters": [], "uuids": [] }, "model": null, "partitions": {}, "removable": "0", "rotational": "1", "sas_address": null, "sas_device_handle": null, "scheduler_mode": "", "sectors": "0", "sectorsize": "512", "size": "0.00 Bytes", "support_discard": "0", "vendor": null, "virtual": 1 }, "sda": { "holders": [], "host": "Non-VGA unclassified device: Red Hat, Inc Virtio SCSI", "links": { "ids": [ "google-system01", "scsi-0Google_PersistentDisk_system01" ], "labels": [], "masters": [], "uuids": [] }, "model": "PersistentDisk", "partitions": { "sda1": { "holders": [], "links": { "ids": [ "google-system01-part1", "scsi-0Google_PersistentDisk_system01-part1" ], "labels": [ "cloudimg-rootfs" ], "masters": [], "uuids": [ "b2e62f4f-d338-470e-9ae7-4fc0e014858c" ] }, "sectors": "20969439", "sectorsize": 512, "size": "10.00 GB", "start": "2048", "uuid": "b2e62f4f-d338-470e-9ae7-4fc0e014858c" } }, "removable": "0", "rotational": "1", "sas_address": null, "sas_device_handle": null, "scheduler_mode": "", "sectors": "20971520", "sectorsize": "512", "size": "10.00 GB", "support_discard": "512", "vendor": "Google", "virtual": 1 } }, "ansible_distribution": "Ubuntu", "ansible_distribution_file_parsed": true, "ansible_distribution_file_path": "/etc/os-release", "ansible_distribution_file_variety": "Debian", "ansible_distribution_major_version": "16", "ansible_distribution_release": "xenial", "ansible_distribution_version": "16.04", "ansible_dns": { "nameservers": [ "169.234.169.254" ], "search": [ "c.learned-nimbus-158816.internal", "google.internal" ] }, "ansible_domain": "c.learned-nimbus-158816.internal", "ansible_effective_group_id": 1003, "ansible_effective_user_id": 1002, "ansible_ens4": { "active": true, "device": "ens4", "features": { "busy_poll": "on [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_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", "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": "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": "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": "12.128.0.5", "broadcast": "12.128.0.2", "netmask": "255.255.255.255", "network": "12.128.0.5" }, "ipv6": [ { "address": "fe80::4001:aff:fe80:2", "prefix": "64", "scope": "link" } ], "macaddress": "42:01:0a:80:00:02", "module": "virtio_net", "mtu": 1460, "pciid": "virtio1", "promisc": false, "speed": -1, "timestamping": [ "tx_software", "rx_software", "software" ], "type": "ether" }, "ansible_env": { "HOME": "/home/mdtutorials2", "LANG": "en_US.UTF-8", "LESSCLOSE": "/usr/bin/lesspipe %s %s", "LESSOPEN": "| /usr/bin/lesspipe %s", "LOGNAME": "mdtutorials2", "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=00: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:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=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:*.jpg=01;35:*.jpeg=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=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:", "MAIL": "/var/mail/mdtutorials2", "PATH": "/home/mdtutorials2/bin:/home/mdtutorials2/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin", "PWD": "/home/mdtutorials2", "SHELL": "/bin/bash", "SHLVL": "1", "SSH_AUTH_SOCK": "/tmp/ssh-QkcXpNHYXO/agent.1940", "SSH_CLIENT": "74.125.41.104 65160 22", "SSH_CONNECTION": "74.125.41.104 65160 10.128.0.2 22", "SSH_TTY": "/dev/pts/0", "TERM": "xterm-256color", "USER": "mdtutorials2", "XDG_RUNTIME_DIR": "/run/user/1002", "XDG_SESSION_ID": "1", "_": "/usr/bin/ansible" }, "ansible_fips": false, "ansible_form_factor": "Other", "ansible_fqdn": "system01.c.learned-nimbus-158816.internal", "ansible_hostname": "system01", "ansible_interfaces": [ "ens4", "lo" ], "ansible_kernel": "4.10.0-35-generic", "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": { "codename": "xenial", "description": "Ubuntu 16.04.3 LTS", "id": "Ubuntu", "major_release": "16", "release": "16.04" }, "ansible_machine": "x86_64", "ansible_machine_id": "9de8b282171f89dc3612b892e2d97653", "ansible_memfree_mb": 2692, "ansible_memory_mb": { "nocache": { "free": 3352, "used": 345 }, "real": { "free": 2692, "total": 3697, "used": 1005 }, "swap": { "cached": 0, "free": 0, "total": 0, "used": 0 } }, "ansible_memtotal_mb": 3697, "ansible_mounts": [ { "block_available": 2080825, "block_size": 4096, "block_total": 2524617, "block_used": 443792, "device": "/dev/sda1", "fstype": "ext4", "inode_available": 1159530, "inode_total": 1280000, "inode_used": 120470, "mount": "/", "options": "rw,relatime,data=ordered", "size_available": 8523059200, "size_total": 10340831232, "uuid": "b2e62f4f-d338-470e-9ae7-4fc0e014858c" } ], "ansible_nodename": "system01", "ansible_os_family": "Debian", "ansible_pkg_mgr": "apt", "ansible_processor": [ "0", "GenuineIntel", "Intel(R) Xeon(R) CPU @ 2.30GHz" ], "ansible_processor_cores": 1, "ansible_processor_count": 1, "ansible_processor_threads_per_core": 1, "ansible_processor_vcpus": 1, "ansible_product_name": "Google Compute Engine", "ansible_product_serial": "NA", "ansible_product_uuid": "NA", "ansible_product_version": "NA", "ansible_python": { "executable": "/usr/bin/python", "has_sslcontext": true, "type": "CPython", "version": { "major": 2, "micro": 12, "minor": 7, "releaselevel": "final", "serial": 0 }, "version_info": [ 2, 7, 12, "final", 0 ] }, "ansible_python_version": "2.7.12", "ansible_real_group_id": 1003, "ansible_real_user_id": 1002, "ansible_selinux": false, "ansible_selinux_python_present": false, "ansible_service_mgr": "systemd", "ansible_ssh_host_key_dsa_public": "AAAAB3NzaC1kc3MAAACBAKlnUyy6oSfsS+2okJOioem66aioPhZfuOJXn2YU3P2cl71W+SLFt2KPFQgvSMy2j3rQEtlVDlPR/1o7nu0X7k6yCQDpE2a959BThyZhKbMQgd9f4oPIQUbQshiCoB0YFIAINKTFGeRLODFKgfG/thyfKASV+VXPLrIWZ8m+pnYtAAAAFQD9Yty5zidPlxVHTpO7Pg6Jk4m6owAAAIAEaGi2iPUMn7cuGWBNiVJFpXGdEfmVlRqQ3xrKLZ0iDAmurhMSqKX8lAZyu28t/wA20MlUJZMcIg/XEX407ZyqLDdlILnZ+TU31ViCdf1Pw0EjDCO8IA4Q0Qo7IUXsyW0rRVCX9j3P+xpUfssMNS7UU5p+y0zxJ4C0AdjrA4M5TgAAAIAJBCo3nXvRXWwLur2D8tJk84XinIOx0kNwRJHJaSwZeZFF4oxodbUW2ga6ofJitQc1lpQGl+11VG5Nxsj0kie0rXOcebFrAQhTa3Tc42mmpOmMq22zU4/SgO0i+EwPoQjj+3VfgXouChrO8jE0jGsKvH1fzSq2M4KBKdZS54/gTw==", "ansible_ssh_host_key_ecdsa_public": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBPQbE3MLTscpESfMx2rNXsfLrIFLZw6xPV10BleZ9rR4eBA/40KXEHIPY5mUSpHWSpSwbM4hqdr0QSr4Ddl1Uqo=", "ansible_ssh_host_key_ed25519_public": "AAAAC3NzaC1lZDI1NTE5AAAAIKKUr96jr0J6smMdXFfolOkbYP8rtnUBbTi92D9olqDj", "ansible_ssh_host_key_rsa_public": "AAAAB3NzaC1yc2EAAAADAQABAAABAQDdiVVIUPBTZE/NG8zCei1TqsCjNveEeA71C0O/Czc5sIRxX6rvm347i5StTFre/IowBXmx7CpHKBM0hr7SdiYtgSNSDGfbXiy2Kd/fCyVLyCfyVt/UOSa6VAakzWULhP+QHzNeqXTuyLJRhKGlwGJO4eP2JsAtPBN0gtYnyfxVey0utkpO5vtWdDBANgin4e96n5Qz8mqp1Gd+0G52kzs08do0Yi0gpIOKMxEOf7wBt8dd6JTEwJb3Wn3eS86+dalVY7PifIKj3XcDAeXJ8u+o1vXfuUblJscfDOGQyEN2QNVPsSVn40XEMTMuktMnhtBndTxYMgW44iLHq54xQetJ", "ansible_swapfree_mb": 0, "ansible_swaptotal_mb": 0, "ansible_system": "Linux", "ansible_system_capabilities": [ "" ], "ansible_system_capabilities_enforced": "True", "ansible_system_vendor": "Google", "ansible_uptime_seconds": 134, "ansible_user_dir": "/home/mdtutorials2", "ansible_user_gecos": "", "ansible_user_gid": 1003, "ansible_user_id": "mdtutorials2", "ansible_user_shell": "/bin/bash", "ansible_user_uid": 1002, "ansible_userspace_architecture": "x86_64", "ansible_userspace_bits": "64", "ansible_virtualization_role": "NA", "ansible_virtualization_type": "NA", "gather_subset": [ "all" ], "module_setup": true }, "changed": false, "failed": false } mdtutorials2@system01:~$ ansible all -m setup

It is possible to filter data from this. Suppose you need all the info regarding the operating system the remote machine uses, then you can give the following command. 

You can use any patterns/wildcards against the filter keyword. I am using 'ansible_distribution' pattern in the command since from the dump above, it shows the OS name.

ansible all -m setup -a 'filter=ansible_distribution'

output
------
localhost | SUCCESS => { "ansible_facts": { "ansible_distribution": "Ubuntu" }, "changed": false, "failed": false}

Using the Ansible facts inside a task

The above examples showed the facts retrieved via command line. But we need to get the output in Ansible task for more meaningful tasks.

In order to retrieve the previous output( OS info) in an ansible task, you can do the following. I am using the 'ansible_distribution' as a variable directly in the debug task.

- hosts: all
  gather_facts: true
  tasks:
    - name: Ansible​ fact OS 
      debug:
        msg: "{{ ansible_distribution }}"
Output —— ok: [localhost] => { “msg”: “Ubuntu” }

The gather_facts is set to true by default. But I am adding it for clarity.

But you can give the variable name by itself in every case. When using the 'ansible_distribution' variable, it was a first level variable. 

For example, if you want to get the mac address of the remote server, and I use the ' macaddress' pattern, it would throw the following error.

"msg": "The task includes an option with an undefined variable. The error was: 'macaddress' is undefined

In such cases, where the variable has a parent variable, you have to add the name of the parent variable also.

From the large dump, you can see the mac address is avaible in two locations. But, I need the one under the 'ansible_default_ipv4'.

I can give the following command to the task.

- hosts: all
  gather_facts: true
  tasks:
    - name: Ansible fact for mac address
      debug:
        msg: "{{ ansible_default_ipv4.macaddress }}"

output
------
ok: [localhost] => {
    "msg": "42:01:0a:80:00:02"
}

Commonly used Ansible Facts

Hostname - {{ ansible_hostname }}

IP address - {{ ansible_default_ipv4.address }}

Mac Address - {{ ansible_default_ipv4.macaddress }}

Architecture(32/64 bit) - {{ ansible_architecture }} - x86_64

Date - {{ ansible_date_time.date }} - 2017-11-21

Time - {{ ansible_date_time.time }} - 06:08:46

OS - {{ ansible_distribution }} - Ubuntu

OS release - {{ ansible_distribution_release }} - xenial

OS version - {{ ansible_distribution_version }} - 16.04

Home directory - {{ ansible_env.HOME }}

Using Conditional statements with facts

You can use these facts to conditionally execute tasks. 

In the below task, the 'release' will only be showed when the OS distribution is Ubuntu.

- hosts: all
  tasks:
    - name: Ansible fact with conditionals
      debug:
        msg: "{{ ansible_distribution_version }}"
      when: ansible_distribution == "Ubuntu"

2 Replies to “Working with Ansible facts – retrieving facts”

  1. thanks a lot. very useful 🙂

    1. Thanks:)

Comments are closed.