How to check foreman collecting facts from all hosts

1.6k views Asked by At

Want to write automation script to ensure foreman collecting facts from all nodes

How to ensure foreman having facts from all nodes ?

1

There are 1 answers

0
Satender346 On

A fact is a key/value data pair that represents some aspect of node state, such as its IP address, uptime, operating system, or whether it's a virtual machine.

1. Manual process are:

a. Login to foreman UI, click Monitor->Facts

b. run facter -p on hosts

2. Automation: I have written below script to check facts from each hosts

#!/usr/bin/python
import requests
import json

foreman_url = "https://foreman_ip/api/hosts"
username = "admin"
password = "changeme"
node = "node1.puppet.com"
headers = {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
}    
def retrive_hostid():
  host_id = requests.get(foreman_url, headers=headers, verify=False, auth=(username, password))
  hostobj = json.loads(host_id.content)
  for s in hostobj:
     print s['host']['name']
     host_name = s['host']['name']
     url = foreman_url  + host_name + '/facts'  # check facts from each hosts
     print url
     response = requests.get(url, headers=headers, verify=False, auth=('admin', 'changeme'))
     #print response
     respobj = json.loads(response.content)
     print respobj['total'] # display total number of facts found

retrive_hostid()