I'm creating a playbook where I query some NetBox child prefixes and then use the ID of each child prefix to look up the available IP's in each one (This doesn't exist in the netbox.netbox module).
To do this, I used nb_lookup to return the IDs of each child prefix, and then I used ansible's own URI module to query the NetBox API (api/ipam/prefixes//available-ips/) and return the available IP.
My difficulty is querying all the IDs, one at a time, as I need to get the first available IP from the queried prefixes.
I'm new to YAML development, and I don't really know how to do this. Here is my code:
---
- name: NetBox
hosts: localhost
connection: local
gather_facts: no
collections:
- netbox.netbox
tasks:
- name: "Get Prefixes"
set_fact:
prefixes: "{{ query('netbox.netbox.nb_lookup', 'prefixes',
api_endpoint='https://url-from-my-nb',
api_filter='role=valid status=active',
validate_certs=False,
token='myToken') }}"
- name: Teste
debug:
msg: "{{ prefixes | json_query('[*].value.id') }}"
- name: Teste 2
uri:
validate_certs: False
url: "https://url-from-my-nb/api/ipam/prefixes/{{ prefixes | json_query('[*].value.id') }}/available-ips/"
headers:
Authorization: "Token myToken"
register: prefix
until: prefix
- name: Teste
debug:
msg: "{{ prefix.json[0].address }}"
Result:
PLAY [NetBox] *********************************************************************************************************************************************************************************
TASK [Get Prefixes] *******************************************************************************************************************************************************************************************
/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py:1045: InsecureRequestWarning: Unverified HTTPS request is being made to host 'url-from-my-nb'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
warnings.warn(
ok: [localhost]
TASK [Teste] **************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": [
2,
4,
5,
6,
11,
7,
8,
10,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
29,
51,
52,
28
]
}
TASK [Teste 2] ************************************************************************************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: http.client.InvalidURL: URL can't contain control characters. '/api/ipam/prefixes/[2, 4, 5, 6, 11, 7, 8, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 51, 52, 28]/available-ips/' (found at least ' ')
fatal: [localhost]: FAILED! => {"attempts": 1, "changed": false, "elapsed": 0, "msg": "Status code was -1 and not [200]: An unknown error occurred: URL can't contain control characters. '/api/ipam/prefixes/[2, 4, 5, 6, 11, 7, 8, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 51, 52, 28]/available-ips/' (found at least ' ')", "redirected": false, "status": -1, "url": "https://ipam.getcard.com.br/api/ipam/prefixes/[2, 4, 5, 6, 11, 7, 8, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 51, 52, 28]/available-ips/"}
PLAY RECAP ****************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
How can I loop to query one ID at a time and check if there is an available IP for it?