<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CLI commands &#8211; Best DevOps</title>
	<atom:link href="https://www.bestdevops.com/tag/cli-commands/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.bestdevops.com</link>
	<description>Lets Learn, Do it &#38; Share! Thats a Best DevOps!!!</description>
	<lastBuildDate>Wed, 14 Oct 2026 02:44:58 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.3</generator>
	<item>
		<title>Network Automation with Ansible: A Practical Introduction for Network Engineers</title>
		<link>https://www.bestdevops.com/an-introduction-to-network-automation-with-ansible/</link>
					<comments>https://www.bestdevops.com/an-introduction-to-network-automation-with-ansible/#respond</comments>
		
		<dc:creator><![CDATA[anil]]></dc:creator>
		<pubDate>Tue, 18 Feb 2020 05:23:03 +0000</pubDate>
				<category><![CDATA[Ansible]]></category>
		<category><![CDATA[CLI commands]]></category>
		<category><![CDATA[DevOps]]></category>
		<category><![CDATA[IT-technolgoy]]></category>
		<category><![CDATA[network automation]]></category>
		<category><![CDATA[Software-Market]]></category>
		<guid isPermaLink="false">http://www.bestdevops.com/?p=10666</guid>

					<description><![CDATA[Learn how to use Ansible for network automation, including directory structure, playbook design, and a working example that collects ARP table data from a Cisco IOS switch.]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Ansible is both an automation language and the engine that runs it. For network engineers, it offers an unusually gentle on-ramp: the configuration syntax is human-readable YAML, the documentation is thorough, and the large community has produced a wealth of tutorials and modules that cover most major vendor platforms.</p>



<h2 class="wp-block-heading">Why Ansible Works Well for Network Automation</h2>



<p class="wp-block-paragraph">Two properties make Ansible particularly attractive in a network context:</p>



<ul class="wp-block-list">
<li><strong>Agentless operation.</strong> Ansible communicates with network devices over SSH (or a vendor API where available). There is nothing to install on the managed device — a major advantage when you are working with switches and routers that do not expose a general-purpose OS.</li>
<li><strong>Low barrier to entry.</strong> YAML playbooks read almost like plain English. Network engineers who are comfortable with incremental learning — picking up IOS features as needed, for example — adapt quickly to Ansible&#8217;s model.</li>
</ul>



<p class="wp-block-paragraph">Ansible runs on a <em>control node</em> (a Linux host or WSL instance) and issues commands to one or many network devices simultaneously. Its behaviour is governed by plain-text configuration files, making it easy to version-control your automation alongside your other infrastructure code.</p>



<h2 class="wp-block-heading">Key Concepts Before You Start</h2>



<ul class="wp-block-list">
<li><strong>Inventory</strong> — a list of the devices Ansible manages, organised into groups.</li>
<li><strong>Playbook</strong> — a YAML file describing one or more <em>plays</em>, each containing one or more <em>tasks</em>.</li>
<li><strong>Module</strong> — a reusable unit of work. Network modules such as <code>ios_command</code>, <code>eos_command</code>, and the vendor-agnostic <code>cli_command</code> handle interaction with specific platforms.</li>
<li><strong>Variables</strong> — stored in <code>group_vars</code> and <code>host_vars</code> directories and merged at run time, giving you fine-grained control over per-device or per-group settings.</li>
</ul>



<h2 class="wp-block-heading">Recommended Directory Structure</h2>



<p class="wp-block-paragraph">You can run Ansible with a single flat file, but that quickly becomes unmanageable. A structured layout separates concerns and scales cleanly as your device count grows. The following layout is used in the examples below:</p>


<pre class="wp-block-code"><span><code class="hljs language-css">.
├── <span class="hljs-selector-tag">ansible</span><span class="hljs-selector-class">.cfg</span>
├── <span class="hljs-selector-tag">group_vars</span>
│   └── <span class="hljs-selector-tag">IOS_switches</span><span class="hljs-selector-class">.yml</span>
├── <span class="hljs-selector-tag">host_vars</span>
│   └── <span class="hljs-selector-tag">test_sw</span><span class="hljs-selector-class">.yml</span>
├── <span class="hljs-selector-tag">inventory</span><span class="hljs-selector-class">.yml</span>
└── <span class="hljs-selector-tag">sh_arp</span><span class="hljs-selector-class">.yml</span></code></span></pre>


<h3 class="wp-block-heading">ansible.cfg</h3>



<p class="wp-block-paragraph">This INI-format file overrides Ansible defaults. The most important settings for network automation are disabling fact gathering (which is designed for Linux hosts, not network devices) and pointing Ansible at your inventory file.</p>


<pre class="wp-block-code"><span><code class="hljs language-php">&#91;defaults]
gathering = explicit
inventory = inventory.yml
retry_files_enabled = <span class="hljs-keyword">False</span></code></span></pre>


<h3 class="wp-block-heading">group_vars/IOS_switches.yml</h3>



<p class="wp-block-paragraph">Variables in <code>group_vars</code> apply to every host in the named group. Here we set the connection type to <code>network_cli</code>, declare the OS as <code>ios</code>, and supply credentials. In production, use Ansible Vault or SSH keys rather than plain-text passwords.</p>


<pre class="wp-block-code"><span><code class="hljs language-php">---
<span class="hljs-comment"># IOS_switches variables</span>
ansible_connection: network_cli
ansible_network_os: ios
ansible_become: yes
ansible_become_method: enable
ansible_become_password: <span class="hljs-string">"foobar"</span>

ansible_user: tester
ansible_ssh_pass: foobar</code></span></pre>


<h3 class="wp-block-heading">host_vars/test_sw.yml</h3>



<p class="wp-block-paragraph">Host-level variables override group variables for a specific device. A management IP address and hostname are the minimum useful entries.</p>


<pre class="wp-block-code"><span><code class="hljs language-css"><span class="hljs-selector-tag">---</span>
# <span class="hljs-selector-tag">test_sw</span> <span class="hljs-selector-tag">variables</span>
<span class="hljs-selector-tag">ipaddr</span>: 192<span class="hljs-selector-class">.168</span><span class="hljs-selector-class">.1</span><span class="hljs-selector-class">.108</span>
<span class="hljs-selector-tag">hostname</span>: <span class="hljs-selector-tag">test_sw</span></code></span></pre>


<h3 class="wp-block-heading">inventory.yml</h3>



<p class="wp-block-paragraph">The inventory file lists your devices and organises them into groups. A single device can belong to multiple groups, which is useful when you want to target switches by both vendor OS and network role.</p>


<pre class="wp-block-code"><span><code class="hljs">---
all:
  children:
    IOS_switches:
      hosts:
        test_sw:
    IOS_routers:
      hosts:
        router1:
    access:
      hosts:
        test_sw:
    core:
      hosts:
        router1:</code></span></pre>


<h2 class="wp-block-heading">Writing Your First Playbook: Collecting the ARP Table</h2>



<p class="wp-block-paragraph">A good first automation task is read-only data collection — it is safe, immediately useful, and gives you a baseline to compare against. The playbook below, <code>sh_arp.yml</code>, issues <code>show arp</code> to every host in the <code>IOS_switches</code> group and prints the output.</p>


<pre class="wp-block-code"><span><code class="hljs language-javascript">---
- name: Gather ARP data
  <span class="hljs-attr">hosts</span>: IOS_switches
  <span class="hljs-attr">tasks</span>:
    - name: <span class="hljs-string">"Task 1: Get output of show arp on IOS"</span>
      <span class="hljs-attr">ios_command</span>:
        commands: <span class="hljs-string">"show arp"</span>
      <span class="hljs-attr">register</span>: cli_result

    - name: <span class="hljs-string">"Task 2: Display stdout_lines&#91;0]"</span>
      <span class="hljs-attr">debug</span>:
        msg: <span class="hljs-string">"{{ cli_result.stdout_lines&#91;0] }}"</span></code></span></pre>


<p class="wp-block-paragraph">Key points about the playbook structure:</p>



<ul class="wp-block-list">
<li><code>ios_command</code> sends a CLI command to an IOS device and returns the output as a string.</li>
<li><code>register: cli_result</code> saves the module&#8217;s return value into a variable for use in later tasks.</li>
<li>The <code>debug</code> module prints the value of <code>cli_result.stdout_lines[0]</code> — a list of output lines from the first (and only) command issued.</li>
</ul>



<h3 class="wp-block-heading">Running the Playbook</h3>


<pre class="wp-block-code"><span><code class="hljs">$ ansible-playbook sh_arp.yml</code></span></pre>


<p class="wp-block-paragraph">Expected output:</p>


<pre class="wp-block-code"><span><code class="hljs language-php">PLAY &#91;Gather ARP data] ***************************************************

TASK &#91;Task <span class="hljs-number">1</span>: Get output of show arp on IOS] *****************************
ok: &#91;test_sw]

TASK &#91;Task <span class="hljs-number">2</span>: Display stdout_lines&#91;<span class="hljs-number">0</span>]] ***********************************
ok: &#91;test_sw] =&gt; {
    <span class="hljs-string">"msg"</span>: &#91;
        <span class="hljs-string">"Protocol  Address     Age (min)  Hardware Addr   Type  Interface"</span>,
        <span class="hljs-string">"Internet  192.168.1.108       -  bcc4.933e.49c0  ARPA  Vlan1"</span>,
        <span class="hljs-string">"Internet  192.168.1.126       5  f894.c220.2177  ARPA  Vlan1"</span>,
        <span class="hljs-string">"Internet  192.168.1.133       1  88c2.55fb.9ce7  ARPA  Vlan1"</span>,
        <span class="hljs-string">"Internet  192.168.1.138       0  a860.b60a.7a3a  ARPA  Vlan1"</span>
    ]
}

