# Variable Not Recognized as Integer

**URL:** <https://forum.ansible.com/t/variable-not-recognized-as-integer/31249>\
**Category:** Ansible Project\
**Created:** [September 10, 2019, 1:55pm UTC](https://forum.ansible.com/t/variable-not-recognized-as-integer/31249 "2019-09-10T13:55:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cade\_Lambert](https://avatars.discourse-cdn.com/v4/letter/c/278dde/32.png) [@Cade\_Lambert](https://forum.ansible.com/u/Cade_Lambert)\
**Post date:** [September 10, 2019, 1:55pm UTC](https://forum.ansible.com/t/variable-not-recognized-as-integer/31249/1 "2019-09-10T13:55:43Z")

</div>

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/](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/](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] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*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*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
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/](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?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 10, 2019, 5:13pm UTC](https://forum.ansible.com/t/variable-not-recognized-as-integer/31249/2 "2019-09-10T17:13:59Z")

</div>

A filter doesn't change a variable, so you need to filter it through int every place you are using the varaible where it need to be a int.

---

<div class="post-metadata">

**Author:** ![Cade\_Lambert](https://avatars.discourse-cdn.com/v4/letter/c/278dde/32.png) [@Cade\_Lambert](https://forum.ansible.com/u/Cade_Lambert)\
**Post date:** [September 10, 2019, 5:18pm UTC](https://forum.ansible.com/t/variable-not-recognized-as-integer/31249/3 "2019-09-10T17:18:33Z")

</div>

`

- name: Get host ID  
uri:  
url: [https://satellite/api/v2/inventories/18/hosts/](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)|int }}”  
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/](https://satellite/api/v2/inventories/18/hosts/)  
method: POST  
body:  
id: “{{ host\_id[0]|int }}”  
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’

`

I have it filtered where host\_id is set, and filtered where it’s used in the API call, and I’m getting the same error.
