{"id":228,"date":"2021-04-07T11:00:38","date_gmt":"2021-04-07T16:00:38","guid":{"rendered":"https:\/\/www.davidhedges.info\/?p=228"},"modified":"2021-04-07T11:00:38","modified_gmt":"2021-04-07T16:00:38","slug":"using-ansible-to-generate-inventory-data","status":"publish","type":"post","link":"https:\/\/www.davidhedges.info\/index.php\/2021\/04\/07\/using-ansible-to-generate-inventory-data\/","title":{"rendered":"Using Ansible to generate inventory data"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Ansible can be used to collect a lot of usable data that can be imported into an api or database driven inventory system, creating an automated way of updating hardware information. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Creating an ansible yml file to pull inventory to a json file is fairly simple:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>---\n- hosts: all\n  gather_facts: no\n  tasks:\n    - setup:\n      register: myinv\n    - copy:\n        content: \"{{ myinv | to_nice_json }}\"\n        dest: \/some\/local\/path\/{{ inventroy_hostname }}.json\n      delegate_to: localhost<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">running the ansible playbook generates a json file for each host. This data can then be parsed and pushed directly into a database, update a system using an api, parsed and pushed into a spreadsheet, or any number of other things that can be thought of. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">using python to parse the json files, below is a snippet of how I take the data contained within the json files, and pull them into variables. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/python\nimport glob, json\nfor file in glob.glob('\/path\/to\/json\/files\/*.json'):\n     hwinf = json.load(open(file))\n     if 'ansible_facts' in hwinfo:\n          hostfqdn = hwinfo&#91;'ansible_facts']&#91;'ansible_nodename']\n          hostip = hwinfo&#91;'ansible_facts']&#91;'ansible_default_ipv4']&#91;'address']\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">the json file is fairly easy to pull data from. From here though you have the data, and just need to determine how you will use it. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ansible can be used to collect a lot of usable data that can be imported into an api or database driven inventory system, creating an automated way of updating hardware information. Creating an ansible yml file to pull inventory to a json file is fairly simple: running the ansible playbook generates a json file for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[3],"tags":[],"class_list":["post-228","post","type-post","status-publish","format-standard","category-it-blog","entry"],"_links":{"self":[{"href":"https:\/\/www.davidhedges.info\/index.php\/wp-json\/wp\/v2\/posts\/228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.davidhedges.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.davidhedges.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.davidhedges.info\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.davidhedges.info\/index.php\/wp-json\/wp\/v2\/comments?post=228"}],"version-history":[{"count":1,"href":"https:\/\/www.davidhedges.info\/index.php\/wp-json\/wp\/v2\/posts\/228\/revisions"}],"predecessor-version":[{"id":229,"href":"https:\/\/www.davidhedges.info\/index.php\/wp-json\/wp\/v2\/posts\/228\/revisions\/229"}],"wp:attachment":[{"href":"https:\/\/www.davidhedges.info\/index.php\/wp-json\/wp\/v2\/media?parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.davidhedges.info\/index.php\/wp-json\/wp\/v2\/categories?post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.davidhedges.info\/index.php\/wp-json\/wp\/v2\/tags?post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}