PLAY RECAP ***************************************************************
test_sw                    : ok=<span class="hljs-number">2</span>    changed=<span class="hljs-number">0</span>    unreachable=<span class="hljs-number">0</span>    failed=<span class="hljs-number">0</span></code></span></pre>


<p class="wp-block-paragraph">The <code>PLAY RECAP</code> line confirms two tasks completed successfully with no changes and no failures — exactly what you want from a read-only operation.</p>



<h2 class="wp-block-heading">Scaling Up: Running Across Many Devices</h2>



<p class="wp-block-paragraph">The real value of Ansible is not running a single command on one switch — it is running a set of tasks across dozens or hundreds of devices with a single command. Once your inventory is populated, the same playbook targets every matching host automatically.</p>



<p class="wp-block-paragraph">Practical use cases that are well suited to this pattern include:</p>



<ul class="wp-block-list">
<li><strong>Compliance auditing.</strong> Verify that device configurations match a known-good template — for example, that QoS policies or MPLS route targets are consistent across the fleet.</li>
<li><strong>Inventory and documentation.</strong> Collect <code>show version</code> output from every device and store it to disk. You immediately have a searchable record of firmware versions, model numbers, and serial numbers.</li>
<li><strong>MAC and IP address tracking.</strong> Save <code>show arp</code> and <code>show mac address-table dynamic</code> output, then use <code>grep</code> to locate any device on the network by its IP or MAC address across hundreds of output files.</li>
<li><strong>Configuration generation and deployment.</strong> Combine Ansible with the Jinja2 templating language to dynamically render device configurations and push them across the network.</li>
</ul>



