We are excited to release a new script that will make automating with Ansible easier than ever before, leveraging Device42’s up to date inventory to generate static Ansible hosts files for later use with the $ ansible command.
This post is part one of a two part post — to be followed by a demonstration of our (also) new Device42-Ansible dynamic inventory script. In this post, we’ll demonstrate the static inventory script running in Ansible to execute an action against the Device42 inventory. This script provides a basic foundation for creating more complex, custom automations that can be built and tailored to your environment’s needs.
We’ll start by providing the information needed to set up your Ansible environment to utilize the static inventory generation script. Following that, we’ll actually use the script to populate your static Ansible ‘hosts’ file, and then we’ll demonstrate the run of a basic command against all the hosts in the newly populated static hosts file.
Setting up the static hosts file generation script:
- Grab the script from our github. Ensure you have a working Python 2.7.x installation & the requests library installed (See README.MD for full details)
- Create a working directory foo. Copy the file lib.py into it, as well as the inventory scripts. The scripts rely on lib.py as an external dependency. In the case of this example, the static script has been named d42_ansible_inventory_hostfile.py; ensure prerequistes in README.MD are met
- Create a conf file named ‘conf’ (or copy the example file) into your working directory foo & populate the file with your Device42 Instance Info [URL, username, password]. Enable or Disable logging (your choice), and set your output grouping choice [by tag, building, customer, or service_level] – this setting will affect how the output list of hosts is organized
- Mark the script executable: $ chmod +x d42_ansible_inventory_hostfile.py
Running the manual inventory script
To execute the manual inventory script, simply run $./d42_ansible_inventory_hostfile.py or $python d42_ansible_inventory_hostfile.py
A “hosts” file will be created in your working directory which will be populated with a list of hosts that were retrieved via the D42 API. If this is not your first run, or a hosts file already exists, you’ll be prompted to either (O)verwrite or (A)ppend to the current file. You probably want to Overwrite.
An example first run:
Some of the contents of the newly created hosts file:
Running a basic command against all the hosts in hosts
Let’s go ahead and simply ping all of the hosts in our file to ensure everything is working as expected. Once this command works as expected, you can run literally any built in or custom Ansible command you can dream up!
$ ansible all -i hosts -m ping -k
The example command above utilizes the -i option to specify the inventory file we’ve just populated from Device42’s API via the script, and the -k option tells Ansible to prompt for a password to use, since RSA authentication isn’t currently set up on all of the remote machines.
We hope you’ve enjoyed this (very) simple demonstration of the potential of Device42’s new Ansible hosts file generation script, and stay tuned for Part 2 of this post, which will demonstrate the new Ansible dynamic inventory script as well as some more in depth use case examples!
We’re pretty sure your real world use cases are going to be bunches more exciting than this simple lab example [you do already have Device42 [Click to Download, for FREE!!], right!?]… So please do share! We always love to hear how you are using, or plan to use these new scripts to power your creative Ansible automations. Leave a comment or drop us an email with any questions, comments, observations, or even just to say hello! We love hearing from everybody!
Happy automating, and happy new year!
-The Device42 team