<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://charlesreid1.com/w/index.php?action=history&amp;feed=atom&amp;title=Ansible%2FVagrant%2FDynamic_Inventory</id>
	<title>Ansible/Vagrant/Dynamic Inventory - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://charlesreid1.com/w/index.php?action=history&amp;feed=atom&amp;title=Ansible%2FVagrant%2FDynamic_Inventory"/>
	<link rel="alternate" type="text/html" href="https://charlesreid1.com/w/index.php?title=Ansible/Vagrant/Dynamic_Inventory&amp;action=history"/>
	<updated>2026-06-19T07:07:45Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.12</generator>
	<entry>
		<id>https://charlesreid1.com/w/index.php?title=Ansible/Vagrant/Dynamic_Inventory&amp;diff=25698&amp;oldid=prev</id>
		<title>Admin: /* Managing a dynamic inventory file using Vagrant boxes */</title>
		<link rel="alternate" type="text/html" href="https://charlesreid1.com/w/index.php?title=Ansible/Vagrant/Dynamic_Inventory&amp;diff=25698&amp;oldid=prev"/>
		<updated>2018-12-10T03:04:46Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Managing a dynamic inventory file using Vagrant boxes&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 03:04, 10 December 2018&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l10&quot;&gt;Line 10:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 10:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Ansible/Vagrant/Dynamic Inventory]] - dynamic inventory uses something like an API or a database to obtain information about the machines that Ansible is managing. This makes scaling and generalization much easier.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Ansible/Vagrant/Dynamic Inventory]] - dynamic inventory uses something like an API or a database to obtain information about the machines that Ansible is managing. This makes scaling and generalization much easier.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Managing a dynamic inventory file &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;using &lt;/del&gt;Vagrant boxes==&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Managing a dynamic inventory file &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;for &lt;/ins&gt;Vagrant boxes==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The following script is a dynamic inventory script run by Ansible. This example script is written in Python. It takes two input arguments, &amp;lt;code&amp;gt;--host=X&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;--list&amp;lt;/code&amp;gt;, and interacts with Vagrant to return the requested information about the managed hosts to Ansible.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The following script is a dynamic inventory script run by Ansible. This example script is written in Python. It takes two input arguments, &amp;lt;code&amp;gt;--host=X&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;--list&amp;lt;/code&amp;gt;, and interacts with Vagrant to return the requested information about the managed hosts to Ansible.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l150&quot;&gt;Line 150:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 150:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;ansible -i dynamic_vagrant.py -u ubuntu myvagrantbox -m ping&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;ansible -i dynamic_vagrant.py -u ubuntu myvagrantbox -m ping&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;=Flag=&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;=Flag=&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;{{AnsibleFlag}}&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;{{AnsibleFlag}}&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Admin</name></author>
	</entry>
	<entry>
		<id>https://charlesreid1.com/w/index.php?title=Ansible/Vagrant/Dynamic_Inventory&amp;diff=25493&amp;oldid=prev</id>
		<title>Admin at 22:57, 14 November 2018</title>
		<link rel="alternate" type="text/html" href="https://charlesreid1.com/w/index.php?title=Ansible/Vagrant/Dynamic_Inventory&amp;diff=25493&amp;oldid=prev"/>
		<updated>2018-11-14T22:57:16Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 22:57, 14 November 2018&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l150&quot;&gt;Line 150:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 150:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;ansible -i dynamic_vagrant.py -u ubuntu myvagrantbox -m ping&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;ansible -i dynamic_vagrant.py -u ubuntu myvagrantbox -m ping&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;=Flag=&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;{{AnsibleFlag}}&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Admin</name></author>
	</entry>
	<entry>
		<id>https://charlesreid1.com/w/index.php?title=Ansible/Vagrant/Dynamic_Inventory&amp;diff=25362&amp;oldid=prev</id>
		<title>Admin: Created page with &quot;{{Main|Ansible/Vagrant}}  This page covers how to manage a dynamic inventory file when using Vagrant boxes. This avoids the need to edit a hosts file by hand.   ==Static vs dy...&quot;</title>
		<link rel="alternate" type="text/html" href="https://charlesreid1.com/w/index.php?title=Ansible/Vagrant/Dynamic_Inventory&amp;diff=25362&amp;oldid=prev"/>
		<updated>2018-11-11T01:43:25Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;{{Main|Ansible/Vagrant}}  This page covers how to manage a dynamic inventory file when using Vagrant boxes. This avoids the need to edit a hosts file by hand.   ==Static vs dy...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Main|Ansible/Vagrant}}&lt;br /&gt;
