<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Ansible on The Network Stack</title>
    <link>https://thenetworkstack.com/tags/ansible/</link>
    <description>Recent content in Ansible on The Network Stack</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <lastBuildDate>Mon, 26 Mar 2018 14:25:52 +0000</lastBuildDate>
    <atom:link href="https://thenetworkstack.com/tags/ansible/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Ansible 2.5 - New Network Modules</title>
      <link>https://thenetworkstack.com/ansible-2-5-new-network-modules/</link>
      <pubDate>Mon, 26 Mar 2018 14:25:52 +0000</pubDate>
      <guid>https://thenetworkstack.com/ansible-2-5-new-network-modules/</guid>
      <description>&lt;p&gt;Ansible 2.5 just came out, and with it comes new network modules (and the network_cli and netconf connectivity methods). The new modules allow us to manage certain IOS configurations without always depending on the &amp;ldquo;ios_config&amp;rdquo; module, while the network_cli connectivity method means we don&amp;rsquo;t always need to configure a &amp;ldquo;provider&amp;rdquo; for every network playbook. These new modules and connectivity methods allow networking configuration management to look and feel similar to our server management playbooks, which is great for cross-functional teams and reusability.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>Ansible 2.5 just came out, and with it comes new network modules (and the network_cli and netconf connectivity methods). The new modules allow us to manage certain IOS configurations without always depending on the &ldquo;ios_config&rdquo; module, while the network_cli connectivity method means we don&rsquo;t always need to configure a &ldquo;provider&rdquo; for every network playbook. These new modules and connectivity methods allow networking configuration management to look and feel similar to our server management playbooks, which is great for cross-functional teams and reusability.</p>
<blockquote>
<p><strong>Ansible 2.5 Release Info:</strong><br>
<a href="https://www.ansible.com/blog/coming-soon-networking-features-in-ansible-2.5">New Networking Features in Ansible 2.5</a><br>
<a href="http://docs.ansible.com/ansible/devel/modules/list_of_network_modules.html">List of All Network Modules in Ansible 2.5</a><br>
<a href="http://docs.ansible.com/ansible/devel/network/user_guide/network_best_practices_2.5.html">Network Best Practices for Ansible 2.5</a></p>
</blockquote>
<p>For a quick introduction, I&rsquo;m going to take my previously discussed <a href="/ansible-vlan-provisioning/">VLAN provisioning playbook</a> and convert it to Ansible 2.5 syntax.</p>
<p>First, remember to upgrade to Ansible 2.5. If you have already have <a href="/ansible-installation/">ansible installed</a>, then just upgrade with:</p>
<pre tabindex="0"><code>apt-get update &amp;&amp; apt-get install ansible
</code></pre><p>Our first major change is going to use &ldquo;group_vars&rdquo; to define connection settings for our network devices. By default, Ansible will read any variable in group_vars/all.yml, group_vars/&lt;group_name&gt; and host_vars/&lt;host_name&gt; when a playbook is executed.  Since we are defining how to connect to <strong>all</strong> our Cisco IOS devices, I am going to put our connection variables in <strong>groups_vars/ios-switches.yml</strong></p>
<pre tabindex="0"><code>ansible_connection: network_cli
ansible_network_os: ios
ansible_user: ciscoadmin
ansible_ssh_pass: cisco123
</code></pre><p>To use the network_cli connection, you must specify the &ldquo;ansible_network_os&rdquo;. Also, please <strong>do not</strong> store your password in plain-text and instead use Ansible Vault, but to keep this example simple, we are using the password &ldquo;cisco123&rdquo;.</p>
<p>Since our &ldquo;ansible_connection&rdquo; is defined in group_vars, we no longer need the &ldquo;connection: local&rdquo; parameter in our playbook.</p>
<p>Next, instead of using the &ldquo;ios_config&rdquo; module to create and name VLAN&rsquo;s, let&rsquo;s use the new <strong>ios_vlan</strong> module.</p>
<pre tabindex="0"><code>---
- hosts: ios-switches
  vars:
    vlan_id: 998
    vlan_name: Ansible_VLAN
    
tasks:
    - name: Provision VLAN
      ios_vlan:
        vlan_id: &#34;{{ vlan_id }}&#34;
        name: &#34;{{ vlan_name }}&#34;
        state: present

    - name: Show VLAN
      ios_command:
        commands: show vlan brief
      register: show_vlan

    - debug: var=show_vlan.stdout_lines
