Home Ansible Examples - Part 3 - Ansible Facts
Post
Cancel

Ansible Examples - Part 3 - Ansible Facts

Here is a sample Ansible script that prints the Ansible facts collected by Ansible.

  1. Use an existing computer. Install Ansible on it.
  2. For this example, create two VMs or use two or more computers you already have. These will be our test computers.
  3. Enter the names and IP addresses of the test computers you want to poll in your /etc/hosts file of your Ansible computer.
  4. From your Ansible computer, try to ping the names of your test computers you want to manage and make sure the names resolve.
  5. For simpicity’s sake, setup passwordless login from your Ansible computers to your test computers. https://castinganet.net/posts/passwordless-login/
  6. On the Ansible computer, make a directory for this example.
  7. Place the following three files into that folder:

inventory.ini

1
2
3
4
5
[group1]
test1.lan

[group2]
test3.lan

facts.sh

1
2
3
4
5
6
7
8
#!/bin/bash

# Hardcoding a password here is NOT recommended. We will fix this later using an Ansible vault.

ansible-playbook \
  -i inventory.ini \
  --extra-vars "ansible_user=dave ansible_sudo_pass=password" \
  facts.yml

facts.yml

1
2
3
4
5
6
7
8
9
10
---
- name: Get the ansible facts and display them
  hosts: all
  become: yes
  gather_facts: yes

  tasks:
    - name: Show the ansible facts
      debug:
        msg: "{{ ansible_facts }}"
  1. In the inventory.ini file change the names and IP address as needed.
  2. Mark facts.sh as executable (chmod +x example1.sh).
  3. If all goes well you should see this output (abbrevated)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
ok: [test1.lan] => {
    "msg": {
        "all_ipv4_addresses": [
            "192.168.1.151"
        ],
        "all_ipv6_addresses": [
            "fe80::be24:11ff:fe60:6022"
        ],
        "ansible_local": {},
        "apparmor": {
            "status": "disabled"
        },
        "architecture": "x86_64",
        "bios_date": "04/01/2014",
        "bios_vendor": "SeaBIOS",
        "bios_version": "rel-1.16.3-0-ga6ed6b701f0a-prebuilt.qemu.org",
        "board_asset_tag": "NA",
        "board_name": "NA",
        "board_serial": "NA",
        "board_vendor": "NA",
        "board_version": "NA",
        "chassis_asset_tag": "NA",
        "chassis_serial": "NA",
        "chassis_vendor": "QEMU",
        "chassis_version": "pc-i440fx-9.0",
...
  • In the facts.yml, you can change the hosts parameter to group1 or group2. “all” means all of the hosts mentioned in your inventory file.
  • When “gather_facts” is set to yes, a bunch of information is collected and stored in a variable called “ansible_facts”
  • This variable contains a lot of really good information. However is does take a few seconds to collect this information.
  • If you do not uses any of this data, you can set “gather_facts” to “no” and the playbook will run faster.
This post is licensed under CC BY 4.0 by the author.