<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Ansible]]></title><description><![CDATA[Ansible]]></description><link>https://ayush-ansible.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 16 Sep 2026 04:49:59 GMT</lastBuildDate><atom:link href="https://ayush-ansible.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Performing Ad-Hoc Commands and Playbooks Using Patterns in Ansible]]></title><description><![CDATA[Introduction
Ansible is a powerful automation tool that enables infrastructure as code, configuration management, and application deployment with ease. This guide provides a detailed walkthrough for setting up Ansible, configuring inventory files, ex...]]></description><link>https://ayush-ansible.hashnode.dev/performing-ad-hoc-commands-and-playbooks-using-patterns-in-ansible</link><guid isPermaLink="true">https://ayush-ansible.hashnode.dev/performing-ad-hoc-commands-and-playbooks-using-patterns-in-ansible</guid><category><![CDATA[ansible]]></category><category><![CDATA[ansible-playbook]]></category><category><![CDATA[Ad-hoc commands in Ansible]]></category><dc:creator><![CDATA[Ayush Sharma]]></dc:creator><pubDate>Tue, 11 Mar 2025 18:30:08 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1741458927271/4288fcfc-a958-402a-9588-bdd2d8749727.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction</h2>
<p>Ansible is a powerful automation tool that enables infrastructure as code, configuration management, and application deployment with ease. This guide provides a detailed walkthrough for setting up Ansible, configuring inventory files, executing ad-hoc commands, and running playbooks on AWS EC2 instances. By following this guide, you will gain hands-on experience in using patterns effectively in Ansible.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>Before you begin, ensure that you have:</p>
<ol>
<li><p>Ansible installed on your local machine.</p>
</li>
<li><p>An AWS EC2 instance set up with a valid SSH key pair.</p>
</li>
<li><p>A user with necessary permissions to execute Ansible commands.</p>
</li>
</ol>
<h3 id="heading-verify-ansible-installation">Verify Ansible Installation</h3>
<p>To check if Ansible is installed, run:</p>
<pre><code class="lang-bash">ansible --version
</code></pre>
<p>If Ansible is not installed, follow the installation steps below.</p>
<h2 id="heading-step-1-install-ansible">Step 1: Install Ansible</h2>
<p>For Ubuntu-based systems, install Ansible using:</p>
<pre><code class="lang-bash">sudo apt update
sudo apt install ansible -y
</code></pre>
<p>For other systems, refer to the official Ansible documentation.</p>
<h2 id="heading-step-2-create-directory-structure">Step 2: Create Directory Structure</h2>
<p>Organize your project by creating a dedicated directory:</p>
<pre><code class="lang-bash">mkdir -p ~/ansible/aws_keypairs
<span class="hljs-built_in">cd</span> ~/ansible
</code></pre>
<h2 id="heading-step-3-copy-aws-key-pair">Step 3: Copy AWS Key Pair</h2>
<p>Ensure your AWS key pair is placed in the correct directory:</p>
<pre><code class="lang-bash">cp ~/aws_keypairs/Test.pem ~/ansible/aws_keypairs/
</code></pre>
<h2 id="heading-step-4-set-key-pair-permissions">Step 4: Set Key Pair Permissions</h2>
<p>Set secure file permissions for your SSH key:</p>
<pre><code class="lang-bash">chmod 400 ~/ansible/aws_keypairs/Test.pem
</code></pre>
<p>This prevents unauthorized access and ensures SSH connections work correctly.</p>
<h2 id="heading-step-5-create-an-inventory-file">Step 5: Create an Inventory File</h2>
<p>Create an inventory file to define target hosts:</p>
<pre><code class="lang-bash">nano ~/ansible/inventory.ini
</code></pre>
<p>Add the following content:</p>
<pre><code class="lang-ini"><span class="hljs-section">[webservers]</span>
ec2-13-127-126-77.ap-south-1.compute.amazonaws.com
ec2-13-201-132-221.ap-south-1.compute.amazonaws.com