</code></pre><p>As you can see the <strong>ios_vlan</strong> module simplifies our VLAN provisioning process, since we can define the VLAN ID and the VLAN NAME in a single task. The &ldquo;state&rdquo; of the VLAN is not required to create a VLAN, but I included it to show how the new modules use declaritive statements to make creating/removing configuration easier.  If we want to remove a VLAN, just change the &ldquo;state&rdquo; to <strong>absent</strong>. I also left in our &ldquo;show vlan&rdquo; task from last time, so we can verify our configuration change without logging into the switch manually.</p>
<p>Now to run the playbook:</p>
<pre tabindex="0"><code>root@ubuntu:/etc/ansible# ansible-playbook network_new-vlan.yml

PLAY [ios-switches] *************************************************************************************************************************************

TASK [Provision VLAN] ***********************************************************************************************************************************
changed: [10.170.200.2]

TASK [Show VLAN] ****************************************************************************************************************************************
ok: [10.170.200.2]

TASK [debug] ********************************************************************************************************************************************
ok: [10.170.200.2] =&gt; {
    &#34;show_vlan.stdout_lines&#34;: [
        [
            &#34;VLAN Name                             Status    Ports&#34;,
            &#34;---- -------------------------------- --------- -------------------------------&#34;,
            &#34;1    default                          active    Gi0/10, Gi0/11, Gi0/12&#34;,
            &#34;10   FW_VLAN                          active    Gi0/3, Gi0/4, Gi0/5, Gi0/6, Gi0/7&#34;,
            &#34;20   Data_VLAN                        active    Gi0/1, Gi0/2&#34;,
            &#34;30   Voice_VLAN                       active    Gi0/1, Gi0/2, Gi0/3, Gi0/4, Gi0/5, Gi0/6, Gi0/7&#34;,
            &#34;998  Ansible_VLAN                     active    &#34;,
            &#34;1002 fddi-default                     act/unsup &#34;,
            &#34;1003 token-ring-default               act/unsup &#34;,
            &#34;1004 fddinet-default                  act/unsup &#34;,
            &#34;1005 trnet-default                    act/unsup&#34;
        ]
    ]
}

PLAY RECAP **********************************************************************************************************************************************
10.170.200.2                : ok=4    changed=1    unreachable=0    failed=0
</code></pre><p>Success!! While this is great for individual VLAN changes, the real power comes when you define all your environment VLAN&rsquo;s in group_var or host_var files, and looping through the &ldquo;ios_vlan&rdquo; task for every VLAN required.  I&rsquo;ll save that for a future post!</p>
<p>You can find this playbook (including ansible.cfg and directory structure) on my github: <a href="https://github.com/dkuchenski/thenetworkstack-ansible/tree/master/vlan-provisioning-2.5">https://github.com/dkuchenski/thenetworkstack-ansible/tree/master/vlan-provisioning-2.5</a></p>
]]></content:encoded>
    </item>
    <item>
      <title>Ansible - BadHostKeyException</title>
      <link>https://thenetworkstack.com/ansible-badhostkeyexception/</link>
      <pubDate>Sat, 27 Jan 2018 18:58:42 +0000</pubDate>
      <guid>https://thenetworkstack.com/ansible-badhostkeyexception/</guid>
      <description>&lt;p&gt;When executing an ansible playbook, you may get the following error:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;An exception occurred during task execution. To see the full traceback, use -vvv. The error was: paramiko.BadHostKeyException: Host key &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; server switch-name does not match!
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This probably happened after you changed the name of your network device, and generated new RSA keys.  Or, if you are using DNS device names in ansible, resolve the name of your network device from your ansible server and verify the DNS resolution is correct.  To fix the issue, edit your &amp;ldquo;known_hosts&amp;rdquo; file and remove the key associated with the network device throwing the error:&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>When executing an ansible playbook, you may get the following error:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">An exception occurred during task execution. To see the full traceback, use -vvv. The error was: paramiko.BadHostKeyException: Host key <span class="k">for</span> server switch-name does not match!
</span></span></code></pre></div><p>This probably happened after you changed the name of your network device, and generated new RSA keys.  Or, if you are using DNS device names in ansible, resolve the name of your network device from your ansible server and verify the DNS resolution is correct.  To fix the issue, edit your &ldquo;known_hosts&rdquo; file and remove the key associated with the network device throwing the error:</p>
<ol>
<li>Open your known_hosts file</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">vi ~/.ssh/known_hosts
</span></span></code></pre></div><ol>
<li>Search for the line with the network device RSA key</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">/network_device_name
</span></span></code></pre></div><ol>
<li>Delete the RSA key for that network device (&ldquo;dd&rdquo; deletes the entire line)</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">dd
</span></span></code></pre></div><ol>
<li>Save the known_hosts file</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="c1">#Press the escape key first</span>
</span></span><span class="line"><span class="cl">:wq
</span></span></code></pre></div><p>If you don&rsquo;t want ansible to perform this check, edit your <strong>ansible.cfg</strong> file with:</p>
<pre tabindex="0"><code>host_key_checking = False
</code></pre>]]></content:encoded>
    </item>
    <item>
      <title>Ansible - VLAN Provisioning</title>
      <link>https://thenetworkstack.com/ansible-vlan-provisioning/</link>
      <pubDate>Wed, 11 Oct 2017 05:44:05 +0000</pubDate>
      <guid>https://thenetworkstack.com/ansible-vlan-provisioning/</guid>
      <description>&lt;p&gt;&lt;em&gt;Note: There is a &lt;a href=&#34;https://thenetworkstack.com/ansible-2-5-new-network-modules/&#34;&gt;newer guide&lt;/a&gt; for VLAN provisioning with Ansible 2.5&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Ansible is such a powerfull tool that it can be easy to get lost in all the possibilities. Running your &amp;ldquo;network infrastructure as code&amp;rdquo; with full configs auto-generated and checked into git is the dream, but we can &lt;em&gt;start simple&lt;/em&gt; with automating time-consuming tasks. This post will focus on getting ansible up and running with a playbook to configure new vlans across your switches.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p><em>Note: There is a <a href="/ansible-2-5-new-network-modules/">newer guide</a> for VLAN provisioning with Ansible 2.5</em></p>
