Hi,
I just want to close this off by saying this is the code I am running:
host = get_json('%shosts/%s' % (satellite_url, hostname), username,
password)
display_json_results(host)
host_scp = get_json('%shosts/%d/smart_class_parameters' % (satellite_url,
host['id']), username, password)
for next_host_scp in host_scp['results']:
host_scp_overrides = get_json(
'%ssmart_class_parameters/%d/override_values' % (satellite_url,
next_host_scp['id']), username, password)
if len(host_scp_overrides['results']):
for next_override in host_scp_overrides['results']:
if next_override['match'] == 'fqdn=%s' % (host['name']):
print(' %s = %s' % (next_host_scp['parameter'], json.dumps(
next_override['value'], indent=6, sort_keys=True)))
elif output_verbosity == 'full':
print(' %s = %s' % (next_host_scp['parameter'], json.dumps(
next_host_scp['default_value'], indent=6, sort_keys=True)))
It needs a bit of tidying up, but does at least display the info I'm
interested in.
I actually wanted to group the variables by puppet class, but when I wrote
the code to find every smart class parameter associated with a puppet
class, and then every override associated with each parameter it was
tragically slow. Here's the code:
host = get_json('%shosts/%s' % (satellite_url, hostname), username,
password)
display_json_results(host)
print('Host is %s' % (host['name']))
parameters = get_json('%shosts/%s/parameters' % (satellite_url, host['id'
]), username, password)
for next_puppet_class in sorted(host['all_puppetclasses']):
print(' Puppet class [%s]' % (next_puppet_class['name']))
sc_params = get_json('%spuppetclasses/%d/smart_class_parameters' % (
satellite_url, next_puppet_class['id']), username, password)
if len(sc_params['results']):
sc_params_ids = set([next_sc_param['id'] for next_sc_param in
sc_params['results']])
for next_sc_param in sc_params_ids:
host_scp_overrides = get_json(
'%ssmart_class_parameters/%d/override_values' % (satellite_url,
next_sc_param), username, password)
for next_override in host_scp_overrides['results']:
if next_override['match'] == 'fqdn=%s' % (host['name']):
print('%s ' % (next_override['value']))
That takes 233 seconds, where as the first example code I gave takes 101
seconds (so neither is really acceptable, but I can't see a better way of
doing it).
Regards,
David
···
On Thursday, 29 October 2015 09:57:44 UTC, David Evans wrote:
>
> Hi,
>
> I think I have my solution. I should have been trying to display overides
> to smart class parameters. Thanks again Justin for all your help - got the
> breakthrough here -
> http://www.katello.org/docs/api/apidoc/override_values.html.
>
> Regards,
> David
>
> On Wednesday, 28 October 2015 14:45:59 UTC, David Evans wrote:
>>
>> Hi Justin,
>>
>> Thanks for that. I can see what you're doign there, and when I do the
>> same I can see the variables associated with other entities (hostgroups for
>> example).
>>
>> Unfortunately I have very few variables assoicated in that way. All mine
>> are associated with the host itself and only one is inherited from anywhere
>> - that's the activation key which comes from the host group. In my
>> previous example I was expecting to see about 20 variables I have
>> overridden on the host itself, but none of them are displayed. I see now
>> that I wasn't clear in my last post what I was expecting to see.
>>
>> Regards,
>> David
>>
>>
>>
>>
>> On Tuesday, 27 October 2015 16:36:58 UTC, Justin Garrison wrote:
>>>
>>> I am also using rhel 7 and satellite 6. Here is rough code that I used
>>> to look for kt_activation_key from a host parameters and if it wasn't found
>>> go up one level to the host group.
>>>
>>> I'm using requests for the
>>> api interaction
>>>
>>> def get_activationkeys(host_id, k_server):
>>> """Get the activation keys a system should have based on the
>>> kt_activation_keys parameter in katello"""
>>> des_act_keys = ""
>>> hg_params = ""
>>>
>>>
>>> k_host = get_katello_host(host_id, k_server)
>>> if k_host['hostgroup_id']:
>>> hg_params =
>>> requests.get('https://%s/api/v2/hostgroups/%s/parameters' \
>>> % (k_server, k_host['hostgroup_id']), auth=KATELLO_AUTH, \
>>> verify=BUNDLE_CERT).json()
>>> if k_host['parameters']:
>>> # try to get kt_activation_key from host. If not use hostgroup
>>> # eventually change to all_parameters
>>> for parameter in k_host['parameters']:
>>> if parameter.get('name') == 'kt_activation_keys':
>>> des_act_keys = parameter['value']
>>> break
>>> if not des_act_keys and hg_params:
>>> for result in hg_params['results']:
>>> if result.get('name') == 'kt_activation_keys':
>>> des_act_keys = result['value']
>>> if des_act_keys:
>>> return des_act_keys.split(",")
>>> else:
>>> return 0
>>>
>>>
>>> This only looks up 1 level but you could repeat if you have multiple
>>> levels.
>>>
>>> Hope that give you a starting point.
>>>
>>> On Tuesday, October 27, 2015 at 7:01:22 AM UTC-7, David Evans wrote:
>>>>
>>>> Hi,
>>>>
>>>> Thanks to both of you for these responses and sorry for the delay
>>>> replying.
>>>>
>>>> @Shlomi - thanks it looks like the you are implementing the solution I
>>>> require, but it will be sometime before it is available to RH Satellite.
>>>> In the meantime I tried to implement the patches, but the code I am
>>>> running is sufficiently different to what the patch is based on that it
>>>> didn't work.
>>>>
>>>> @Justin - this looks like exactly what I need to get me going while I
>>>> await the updated functionality mentioned by Shlomi. Unfortunately it
>>>> doesn't work for me. Things may be slightly different because I am using
>>>> Satellite and not pure Foreman - I don't know. Anyway, the path to the API
>>>> is different (uses 'api/v2') and using that I got it to run successfully,
>>>> but it returns nothing. Here's the code I am using:
>>>>
>>>> host = get_json('%shosts/%s' % (satellite_url, hostname), username,
>>>> password)
>>>> display_json_results(host)
>>>>
>>>>
>>>> print('Host id is %s' % (host['id']))
>>>>
>>>>
>>>> parameters = get_json('%shosts/%d/parameters' % (satellite_url, host[
>>>> 'id']), username, password)
>>>> display_json_results(parameters)
>>>>
>>>>
>>>> The first part of that works, and I get the host id, which is correct
>>>> (verified through hammer), but the final part doesn't give me any
>>>> parameters for the host, just this:
>>>>
>>>>
>>>>
>>>> "subnet_name": "BRL_DEVUAT_INFRA_SERVER",
>>>> "updated_at": "2015-10-27T13:34:02Z",
>>>> "use_image": null,
>>>> "uuid": null
>>>> }
>>>> Host id is 221
>>>> {
>>>> "page": 1,
>>>> "per_page": 40,
>>>> "results": [],
>>>> "search": null,
>>>> "sort": {
>>>> "by": null,
>>>> "order": null
>>>> },
>>>> "subtotal": 0,
>>>> "total": 0
>>>> }
>>>>
>>>> I am trying to run 'yum update' on my Satellite server in case that
>>>> helps with any of these issues and I'll report back once that is done.
>>>> There is an issue with that at the moment, which is rpm dependency
>>>> related, so it may not be immediate. In the meantime if anyone can spot
>>>> anything stupidly obvious that is wrong with the code I'm running I'd
>>>> appreciate it. Thanks.
>>>>
>>>> Regards,
>>>> David
>>>>
>>>> On Monday, 26 October 2015 23:32:35 UTC, Justin Garrison wrote:
>>>>>
>>>>> I was having the exact same problem. The documentation here
>>>>> says
>>>>> you can get parameters from /api/hosts/:host_id/parameters but that will
>>>>> only show parameters that are set directly on the host or set as an
>>>>> override on the host. It will not show any inherited parameters. My
>>>>> solution was to look for a parameter in the host/parameters and if it's not
>>>>> there get the host group id and look for the parameter there. If multiple
>>>>> host groups are nested you may have to do this repeatedly until you find
>>>>> the parameter you want.
>>>>>
>>>>> That's a terrible solution. Luckily in a future version there will be
>>>>> a all_parameters value on the host which will show all parameters merged
>>>>> into one dictionary. See http://projects.theforeman.org/issues/11763
>>>>> to track the issue. I'm watching that bug and plan to change my code when
>>>>> it's merged and released.
>>>>>
>>>>> Hope that helps
>>>>>
>>>>> On Monday, October 26, 2015 at 7:08:42 AM UTC-7, David Evans wrote:
>>>>>>
>>>>>> Hi,
>>>>>>
>>>>>> I'm writing a script in Python that needs to enumerate all the host
>>>>>> parameters that have been overridden for a given host. Can anyone help me
>>>>>> understand how to do that?
>>>>>>
>>>>>> Initially I was looking through the Red Hat Satellite API guide, but
>>>>>> I couldn't find a way to do it there. Then I tried to accomplish the same
>>>>>> using hammer, but again with no success. My thinking there was that if I
>>>>>> could accomplish it via hammer that would probably help my understanding of
>>>>>> how it should be implemented in python. So, with that in mind I guess my
>>>>>> questions are:
>>>>>>
>>>>>> 1. Which tree of the API will allow me to see which variables have
>>>>>> been overridden and what values have been set?
>>>>>> 2. Is there a way in the API to query overrides by host, or is that
>>>>>> something I will need to do myself?
>>>>>>
>>>>>> I want to clarify what I mean by 'host parameters' in case it is not
>>>>>> clear. If go to 'Hosts\All Hosts' in the Satellite Web UI and then select
>>>>>> a host and go to the parameters tab I see a list of the variables available
>>>>>> to associated puppet modules. If I elect to override any of those
>>>>>> parameters they appear at the bottom of the tab under the heading 'host
>>>>>> parameters'. In this list I can see the puppet module, variable name and
>>>>>> override value, which is the data I need to extract for my script.
>>>>>>
>>>>>> Thanks in advance for any assistance.
>>>>>>
>>>>>> Regards,
>>>>>> David
>>>>>>
>>>>>> PS, my environment is RHEL7, Satellite 6, python 2.7 and I have
>>>>>> written enough of the code to query simple braches and display output (such
>>>>>> as hosts and environments).
>>>>>>
>>>>>