1

想编写自动化脚本以确保工头从所有节点收集事实

如何确保工头拥有来自所有节点的事实?

4

1 回答 1

1

事实是表示节点状态某些方面的键/值数据对,例如其 IP 地址、正常运行时间、操作系统,或者它是否是虚拟机。

1.手动流程有:

一个。登录工头 UI,单击 Monitor->Facts

湾。facter -p在主机上运行

2.自动化: 我写了下面的脚本来检查每个主机的事实

#!/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()
于 2017-07-04T17:46:29.810 回答