<p>Ansible is such a powerfull tool that it can be easy to get lost in all the possibilities. Running your &ldquo;network infrastructure as code&rdquo; with full configs auto-generated and checked into git is the dream, but we can <em>start simple</em> with automating time-consuming tasks. This post will focus on getting ansible up and running with a playbook to configure new vlans across your switches.</p>
<blockquote>
<p><a href="/ansible-installation/">see my guide for installing Ansible on Ubuntu 16.04</a></p>
</blockquote>
<p>First, we need to build our Ansible <strong>hosts</strong> file which is an inventory list of our network devices. We can categorize and group devices within the hosts file, which makes configuring the right devices easier down the road.</p>
<pre tabindex="0"><code>root@ubuntu:~# cd /etc/ansible/
root@ubuntu:/etc/ansible# vi hosts
# This is the default ansible &#39;hosts&#39; file.
#
[networkdevices:children]
switches
routers

[switches]
switch01.thenetworkstack.com
switch02 ansible_host=10.170.200.1
10.170.200.2

[routers]
</code></pre><p>I created a parent group called <strong>networkdevices</strong>, and then made two &ldquo;child&rdquo; groups called <strong>switches</strong> and <strong>routers</strong>. For the VLAN playbook we are going to create, we only really care about the <strong>switches</strong> group, but I wanted to show how you can easily create a grouping structure. For defining your actual network devices, there are three main options shown above in the <strong>switches</strong> group:</p>
<ul>
<li>The fully-qualified domain name of the device (assuming you have DNS working)</li>
<li>A local device name, and define the device ip address with the &ldquo;ansible_host&rdquo; variable</li>
<li>The ip address of the device</li>
</ul>
<p>After you configure your Ansible hosts file with a network device or two, we can start building a <strong>playbook</strong>. A playbook is Ansible&rsquo;s way of defining a set of tasks. Within a playbook, we will use <strong>network modules</strong> to run different types of commands (for example, <em>show</em> vs <em>config</em>) on our devices. Ansible comes with support for most major network devices, listed here: <a href="http://docs.ansible.com/ansible/latest/list_of_network_modules.html">http://docs.ansible.com/ansible/latest/list_of_network_modules.html</a></p>
<p>Let&rsquo;s get started with our playbook. First, give your playbook a name, and make sure it ends in &ldquo;.yml&rdquo;. To start your YAML file, the first line should be &ldquo;&mdash;&rdquo;, and remember <strong>spaces matter</strong>. If you get syntax errors, check your spacing on each line:</p>
<pre tabindex="0"><code>root@ubuntu:/etc/ansible# vi network_new-vlan.yml
---
- hosts: switches
  connection: local

  tasks:
    - name: Show VLAN
      ios_command:
        commands: show vlan brief
      register: show_vlan

    - debug: var=show_vlan.stdout_lines