<h2 class="wp-block-heading">Beyond the Basics</h2>



<p class="wp-block-paragraph">The example above covers a small fraction of what Ansible can do for network teams:</p>



<ul class="wp-block-list">
<li><strong>Ad hoc mode.</strong> Run a single module against an inventory group without writing a playbook — useful for quick checks during troubleshooting.</li>
<li><strong>Ansible Vault.</strong> Encrypt sensitive files such as credential variables so that secrets are never stored in plain text.</li>
<li><strong>Filters and plugins.</strong> Parse structured output, transform data, and extend Ansible&#8217;s capabilities beyond its built-in modules.</li>
<li><strong>Jinja2 templating.</strong> Generate device-specific configuration files from a single template, replacing manual copy-paste workflows.</li>
<li><strong>Roles.</strong> Package related tasks, variables, and templates into a reusable unit that can be shared across projects or published to Ansible Galaxy.</li>
</ul>



<p class="wp-block-paragraph">Nick Russo&#8217;s tutorial, <em>Automating Networks with Ansible the Right Way</em>, is frequently cited as a useful resource for engineers who want to move beyond basic playbooks into areas such as maintaining MPLS configurations with Ansible.</p>



<h2 class="wp-block-heading">Getting Started: Recommended Approach</h2>



<ol class="wp-block-list">
<li>Install Ansible on a Linux control node (or WSL on Windows).</li>
<li>Build the directory structure shown above with a single test device in your inventory.</li>
<li>Start with the read-only ARP collection playbook and confirm it works.</li>
<li>Expand to additional <code>show</code> commands and save results to files using the <code>copy</code> or <code>template</code> modules.</li>
<li>Once comfortable, explore write operations — start with non-production devices and always test with <code>--check</code> mode where supported.</li>
</ol>



<p class="wp-block-paragraph">Ansible&#8217;s official <em>Ansible for Network Automation</em> documentation section is the authoritative starting point. It links to a getting-started guide, an advanced topics guide, and a full list of network modules organised by vendor and platform.</p>



<h2 class="wp-block-heading">Summary</h2>



<p class="wp-block-paragraph">Ansible is a practical choice for network automation: it is agentless, uses SSH, and its YAML-based playbooks are no harder to learn than many networking concepts you already know. Starting with simple read-only tasks — collecting ARP tables, documenting firmware versions, auditing configurations — gives you immediate value with minimal risk. As your confidence grows, the same tool scales to configuration deployment, compliance enforcement, and full network lifecycle management.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.bestdevops.com/an-introduction-to-network-automation-with-ansible/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