<span class="hljs-section">[webservers:vars]</span>
<span class="hljs-attr">ansible_ssh_user</span>=ubuntu
<span class="hljs-attr">ansible_ssh_private_key_file</span>=~/ansible/aws_keypairs/Test.pem
</code></pre>
<p>Save and exit the file.</p>
<h2 id="heading-step-6-test-ssh-connection">Step 6: Test SSH Connection</h2>
<p>Before using Ansible, manually test SSH connectivity:</p>
<pre><code class="lang-bash">ssh -i ~/ansible/aws_keypairs/Test.pem ubuntu@ec2-13-127-126-77.ap-south-1.compute.amazonaws.com
</code></pre>
<p>If you face <strong>Permission denied (publickey)</strong> errors:</p>
<ul>
<li><p>Ensure the correct user (<code>ubuntu</code> for Ubuntu, <code>ec2-user</code> for Amazon Linux).</p>
</li>
<li><p>Verify that the SSH key is correctly associated with the EC2 instance.</p>
</li>
<li><p>Check that the security group allows inbound SSH (port 22) connections.</p>
</li>
</ul>
<h3 id="heading-add-host-to-known-hosts">Add Host to Known Hosts</h3>
<p>If prompted about authenticity, add the host manually:</p>
<pre><code class="lang-bash">ssh-keyscan -H ec2-13-127-126-77.ap-south-1.compute.amazonaws.com &gt;&gt; ~/.ssh/known_hosts
</code></pre>
<h2 id="heading-step-7-run-ansible-ping-command">Step 7: Run Ansible Ping Command</h2>
<p>Test Ansible connectivity to the EC2 instances:</p>
<pre><code class="lang-bash">ansible webservers -i ~/ansible/inventory.ini -m ping
</code></pre>
<p>If successful, it will return <code>pong</code> messages from the servers.</p>
<h2 id="heading-step-8-troubleshooting-ssh-and-ansible-issues">Step 8: Troubleshooting SSH and Ansible Issues</h2>
<ul>
<li><p><strong>Check Directory Structure</strong>:</p>
<pre><code class="lang-bash">  ls -l ~/ansible/aws_keypairs/
</code></pre>
</li>
<li><p><strong>Verify Key Permissions</strong>:</p>
<pre><code class="lang-bash">  ls -l ~/ansible/aws_keypairs/Test.pem
</code></pre>
</li>
<li><p><strong>Check Network Connectivity</strong>:</p>
<pre><code class="lang-bash">  ping ec2-13-127-126-77.ap-south-1.compute.amazonaws.com
</code></pre>
</li>
</ul>
<h2 id="heading-step-9-execute-ad-hoc-commands">Step 9: Execute Ad-Hoc Commands</h2>
<p>Now that connectivity is confirmed, you can execute ad-hoc Ansible commands.</p>
<p>Restart the Apache service on all web servers:</p>
<pre><code class="lang-bash">ansible webservers -i ~/ansible/inventory.ini -m service -a <span class="hljs-string">"name=apache2 state=restarted"</span>
</code></pre>
<h3 id="heading-install-apache2-on-ec2-instances">Install Apache2 on EC2 Instances</h3>
<ol>
<li><p><strong>Update packages</strong>:</p>
<pre><code class="lang-bash"> sudo apt update
</code></pre>
</li>
<li><p><strong>Install Apache</strong>:</p>
<pre><code class="lang-bash"> sudo apt install apache2 -y
</code></pre>
</li>
<li><p><strong>Start Apache service</strong>:</p>
<pre><code class="lang-bash"> sudo systemctl start apache2
</code></pre>
</li>
<li><p><strong>Enable Apache to start on boot</strong>:</p>
<pre><code class="lang-bash"> sudo systemctl <span class="hljs-built_in">enable</span> apache2
</code></pre>
</li>
<li><p><strong>Check Apache status</strong>:</p>
<pre><code class="lang-bash"> sudo systemctl status apache2
</code></pre>
</li>
</ol>
<h2 id="heading-step-10-create-an-ansible-playbook">Step 10: Create an Ansible Playbook</h2>
<p>For automation, create a playbook to restart Apache.</p>
<ol>
<li><p>Create a YAML file:</p>
<pre><code class="lang-bash"> nano restart_apache.yml
</code></pre>
</li>
<li><p>Add the following content:</p>
<pre><code class="lang-yaml"> <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Restart</span> <span class="hljs-string">Apache</span> <span class="hljs-string">on</span> <span class="hljs-string">webservers</span>
   <span class="hljs-attr">hosts:</span> <span class="hljs-string">webservers</span>
   <span class="hljs-attr">tasks:</span>
     <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Restart</span> <span class="hljs-string">Apache</span> <span class="hljs-string">service</span>
       <span class="hljs-attr">service:</span>
         <span class="hljs-attr">name:</span> <span class="hljs-string">apache2</span>
         <span class="hljs-attr">state:</span> <span class="hljs-string">restarted</span>
