Problem: in Foreman few of the host’s hostgroup update failed via api/hammer. While try to update via api/hammer getting the error “Name has already been taken error”, but there is no duplicate entry found in foreman.
Expected outcome: foreman api / hammer should update the hostgroup for hosts.
Foreman and Proxy versions: Foreman : 3.4.0-rc1, Proxy: 3.4.0~rc1-1
Foreman and Proxy plugin versions:
Distribution and version: Debian 11 (bullseye )
Other relevant data:
[ INFO 2022-08-25T01:51:03 HammerCLIForeman::Host::UpdateCommand] Called with options: {“option_volume_list”=>, “option_interface_list”=>, “option_id”=>852, “option_name”=>“10.200.70.81”, “option_hostgroup_title”=>“Test”, “option_hostgroup_id”=>408}
{“name”=>“10.200.70.81”, “hostgroup_id”=>408, “puppet_attributes”=>{}}
[DEBUG 2022-08-25T01:51:03 HammerCLIForeman::CommandExtensions::UpdateCommon] Called block for HammerCLIForeman::UpdateCommand request params:
#<Proc:0x00005631a99d3038 /usr/lib/ruby/vendor_ruby/hammer_cli_foreman/command_extensions/update_common.rb:8>
[DEBUG 2022-08-25T01:51:03 HammerCLIForemanPuppet::CommandExtensions::HostPuppetProxy] Called block for HammerCLIForeman::Host::UpdateCommand request params:
#<Proc:0x00005631aa0abdd8 /usr/lib/ruby/vendor_ruby/hammer_cli_foreman_puppet/command_extensions/host.rb:37>
[ INFO 2022-08-25T01:51:03 API] Server: https://foreman.example.com
[ INFO 2022-08-25T01:51:03 API] PUT /api/hosts/852
[DEBUG 2022-08-25T01:51:03 API] Params: {
“host” => {
“name” => “10.200.70.81”,
“hostgroup_id” => 408,
“puppet_attributes” => {}
}
}
[DEBUG 2022-08-25T01:51:03 API] Headers: {}
[DEBUG 2022-08-25T01:51:03 API] Using authenticator: HammerCLIForeman::Api::InteractiveBasicAuth
[ERROR 2022-08-25T01:51:03 API] 422 Unprocessable Entity
[DEBUG 2022-08-25T01:51:03 API] {
“error” => {
“id” => 852,
“errors” => {
“name” => [
[0] “has already been taken”
]
},
“full_messages” => [
[0] “Name has already been taken”
]
}
}
[DEBUG 2022-08-25T01:51:03 Exception] Using exception handler HammerCLIForeman::ExceptionHandler#handle_unprocessable_entity
[ERROR 2022-08-25T01:51:03 Exception] Name has already been taken
Could not update the host:
Name has already been taken
This is still an issue with foreman-3.6.1 and the above work around does work but is there a better fix for this ?
In our scenario, we were updating just the device owner in Foreman via the API for an existing device which returned the “Name has already been taken”.
I understand the need to enforce this during a name attribute update, but in this case we are only updating the owner and not the name at all.
Turns out my problem is unrelated - we have webhooks calling shellhooks and once we disabled them it started working again.
Strangely enough even without any erb expansion and using the example debug shellhooks we are still seeing issues - will gather more data and start a new thread instead.
@Marek_Hulan I think there’s a bug in foreman which casues it.
It was reported on the forums many times, e.g.:
I am using Foreman v. 3.13 and theforeman.foreman.host module (theforeman.foreman.host module – Manage Hosts — Ansible Community Documentation) with Ansible and I am experiencing the same problem. I can’t add a host to Foreman, because, allegedly the name is already taken!
So after reading this and other threads I queried the database to check if hosts is added:
select id, name, type, organization_id, location_id from hosts where name like '%ee0407%';
id|name|type|organization_id|location_id
2497|ee0407||1|9
(1 row)
It’s there! Some other host, which is really added to foreman and can see that host in the UI:
select id, name, type, organization_id, location_id from hosts where name like '%ee0408%';
id|name|type|organization_id|location_id
2495|ee0408|Host::Managed|1|9
As you can see the only difference is the type, which is set to Host::Managed.
Now, if host exists in the foreman database, can someone please explain why:
Host is not being displayed in the gui?
I can’t retrieve the host details using the foreman API, e.g:
Http get call to: /api/hosts/2497
{
“error”: {“message”:“Resource host not found by id ‘2497’”}
}
Regardless if host is already to Foreman or not, ansible foreman.host module should be indempotent, right? So if host exists, should update its properties. If not, it should add it. However, it fails with:
“msg”: “Failed to ensure entity state: ForemanApiException: Error while performing create on hosts: 422 Client Error: Unprocessable Content for url: /api/hosts - {‘id’: None, ‘errors’: {‘name’: [‘has already been taken’]}, ‘full_messages’: [‘Name has already been taken’]}”
ID: None, in the result which is weird.
Adding state: absent to the ansible module doesn’t work:
Personally, I think it’s caused by Foreman Ansible Callback plugin.
When you can’t add a host to foreman by whatever reason, callback plugin still reports that host and it will be added to Foreman automatically if this option is turned on (which is default).