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

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/
    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

I did this playbooks and it fails:

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}}/ works OK.

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

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/
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?

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
    Haven’t attempted the api with satellite yet. So, don’t have any direct experience.

It fails too.

  • name: Assign sat server {{server}} to {{host_collection}}
    uri:
    url: “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”
]

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}}/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]

Great! :slight_smile: