I’m attempting to use the Tower API to remove a host from an inventory. Here’s the tasks in my playbook:
`
-
name: Get host ID
uri:
url: https://satellite/api/v2/inventories/18/hosts/
method: GET
body_format: json
force_basic_auth: yes
user: “{{ tower_user }}”
password: “{{ tower_pass }}”
validate_certs: no
status_code: 200
register: host_list
when: inventory_action == ‘Remove Host’ -
set_fact:
host_id: “{{ host_list.json.results|json_query(query) }}”
vars:
query: “[?name==‘{{ host_name }}’].id”
when: inventory_action == ‘Remove Host’ -
debug:
msg: “{{ host_id[0]| int }}”
when: inventory_action == ‘Remove Host’
#I added [0] to the host_id variable to remove the brackets from the json output.
- name: Remove server from, Tower inventory
uri:
url: https://satellite/api/v2/inventories/18/hosts/
method: POST
body:
id: “{{ host_id[0] }}”
disassociate: true
body_format: json
force_basic_auth: yes
user: “{{ tower_user }}”
password: “{{ tower_pass }}”
validate_certs: no
status_code: 204
when: inventory_action == ‘Remove Host’
`
Results:
`
PLAY [localhost] ********************************************************************************************************************************************************************************************************************************
TASK [Add server to Tower inventory] ************************************************************************************************************************************************************************************************************
skipping: [localhost]
TASK [Get host ID] ******************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [set_fact] *********************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [debug] ************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
“msg”: “813”
}
TASK [Remove server from, Tower inventory] ******************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {“allow”: “GET, POST, HEAD, OPTIONS”, “changed”: false, “connection”: “close”, “content”: “{"msg":"\"id\" field must be an integer."}”, “content_language”: “en”, “content_length”: “42”, “content_type”: “application/json”, “date”: “Tue, 10 Sep 2019 13:47:41 GMT”, “elapsed”: 0, “json”: {“msg”: “"id" field must be an integer.”}, “msg”: “Status code was 400 and not [204]: HTTP Error 400: Bad Request”, “redirected”: false, “server”: “nginx/1.14.1”, “status”: 400, “url”: “https://satellite/api/v2/inventories/18/hosts/”, “vary”: “Accept, Accept-Language, Cookie, Origin”, “x_api_node”: “localhost”, “x_api_time”: “0.100s”, “x_api_total_time”: “0.122s”}
PLAY RECAP **************************************************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=1 skipped=1 rescued=0 ignored=0
`
I’ve tried casting to int using ‘| int’ on the host_id variable but I still get that “field must be an integer” error. Any ideas?