</code></pre>
</li>
<li><p>Save and exit.</p>
</li>
</ol>
<h2 id="heading-step-11-run-the-ansible-playbook">Step 11: Run the Ansible Playbook</h2>
<p>Execute the playbook using:</p>
<pre><code class="lang-bash">ansible-playbook -i ~/ansible/inventory.ini restart_apache.yml
</code></pre>
<h2 id="heading-summary">Summary</h2>
<ol>
<li><p><strong>Set up Ansible</strong> and install required packages.</p>
</li>
<li><p><strong>Define inventory files</strong> to specify target hosts.</p>
</li>
<li><p><strong>Verify SSH connectivity</strong> before running commands.</p>
</li>
<li><p><strong>Execute ad-hoc commands</strong> for quick operations.</p>
</li>
<li><p><strong>Use playbooks</strong> for automation and repeatability.</p>
</li>
<li><p><strong>Troubleshoot common issues</strong> to ensure smooth execution.</p>
</li>
</ol>
<p>By following this guide, you have successfully automated server management tasks using Ansible! 🚀</p>
]]></content:encoded></item><item><title><![CDATA[A Comprehensive Guide to Ansible Inventory Management]]></title><description><![CDATA[Introduction
Ansible inventory is the backbone of Ansible automation. It defines the systems that Ansible manages, organizes them into groups, and provides essential metadata for automation tasks. Understanding how to structure and manage an inventor...]]></description><link>https://ayush-ansible.hashnode.dev/a-comprehensive-guide-to-ansible-inventory-management</link><guid isPermaLink="true">https://ayush-ansible.hashnode.dev/a-comprehensive-guide-to-ansible-inventory-management</guid><category><![CDATA[ansible]]></category><category><![CDATA[ansible-playbook]]></category><dc:creator><![CDATA[Ayush Sharma]]></dc:creator><pubDate>Sun, 09 Mar 2025 18:30:19 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1741458568570/8765eb20-f5fa-49cf-b221-196f5c5e89a6.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction</h2>
<p>Ansible inventory is the backbone of Ansible automation. It defines the systems that Ansible manages, organizes them into groups, and provides essential metadata for automation tasks. Understanding how to structure and manage an inventory is crucial for efficient configuration management and deployment.</p>
<p>In this blog, we will explore:</p>
<ul>
<li><p>What Ansible inventory is</p>
</li>
<li><p>How to define an inventory file</p>
</li>
<li><p>Different formats of inventory</p>
</li>
<li><p>Managing dynamic inventory</p>
</li>
<li><p>Best practices for organizing inventory</p>
</li>
</ul>
<h2 id="heading-what-is-ansible-inventory">What is Ansible Inventory?</h2>
<p>Ansible inventory is a list of nodes (managed hosts) that Ansible uses to execute tasks. It provides a structured way to define hosts, groups, variables, and connection details.</p>
<p>By default, Ansible looks for its inventory in <code>/etc/ansible/hosts</code>, but you can specify a custom inventory file using the <code>-i</code> flag.</p>
<h2 id="heading-types-of-ansible-inventory">Types of Ansible Inventory</h2>
<p>Ansible supports two primary types of inventory:</p>
<h3 id="heading-1-static-inventory">1. <strong>Static Inventory</strong></h3>
<p>A static inventory is a simple text file (INI or YAML format) where hosts and groups are manually defined.</p>
<h4 id="heading-ini-format">INI Format:</h4>
<pre><code class="lang-ini"><span class="hljs-section">[web]</span>
webserver1 <span class="hljs-attr">ansible_host</span>=<span class="hljs-number">192.168</span>.<span class="hljs-number">1.10</span> ansible_user=ubuntu
webserver2 <span class="hljs-attr">ansible_host</span>=<span class="hljs-number">192.168</span>.<span class="hljs-number">1.11</span> ansible_user=ubuntu