&lt;br /&gt;
This page covers how to manage a dynamic inventory file when using Vagrant boxes. This avoids the need to edit a hosts file by hand.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Static vs dynamic inventory==&lt;br /&gt;
&lt;br /&gt;
[[Ansible/Vagrant/Static Inventory]] - static inventory requires the hosts file (containing the list of machines that Ansible is managing) be kept up to date by hand. This can be a burden if details are changing or if things are allocated automatically.&lt;br /&gt;
&lt;br /&gt;
[[Ansible/Vagrant/Dynamic Inventory]] - dynamic inventory uses something like an API or a database to obtain information about the machines that Ansible is managing. This makes scaling and generalization much easier.&lt;br /&gt;
&lt;br /&gt;
==Managing a dynamic inventory file using Vagrant boxes==&lt;br /&gt;
&lt;br /&gt;
The following script is a dynamic inventory script run by Ansible. This example script is written in Python. It takes two input arguments, &amp;lt;code&amp;gt;--host=X&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;--list&amp;lt;/code&amp;gt;, and interacts with Vagrant to return the requested information about the managed hosts to Ansible.&lt;br /&gt;
&lt;br /&gt;
===Getting List of Hosts===&lt;br /&gt;
&lt;br /&gt;
Start with how to get a list of hosts for the &amp;lt;code&amp;gt;--list&amp;lt;/code&amp;gt; flag.&lt;br /&gt;
&lt;br /&gt;
Vagrant provides a command to show info about all running vagrant machines:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vagrant status&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This info can be used to create the list of hosts for the dynamic inventory file. A more convenient format for parsing is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vagrant status --machine-readable&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which prints everything in CSV format. The code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def list_running_hosts():&lt;br /&gt;
    cmd = &amp;quot;vagrant status --machine-readable&amp;quot;&lt;br /&gt;
    status = subprocess.check_output(cmd.split()).rstrip().decode(&amp;#039;utf-8&amp;#039;)&lt;br /&gt;
    hosts = []&lt;br /&gt;
    for line in status.split(&amp;#039;\n&amp;#039;):&lt;br /&gt;
        (_, host, key, value) = line.split(&amp;#039;,&amp;#039;)[:4]&lt;br /&gt;
        if key == &amp;#039;state&amp;#039; and value == &amp;#039;running&amp;#039;:&lt;br /&gt;
            hosts.append(host)&lt;br /&gt;
    return hosts&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Getting Machine Configuration Details===&lt;br /&gt;
&lt;br /&gt;
Now we need to get host-specific details for the &amp;lt;code&amp;gt;--host=X&amp;lt;/code&amp;gt; flag.&lt;br /&gt;
&lt;br /&gt;
Next we can use the &amp;lt;code&amp;gt;vagrant ssh-config&amp;lt;/code&amp;gt; command to get info about the machine-specific configuration details of each machine, which is necessary for Ansible to connect to them. To parse SSH config files, we can use a Python library called pamiko.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def get_host_details(host):&lt;br /&gt;
    cmd = &amp;quot;vagrant ssh-config {}&amp;quot;.format(host)&lt;br /&gt;
    p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)&lt;br /&gt;
    config = paramiko.SSHConfig()&lt;br /&gt;
    config.parse(p.stdout)&lt;br /&gt;
    c = config.lookup(host)&lt;br /&gt;
    return {&amp;#039;ansible_host&amp;#039;: c[&amp;#039;hostname&amp;#039;],&lt;br /&gt;
            &amp;#039;ansible_port&amp;#039;: c[&amp;#039;port&amp;#039;],&lt;br /&gt;
            &amp;#039;ansible_user&amp;#039;: c[&amp;#039;user&amp;#039;],&lt;br /&gt;
            &amp;#039;ansible_private_key_file&amp;#039;: c[&amp;#039;identityfile&amp;#039;][0]}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Parsing user arguments===&lt;br /&gt;
&lt;br /&gt;
A quick argparser tutorial for the flags we&amp;#039;re interested in parsing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def parse_args():&lt;br /&gt;
    parser = argparse.ArgumentParser(description=&amp;quot;Vagrant inventory script&amp;quot;)&lt;br /&gt;
    group = parser.add_mutually_exclusive_group(required=True)&lt;br /&gt;
    group.add_argument(&amp;#039;--list&amp;#039;, action=&amp;#039;store_true&amp;#039;)&lt;br /&gt;
    group.add_argument(&amp;#039;--host&amp;#039;)&lt;br /&gt;
    return parser.parse_args()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can call this function and use args.list or args.host to access the values of the flags.&lt;br /&gt;
&lt;br /&gt;
===Putting it all together===&lt;br /&gt;
&lt;br /&gt;
Here is the final dynamic inventory script:&lt;br /&gt;
&lt;br /&gt;
(In the ansible repo under contrib/inventory: https://github.com/ansible/ansible/tree/devel/contrib/inventory)&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;dynamic_vagrant.py&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/env python&lt;br /&gt;
# Adapted from Mark Mandel&amp;#039;s implementation&lt;br /&gt;
# https://github.com/ansible/ansible/blob/stable-2.1/contrib/inventory/vagrant.py&lt;br /&gt;
# License: GNU General Public License, Version 3 &amp;lt;http://www.gnu.org/licenses/&amp;gt;&lt;br /&gt;
import argparse&lt;br /&gt;
import json&lt;br /&gt;
import paramiko&lt;br /&gt;
import subprocess&lt;br /&gt;
import sys&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def parse_args():&lt;br /&gt;
    parser = argparse.ArgumentParser(description=&amp;quot;Vagrant inventory script&amp;quot;)&lt;br /&gt;
    group = parser.add_mutually_exclusive_group(required=True)&lt;br /&gt;
    group.add_argument(&amp;#039;--list&amp;#039;, action=&amp;#039;store_true&amp;#039;)&lt;br /&gt;
    group.add_argument(&amp;#039;--host&amp;#039;)&lt;br /&gt;
    return parser.parse_args()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def list_running_hosts():&lt;br /&gt;
    cmd = &amp;quot;vagrant status --machine-readable&amp;quot;&lt;br /&gt;
    status = subprocess.check_output(cmd.split()).rstrip().decode(&amp;#039;utf-8&amp;#039;)&lt;br /&gt;
    hosts = []&lt;br /&gt;
    for line in status.split(&amp;#039;\n&amp;#039;):&lt;br /&gt;
        (_, host, key, value) = line.split(&amp;#039;,&amp;#039;)[:4]&lt;br /&gt;
        if key == &amp;#039;state&amp;#039; and value == &amp;#039;running&amp;#039;:&lt;br /&gt;
            hosts.append(host)&lt;br /&gt;
    return hosts&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def get_host_details(host):&lt;br /&gt;
    cmd = &amp;quot;vagrant ssh-config {}&amp;quot;.format(host)&lt;br /&gt;
    p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)&lt;br /&gt;
    config = paramiko.SSHConfig()&lt;br /&gt;
    config.parse(p.stdout)&lt;br /&gt;
    c = config.lookup(host)&lt;br /&gt;
    return {&amp;#039;ansible_host&amp;#039;: c[&amp;#039;hostname&amp;#039;],&lt;br /&gt;
            &amp;#039;ansible_port&amp;#039;: c[&amp;#039;port&amp;#039;],&lt;br /&gt;
            &amp;#039;ansible_user&amp;#039;: c[&amp;#039;user&amp;#039;],&lt;br /&gt;
            &amp;#039;ansible_private_key_file&amp;#039;: c[&amp;#039;identityfile&amp;#039;][0]}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    args = parse_args()&lt;br /&gt;
    if args.list:&lt;br /&gt;
        hosts = list_running_hosts()&lt;br /&gt;
        json.dump({&amp;#039;vagrant&amp;#039;: hosts}, sys.stdout)&lt;br /&gt;
    else:&lt;br /&gt;
        details = get_host_details(args.host)&lt;br /&gt;
        json.dump(details, sys.stdout)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;#039;__main__&amp;#039;:&lt;br /&gt;
    main()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Running the dynamic inventory script===&lt;br /&gt;
&lt;br /&gt;
Once we&amp;#039;ve written the dynamic inventory script and we&amp;#039;re ready to run Ansible using it, we can pass the inventory script using the -i flag:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ansible -i dynamic_vagrant.py -u ubuntu myvagrantbox -m ping&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>
	</entry>
</feed>