</code></pre><p>This simple playbook just runs a command (show vlan brief), registers the output of that command to a variable (show_vlan) and then displays the formatted value of the variable (stdout_lines).</p>
<p>You may notice we don&rsquo;t have any credentials stored in this playbook - we will provide them when we run the playbook (I&rsquo;m using <em>cisco</em> as my username):</p>
<pre tabindex="0"><code>root@ubuntu:/etc/ansible# ansible-playbook network_new-vlan.yml -u cisco -k
SSH password:

PLAY [switches] *******************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************
ok: [10.170.200.2]

TASK [Show VLAN] *****************************************************************************************************************************************************************
ok: [10.170.200.2]

TASK [debug] **********************************************************************************************************************************************************************
ok: [10.170.200.2] =&gt; {
    &#34;show_vlan.stdout_lines&#34;: [
        [
            &#34;VLAN Name                             Status    Ports&#34;,
            &#34;---- -------------------------------- --------- -------------------------------&#34;,
            &#34;1    default                          active    Gi0/10, Gi0/11, Gi0/12&#34;,
            &#34;10   FW_VLAN                          active    Gi0/3, Gi0/4, Gi0/5, Gi0/6, Gi0/7&#34;,
            &#34;20   Data_VLAN                        active    Gi0/1, Gi0/2&#34;,
            &#34;30   Voice_VLAN                       active    Gi0/1, Gi0/2, Gi0/3, Gi0/4, Gi0/5, Gi0/6, Gi0/7&#34;,
            &#34;1002 fddi-default                     act/unsup &#34;,
            &#34;1003 token-ring-default               act/unsup &#34;,
            &#34;1004 fddinet-default                  act/unsup &#34;,
            &#34;1005 trnet-default                    act/unsup&#34;
        ]
    ]
}

PLAY RECAP ************************************************************************************************************************************************************************
10.170.200.2                : ok=3    changed=0    unreachable=0    failed=0
</code></pre><p>Pretty simple! I currently only have a single device in my Ansible hosts file, but with multiple devices you can issue show commands across every switch in your network. You can also modify the playbook above with any &ldquo;show&rdquo; command.</p>
<p>We used the &ldquo;ios_command&rdquo; network module above, but to actually make changes, we will need to use the &ldquo;ios_config&rdquo; network module.</p>
<p>When creating playbooks for common tasks (like creating a vlan), I try to use variables as much as possible so the playbook is easy to reuse. You can see below that I define the <strong>vlan_id</strong> and <strong>vlan_name</strong> variable, which makes them very easy to change when needed - you don&rsquo;t need to edit anything else in the playbook.</p>
<pre tabindex="0"><code>---
- hosts: switches
  connection: local
  vars:
    vlan_id: 998
    vlan_name: Ansible_VLAN

  tasks:
    - name: Configure VLAN ID
      ios_config:
        lines:
          - vlan {{ vlan_id }}

    - name: Configure VLAN Name
      ios_config:
        lines:
          - name {{ vlan_name }}
        parents: vlan {{ vlan_id }}

    - name: Show VLAN
      ios_command:
        commands: show vlan brief
      register: show_vlan

    - debug: var=show_vlan.stdout_lines
</code></pre><p>As you can see, we added the VLAN configuration <em>before</em> our &ldquo;Show VLAN&rdquo; task. There are two parts to configuring a VLAN - the VLAN ID and the VLAN name. The first task configures the VLAN ID on the switch, then the second task tells Ansible to configure the VLAN name under the VLAN ID we just created. Since the VLAN name is part of a heirarchical configuration in Cisco IOS, we can use the &ldquo;parents&rdquo; feature in Ansible to let it know that the name is placed under the VLAN ID configuration.</p>
<p>After the VLAN configuration is completed, our &ldquo;Show VLAN&rdquo; task will display the VLAN database with our newly created VLAN.</p>
<pre tabindex="0"><code>root@ubuntu:/etc/ansible# ansible-playbook network_new-vlan.yml -u admin -k
SSH password:

PLAY [switches] *******************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************
ok: [10.170.200.2]

TASK [Configure VLAN ID] **********************************************************************************************************************************************************
changed: [10.170.200.2]

TASK [Configure VLAN Name] ********************************************************************************************************************************************************
changed: [10.170.200.2]

TASK [Show VLAN] ******************************************************************************************************************************************************************
ok: [10.170.200.2]

TASK [debug] **********************************************************************************************************************************************************************
ok: [10.170.200.2] =&gt; {
    &#34;show_vlan.stdout_lines&#34;: [
        [
            &#34;VLAN Name                             Status    Ports&#34;,
            &#34;---- -------------------------------- --------- -------------------------------&#34;,
            &#34;1    default                          active    Gi0/10, Gi0/11, Gi0/12&#34;,
            &#34;10   FW_VLAN                          active    Gi0/3, Gi0/4, Gi0/5, Gi0/6, Gi0/7&#34;,
            &#34;20   Data_VLAN                        active    Gi0/1, Gi0/2&#34;,
            &#34;30   Voice_VLAN                       active    Gi0/1, Gi0/2, Gi0/3, Gi0/4, Gi0/5, Gi0/6, Gi0/7&#34;,
            &#34;998  Ansible_VLAN                     active    &#34;,
            &#34;1002 fddi-default                     act/unsup &#34;,
            &#34;1003 token-ring-default               act/unsup &#34;,
            &#34;1004 fddinet-default                  act/unsup &#34;,
            &#34;1005 trnet-default                    act/unsup&#34;
        ]
    ]
}

PLAY RECAP ************************************************************************************************************************************************************************
10.170.200.2                : ok=5    changed=2    unreachable=0    failed=0

root@ubuntu:/etc/ansible#
</code></pre><p>Success!!</p>
<p>Now when a new VLAN needs to be provisioned, just change the <strong>vlan_id</strong> and <strong>vlan_name</strong> vars, and you can configure a VLAN across your entire network in less than a minute.</p>
<p>You can also find the <strong>hosts</strong> file and <strong>playbook</strong> on my github: <a href="https://github.com/dkuchenski/thenetworkstack-ansible/tree/master/vlan-provisioning">https://github.com/dkuchenski/thenetworkstack-ansible/tree/master/vlan-provisioning</a></p>
]]></content:encoded>
    </item>
    <item>
      <title>Ansible Installation - Ubuntu 16.04</title>
      <link>https://thenetworkstack.com/ansible-installation/</link>
      <pubDate>Sun, 23 Jul 2017 07:21:34 +0000</pubDate>
      <guid>https://thenetworkstack.com/ansible-installation/</guid>
      <description>This post will focus on getting Ansible up and running on a Ubuntu 16.04 server (in three steps).</description>
      <content:encoded><![CDATA[<p>For network automation, I believe the best tool out there right now is Ansible. Ansible is open-source, agentless, has a large community, and is easy to get started quickly. For the major networking vendors, there are already pre-built modules making it simple to interface with their equipment. This post will focus on getting Ansible up and running on a Ubuntu 16.04 server (<em>in three steps</em>), and following posts will show how to use Ansible to automate network configuration.</p>
<p>Assuming this is a fresh install, first install &lsquo;software-properties-common&rsquo;:<br>
<code>sudo apt-get install software-properties-common</code></p>
<p>Then add Ansible&rsquo;s PPA (Personal Project Archive) to Ubuntu:<br>
<code>sudo apt-add-repository --yes --update ppa:ansible/ansible</code></p>
<p>Install Ansible:<br>
<code>sudo apt-get install ansible</code></p>
<p><strong>And that&rsquo;s it, your Ansible installation is complete!</strong></p>
<p>The Ansible installation should have included python, verify it is installed:<br>
<code>python -V</code></p>
<p>You can also verify the version of Ansible installed:<br>
<code>ansible --version</code></p>
<p>Ansible: <a href="https://www.ansible.com/">https://www.ansible.com/</a><br>
Ansible Getting Started: <a href="https://www.ansible.com/get-started">https://www.ansible.com/get-started</a><br>
Network Modules: <a href="http://docs.ansible.com/ansible/latest/list_of_network_modules.html">http://docs.ansible.com/ansible/latest/list_of_network_modules.html</a></p>
]]></content:encoded>
    </item>
  </channel>
</rss>