<span class="hljs-section">[db]</span>
dbserver1 <span class="hljs-attr">ansible_host</span>=<span class="hljs-number">192.168</span>.<span class="hljs-number">1.20</span> ansible_user=ubuntu
</code></pre>
<h4 id="heading-yaml-format">YAML Format:</h4>
<pre><code class="lang-yaml"><span class="hljs-attr">all:</span>
  <span class="hljs-attr">hosts:</span>
    <span class="hljs-attr">webserver1:</span>
      <span class="hljs-attr">ansible_host:</span> <span class="hljs-number">192.168</span><span class="hljs-number">.1</span><span class="hljs-number">.10</span>
      <span class="hljs-attr">ansible_user:</span> <span class="hljs-string">ubuntu</span>
    <span class="hljs-attr">webserver2:</span>
      <span class="hljs-attr">ansible_host:</span> <span class="hljs-number">192.168</span><span class="hljs-number">.1</span><span class="hljs-number">.11</span>
      <span class="hljs-attr">ansible_user:</span> <span class="hljs-string">ubuntu</span>
  <span class="hljs-attr">children:</span>
    <span class="hljs-attr">web:</span>
      <span class="hljs-attr">hosts:</span>
        <span class="hljs-attr">webserver1:</span>
        <span class="hljs-attr">webserver2:</span>
    <span class="hljs-attr">db:</span>
      <span class="hljs-attr">hosts:</span>
        <span class="hljs-attr">dbserver1:</span>
          <span class="hljs-attr">ansible_host:</span> <span class="hljs-number">192.168</span><span class="hljs-number">.1</span><span class="hljs-number">.20</span>
          <span class="hljs-attr">ansible_user:</span> <span class="hljs-string">ubuntu</span>
</code></pre>
<h3 id="heading-2-dynamic-inventory">2. <strong>Dynamic Inventory</strong></h3>
<p>A dynamic inventory is used when managing large-scale infrastructure where hosts are added or removed dynamically, such as cloud environments.</p>
<p>Examples of dynamic inventory sources:</p>
<ul>
<li><p>AWS EC2 (<code>aws_ec2</code> plugin)</p>
</li>
<li><p>Azure (<code>azure_rm</code> plugin)</p>
</li>
<li><p>Google Cloud (<code>gcp_compute</code> plugin)</p>
</li>
<li><p>Kubernetes (<code>k8s</code> plugin)</p>
</li>
</ul>
<p>To use a dynamic inventory, you need an inventory script or a plugin:</p>
<pre><code class="lang-bash">ansible-inventory -i aws_ec2.yml --list
</code></pre>
<p>Example AWS EC2 dynamic inventory configuration:</p>
<pre><code class="lang-yaml"><span class="hljs-attr">plugin:</span> <span class="hljs-string">aws_ec2</span>
<span class="hljs-attr">regions:</span>
  <span class="hljs-bullet">-</span> <span class="hljs-string">us-east-1</span>
