# Error add host to host collection with uri module api rest operations with satellite

**URL:** <https://forum.ansible.com/t/error-add-host-to-host-collection-with-uri-module-api-rest-operations-with-satellite/28850>\
**Category:** Ansible Project\
**Created:** [October 23, 2018, 8:34am UTC](https://forum.ansible.com/t/error-add-host-to-host-collection-with-uri-module-api-rest-operations-with-satellite/28850 "2018-10-23T08:34:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alberto\_Jimenez\_Loza](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@Alberto\_Jimenez\_Loza](https://forum.ansible.com/u/Alberto_Jimenez_Loza)\
**Post date:** [October 23, 2018, 8:34am UTC](https://forum.ansible.com/t/error-add-host-to-host-collection-with-uri-module-api-rest-operations-with-satellite/28850/1 "2018-10-23T08:34:43Z")

</div>

I’m working wit uri module with satellite.  
I do one playbook for create a new host\_collection and it works OK.

- name: CREATE HC  
uri:  
url: “[https://xxxxxx/katello/api/host\_collections/](https://xxxxxx/katello/api/host_collections/)”  
user: “{{user}}”  
password: “{{password}}”  
method: POST  
return\_content: yes  
force\_basic\_auth: yes  
validate\_certs: no  
body\_format: json  
body: ‘{“name”:“{{hc}}”,“organization\_id”:“1”}’  
status\_code: [200,201,202]

But I want to do a playbook to add/remove host from a host collection, and the urls that appears in the api guides fails.

For api v1 the api guide indicates use PUT /katello/api/v2/host\_collections/:id/add\_systems

[https://access.redhat.com/documentation/en-us/red\_hat\_satellite/6.0/html/api\_guide/add\_systems\_to\_the\_host\_collection](https://access.redhat.com/documentation/en-us/red_hat_satellite/6.0/html/api_guide/add_systems_to_the_host_collection)

I did this playbooks and it fails:

- name: Assign sat server {{server}} to {{host\_collection}}  
uri:  
url: “[https://xxxx/katello/api/v2/host\_collections/{{hc\_id}](https://xxxx/katello/api/v2/host_collections/%7B%7Bhc_id%7D)}/add\_systems/”

user: “{{user}}”  
password: “{{password}}”  
method: PUT  
return\_content: yes  
force\_basic\_auth: yes  
validate\_certs: no  
body\_format: json  
body: ‘{“system\_ids”:“[972,1280]”}’

status\_code: [200,201,202,204,301,401]

The message error is 404 error

The page you were looking for doesn’t exist (404)

The page you were looking for doesn’t exist

And the path [https://xxxx/katello/api/v2/host\_collections/{{hc\_id}](https://xxxx/katello/api/v2/host_collections/%7B%7Bhc_id%7D)}/ works OK.

---

<div class="post-metadata">

**Author:** ![Darkhan](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@Darkhan](https://forum.ansible.com/u/Darkhan)\
**Post date:** [October 23, 2018, 9:29am UTC](https://forum.ansible.com/t/error-add-host-to-host-collection-with-uri-module-api-rest-operations-with-satellite/28850/2 "2018-10-23T09:29:35Z")

</div>

Can you do a get on both of those ids (972,1280)? Seems like one of them is not existing.

---

<div class="post-metadata">

**Author:** ![Alberto\_Jimenez\_Loza](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@Alberto\_Jimenez\_Loza](https://forum.ansible.com/u/Alberto_Jimenez_Loza)\
**Post date:** [October 23, 2018, 9:39am UTC](https://forum.ansible.com/t/error-add-host-to-host-collection-with-uri-module-api-rest-operations-with-satellite/28850/3 "2018-10-23T09:39:41Z")

</div>

The both hosts exists.

I did the same operation api rest using hammer with -d option and I saw the command that uses.

[INFO 2018-10-23 11:24:05 API] PUT /katello/api/host\_collections/586/remove\_hosts  
[DEBUG 2018-10-23 11:24:05 API] Params: {  
“host\_ids” =\> [  
[0] “1280”  
]  
}  
[DEBUG 2018-10-23 11:24:05 API] Headers: {}  
[DEBUG 2018-10-23 11:24:05 API] Using authenticator: HammerCLIForeman::Api::InteractiveBasicAuth  
[DEBUG 2018-10-23 11:24:05 API] Response: {  
“displayMessages” =\> {  
“success” =\> [  
[0] “Successfully removed 1 Host(s).”  
],  
“error” =\>   
}

Then I removed de host with id 1280 from host\_collection and I tried to add the host to hostname, but it fails.

- name: Assign sat server {{server}} to {{host\_collection}}  
uri:

url: “[https://xxxxxx/katello/api/host\_collections/586/add\_hosts/](https://xxxxxx/katello/api/host_collections/586/add_hosts/)”  
user: “{{user}}”  
password: “{{password}}”  
method: PUT  
return\_content: yes  
force\_basic\_auth: yes  
validate\_certs: no  
body\_format: json  
body: ‘{“hosts\_ids”:“[1280]”}’  
status\_code: [200,201,202,204,301,401]

“json”: {  
“displayMessage”: “undefined method `map' for nil:NilClass\nDid you mean? tap", "errors": [ "undefined method `map’ for nil:NilClass\nDid you mean? tap”  
]  
},

The json with the array is badly built?

---

<div class="post-metadata">

**Author:** ![Brad2](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Brad2](https://forum.ansible.com/u/Brad2)\
**Post date:** [October 23, 2018, 11:44am UTC](https://forum.ansible.com/t/error-add-host-to-host-collection-with-uri-module-api-rest-operations-with-satellite/28850/4 "2018-10-23T11:44:48Z")

</div>

Two things:

1. Try removing the trailing slash in the url.
2. Try: url: [https://xxxx/katello/api/v2/host\_collections/{{ hc\_id }}/add\_systems](https://xxxx/katello/api/v2/host_collections/%7B%7Bhc_id%7D%7D/add_systems/)  
Haven’t attempted the api with satellite yet. So, don’t have any direct experience.

---

<div class="post-metadata">

**Author:** ![Alberto\_Jimenez\_Loza](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@Alberto\_Jimenez\_Loza](https://forum.ansible.com/u/Alberto_Jimenez_Loza)\
**Post date:** [October 23, 2018, 12:58pm UTC](https://forum.ansible.com/t/error-add-host-to-host-collection-with-uri-module-api-rest-operations-with-satellite/28850/5 "2018-10-23T12:58:31Z")

</div>

It fails too.

- name: Assign sat server {{server}} to {{host\_collection}}  
uri:  
url: “[https://xxxx/katello/api/host\_collections/586/add\_hosts](https://xxxx/katello/api/host_collections/586/add_hosts)”  
user: “{{user}}”  
password: “{{password}}”  
method: PUT  
return\_content: yes  
force\_basic\_auth: yes  
validate\_certs: no  
body\_format: json  
body: ‘{“host\_ids”:“[972]”}’  
status\_code: [200,201,202,204,301,401]

“json”: {  
“displayMessage”: “undefined method `map' for \"[972]\":String\nDid you mean? tap", "errors": ["undefined method `map’ for "[972]":String\nDid you mean? tap”  
]

---

<div class="post-metadata">

**Author:** ![Alberto\_Jimenez\_Loza](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@Alberto\_Jimenez\_Loza](https://forum.ansible.com/u/Alberto_Jimenez_Loza)\
**Post date:** [October 23, 2018, 3:20pm UTC](https://forum.ansible.com/t/error-add-host-to-host-collection-with-uri-module-api-rest-operations-with-satellite/28850/6 "2018-10-23T15:20:43Z")

</div>

I changed the playbook and it runs ok.

- name: Assign sat server {{server}} with id {{host.json.id}} to {{host\_collection}} with id {{hc\_id}}  
uri:  
url: “[https://xxxxx/katello/api/host\_collections/{{hc\_id}](https://xxxxx/katello/api/host_collections/%7B%7Bhc_id%7D)}/add\_hosts”  
user: “{{user}}”  
password: “{{password}}”  
method: PUT  
return\_content: yes  
force\_basic\_auth: yes  
validate\_certs: no  
body\_format: json  
body: ‘{“host\_ids”:[“{{host.json.id}}”]}’  
status\_code: [200,201,202,204,301,401]

---

<div class="post-metadata">

**Author:** ![Brad2](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Brad2](https://forum.ansible.com/u/Brad2)\
**Post date:** [October 23, 2018, 4:42pm UTC](https://forum.ansible.com/t/error-add-host-to-host-collection-with-uri-module-api-rest-operations-with-satellite/28850/7 "2018-10-23T16:42:49Z")

</div>

Great! 🙂
