I have successfully connected my ansible playbook to a netbox host. Within this netbox installation i want to manage the IP address of a host in a custom field. (I understand that it should be done via interfaces but creating IP Adresses and interfaces is a lot of work that I would like to avoid.)
The value “local_ip” is visible when running
ansible-inventory --host hostname -i netbox_inventory.yml
....
...
"local_ip": "10.xxx.yyy.zz",
Then I want to use the compose statement in the inventory defintion to map that IP to my ansible_host variable
compose:
ansible_host: local_ip
Unfortunatly ansible_host is not set when running playbooks or ansible-inventory.
Any hints or ideas?
I have only started to play with Netbox, the best command line you can use with any Inventory is:
ansible-inventory -i netbox_inv.yml --list --yaml
Replace netbox_inv.yml
with your inventory name and this will return the Hosts with their Variables. I have a Test Device below and you can see my custom_fields
section
So your local_ip
, should be something like custom_fields.local_ip
and therefore no need to set the compose
part ?
cluster_oberon:
hosts:
Test3:
cluster: oberon
cluster_device:
description: ''
display: oberon
id: 1
name: oberon
url: https://...
cluster_type: proxmox
custom_fields:
proxmox_onboot: 'true'
proxmox_template: local:vztmpl/ubuntu-24.04-standard_24.04-2_amd64.tar.zst
proxmox_vmid: '109'
disk: 32
is_virtual: true
...
I appreaciate your help but thats not what I asked. I want the local_ip to be mapped as ansible_host
I have just tested this with my Netbox data and my answer given does have the answer in it
Your Custom Fields will display in your Inventory like the below, so your local_ip
can be referenced as custom_fields.local_ip
...
custom_fields:
proxmox_onboot: 'true'
proxmox_template: local:vztmpl/ubuntu-24.04-standard_24.04-2_amd64.tar.zst
proxmox_vmid: '109'
...
So as a test, I used one of my existing custom fields proxmox_vmid
and put that as ansible_host
as a quick test in my netbox_inv.yml
file
compose:
ansible_host: custom_fields.proxmox_vmid
Then running the Ansible Inventory command given above, I can now see I have a new field of ansible_host
which has been populated with proxmox_vmid
...
test6:
ansible_host: '109'
cluster: oberon
...
I hope this helps
yes. that helped. When adding the field as custom_fields its working. My first approach was to add a new value via json in local_context_data. That data was there but it seem data in local_context_data cannot be used in compose directiv