<span class="hljs-attr">keyed_groups:</span>
  <span class="hljs-bullet">-</span> <span class="hljs-attr">key:</span> <span class="hljs-string">tags.Name</span>
    <span class="hljs-attr">prefix:</span> <span class="hljs-string">tag_</span>
</code></pre>
<h2 id="heading-defining-host-variables">Defining Host Variables</h2>
<p>Host variables provide additional configuration for each host. They can be defined in the inventory file or stored separately in the <code>host_vars/</code> directory.</p>
<p>Example:</p>
<pre><code class="lang-yaml"><span class="hljs-attr">all:</span>
  <span class="hljs-attr">hosts:</span>
    <span class="hljs-attr">webserver1:</span>
      <span class="hljs-attr">ansible_host:</span> <span class="hljs-number">192.168</span><span class="hljs-number">.1</span><span class="hljs-number">.10</span>
      <span class="hljs-attr">ansible_user:</span> <span class="hljs-string">ubuntu</span>
      <span class="hljs-attr">ansible_python_interpreter:</span> <span class="hljs-string">/usr/bin/python3</span>
</code></pre>
<p>Alternatively, store host-specific variables in <code>host_vars/webserver1.yml</code>:</p>
<pre><code class="lang-yaml"><span class="hljs-attr">ansible_host:</span> <span class="hljs-number">192.168</span><span class="hljs-number">.1</span><span class="hljs-number">.10</span>
<span class="hljs-attr">ansible_user:</span> <span class="hljs-string">ubuntu</span>
<span class="hljs-attr">ansible_python_interpreter:</span> <span class="hljs-string">/usr/bin/python3</span>
</code></pre>
<h2 id="heading-group-variables">Group Variables</h2>
<p>Group variables apply common settings to all hosts within a group. These can be stored in <code>group_vars/</code>.</p>
<p>Example: <code>group_vars/web.yml</code></p>
<pre><code class="lang-yaml"><span class="hljs-attr">ansible_user:</span> <span class="hljs-string">ubuntu</span>
<span class="hljs-attr">ansible_ssh_private_key_file:</span> <span class="hljs-string">~/.ssh/id_rsa</span>
</code></pre>
<p>This will apply to all hosts in the <code>[web]</code> group.</p>
<h2 id="heading-using-multiple-inventory-files">Using Multiple Inventory Files</h2>
<p>Ansible allows you to define multiple inventory files using an inventory directory.</p>
<p>Example structure:</p>
<pre><code class="lang-plaintext">/inventory/
  hosts.yml
  group_vars/
    web.yml
    db.yml
  host_vars/
    webserver1.yml
    dbserver1.yml
</code></pre>
<p>To use this inventory directory:</p>
<pre><code class="lang-bash">ansible -i inventory/ all --list-hosts
</code></pre>
<h2 id="heading-best-practices-for-inventory-management">Best Practices for Inventory Management</h2>
<ol>
<li><p><strong>Use Dynamic Inventory for Cloud Environments</strong> – Automate inventory updates using cloud plugins.</p>
</li>
<li><p><strong>Organize Inventory Using Directories</strong> – Store variables separately in <code>host_vars/</code> and <code>group_vars/</code>.</p>
</li>
<li><p><strong>Use YAML Format</strong> – YAML is more readable and structured compared to INI.</p>
</li>
<li><p><strong>Leverage Ansible Vault</strong> – Encrypt sensitive data like passwords using <code>ansible-vault</code>.</p>
</li>
<li><p><strong>Use Patterns for Targeting Hosts</strong> – Instead of <code>all</code>, specify groups or patterns in playbooks.</p>
</li>
</ol>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Ansible inventory is a powerful way to define and manage hosts in an automated setup. Whether using static or dynamic inventory, understanding its structure and best practices will enhance your automation workflow.</p>
<p>By organizing inventory files efficiently, using host and group variables properly, and leveraging dynamic inventory when needed, you can build a scalable and maintainable infrastructure with Ansible.</p>
<p>Stay tuned for more Ansible automation insights!</p>
]]></content:encoded></item></channel></rss>