With this python script you can use Cisco APIC-EM as a dynamic inventory for ansible.
I want to mention that I used code snippets from the project ansible-apic-em, which contains an APIC-EM ansible module.
By contrast my project uses APIC-EM as a real dynamic inventory for ansible playbooks.
- Place the file apicem-dynamic-inventory.py in a folder at your ansible-project. For example the folder can be called "dynamic-inventory".
- Edit the file and change the variables self.controllername, self.username and self.password to your own APIC-EM values.
- Execute a playbook with APIC-EM as the dynamic inventory.
The example playbook uses the ansible module ntc_show_command of the project ntc-ansible. Please consider that you need the module and the template-file for the command you are using (in this case show clock).
all devices
ansible-playbook regex.yml -i dynamic-inventory
limit to one location
ansible-playbook regex.yml -i dynamic-inventory --limit "mylocation"
limit to one device
ansible-playbook regex.yml -i dynamic-inventory --limit "mydevice"
In this version the following variables are returned by the dynamic inventory as host variables:
- macAddress
- upTime
- bootDateTime
- location
- device_ip
- software
Following versions will also return platform as a variable. This would be useful for other ansible-modules like the ones from ntc-ansible.
- this version doesnt return devices which have no location set in APIC-EM
This project is published with the MIT license. So feel free to use the code in your own projects.