Hammer wildcard

Hello,

I need to remove a large amount of hosts registered in Foreman using API
(curl) or hammer.
For exemple, I want to delete all hosts in the hostgroup "gce" where last
report is older that 2 days.

How can I manage this kind of request ?

note that my concerned hosts are all using a prefix like "gce-XXX".

thanks,
JonDC

A better dev can correct me if I'm wrong, but I don't believe this is
possible with hammer. You can write your own script that works with the
api, or alternatively you could use the irb shell for foreman to talk
directly with rails to remove the machines.

··· On Nov 13, 2017 07:12, "JonDC" wrote:

Hello,

I need to remove a large amount of hosts registered in Foreman using API
(curl) or hammer.
For exemple, I want to delete all hosts in the hostgroup “gce” where last
report is older that 2 days.

How can I manage this kind of request ?

note that my concerned hosts are all using a prefix like “gce-XXX”.

thanks,
JonDC


You received this message because you are subscribed to the Google Groups
"Foreman users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to foreman-users+unsubscribe@googlegroups.com.
To post to this group, send email to foreman-users@googlegroups.com.
Visit this group at https://groups.google.com/group/foreman-users.
For more options, visit https://groups.google.com/d/optout.

You could even use hammer with one of the output formatters, and loop
over it.

Something like "hammer --csv host list --hostgroup GCE" would get you
the starting list of hosts to loop, then a call to this API (don't think
hammer has last-report):

'/api/v2/hosts/<host-fqdn>/reports/last' | json_pp | grep "reported_at"

would give you the last report date. You could compare those to $now and
then call "hammer host delete --name <host-fqdn>" if it's too old.

OR consider using the expire_hosts plugin, which if you're just
concerned about cleanin up old hosts, might be exactly what you want.
See https://github.com/theforeman/foreman_expire_hosts

Greg

Greg ++ as always with the save :slight_smile:

··· On Mon, Nov 13, 2017 at 8:59 AM Greg Sutcliffe wrote:

You could even use hammer with one of the output formatters, and loop
over it.

Something like “hammer --csv host list --hostgroup GCE” would get you
the starting list of hosts to loop, then a call to this API (don’t think
hammer has last-report):

‘/api/v2/hosts//reports/last’ | json_pp | grep “reported_at”

would give you the last report date. You could compare those to $now and
then call "hammer host delete --name " if it’s too old.

OR consider using the expire_hosts plugin, which if you’re just
concerned about cleanin up old hosts, might be exactly what you want.
See https://github.com/theforeman/foreman_expire_hosts

Greg


You received this message because you are subscribed to the Google Groups
"Foreman users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to foreman-users+unsubscribe@googlegroups.com.
To post to this group, send email to foreman-users@googlegroups.com.
Visit this group at https://groups.google.com/group/foreman-users.
For more options, visit https://groups.google.com/d/optout.

I'll just add that you can do either

$ hammer --csv host list --search 'name ~ "gce*" and last_report > "2 days
ago"'

or

$ hammer --csv host list --search 'hostgroup = "gce" and last_report > "2
days ago"'

to get the list to iterate over. The --search argument is using same syntax
as the search field in hosts UI.

Martin

··· On Mon, Nov 13, 2017 at 3:01 PM, Neil Hanlon wrote:

Greg ++ as always with the save :slight_smile:

On Mon, Nov 13, 2017 at 8:59 AM Greg Sutcliffe greg@emeraldreverie.org > wrote:

You could even use hammer with one of the output formatters, and loop
over it.

Something like “hammer --csv host list --hostgroup GCE” would get you
the starting list of hosts to loop, then a call to this API (don’t think
hammer has last-report):

‘/api/v2/hosts//reports/last’ | json_pp | grep “reported_at”

would give you the last report date. You could compare those to $now and
then call "hammer host delete --name " if it’s too old.

OR consider using the expire_hosts plugin, which if you’re just
concerned about cleanin up old hosts, might be exactly what you want.
See https://github.com/theforeman/foreman_expire_hosts

Greg


You received this message because you are subscribed to the Google Groups
"Foreman users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to foreman-users+unsubscribe@googlegroups.com.
To post to this group, send email to foreman-users@googlegroups.com.
Visit this group at https://groups.google.com/group/foreman-users.
For more options, visit https://groups.google.com/d/optout.


You received this message because you are subscribed to the Google Groups
"Foreman users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to foreman-users+unsubscribe@googlegroups.com.
To post to this group, send email to foreman-users@googlegroups.com.
Visit this group at https://groups.google.com/group/foreman-users.
For more options, visit https://groups.google.com/d/optout.

Thanks to all of you.

foreman_expire_hosts <https://github.com/theforeman/foreman_expire_hosts>
looks like to be what I am looking for except I can't succeed to add the
"expire date" on table host (I am running a foreman 11.1 and the purposed
method looks doens't regognize the VERSION variable).

The solution of Martin looks OK for me.

Thanks.
Jon

··· On Tuesday, November 14, 2017 at 9:08:44 AM UTC+1, Martin Bačovský wrote: > > I'll just add that you can do either > > $ hammer --csv host list --search 'name ~ "gce*" and last_report > "2 days > ago"' > > or > > $ hammer --csv host list --search 'hostgroup = "gce" and last_report > "2 > days ago"' > > to get the list to iterate over. The --search argument is using same > syntax as the search field in hosts UI. > > Martin > > On Mon, Nov 13, 2017 at 3:01 PM, Neil Hanlon > wrote: > >> Greg ++ as always with the save :) >> >> On Mon, Nov 13, 2017 at 8:59 AM Greg Sutcliffe > > wrote: >> >>> You could even use hammer with one of the output formatters, and loop >>> over it. >>> >>> Something like "hammer --csv host list --hostgroup GCE" would get you >>> the starting list of hosts to loop, then a call to this API (don't think >>> hammer has last-report): >>> >>> '/api/v2/hosts//reports/last' | json_pp | grep "reported_at" >>> >>> would give you the last report date. You could compare those to $now and >>> then call "hammer host delete --name " if it's too old. >>> >>> *OR* consider using the expire_hosts plugin, which if you're just >>> concerned about cleanin up old hosts, might be exactly what you want. >>> See https://github.com/theforeman/foreman_expire_hosts >>> >>> Greg >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Foreman users" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to foreman-user...@googlegroups.com . >>> To post to this group, send email to forema...@googlegroups.com >>> . >>> Visit this group at https://groups.google.com/group/foreman-users. >>> For more options, visit https://groups.google.com/d/optout. >>> >> -- >> You received this message because you are subscribed to the Google Groups >> "Foreman users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to foreman-user...@googlegroups.com . >> To post to this group, send email to forema...@googlegroups.com >> . >> Visit this group at https://groups.google.com/group/foreman-users. >> For more options, visit https://groups.google.com/d/optout. >> > >