<?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-Tasks]]></title><description><![CDATA[Ansible-Tasks]]></description><link>https://ansible-realtime-tasks.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 29 Nov 2026 20:08:23 GMT</lastBuildDate><atom:link href="https://ansible-realtime-tasks.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[🧩 Real-Time Ansible Use Cases for Multi-Region EKS (Finance + Food Apps)]]></title><description><![CDATA[Below are 5 practical playbooks you can confidently discuss in interviews.Each is region-aware, idempotent, and maps to a real EKS + AWS multi-region…]]></description><link>https://ansible-realtime-tasks.hashnode.dev/real-time-ansible-use-cases-for-multi-region-eks-finance-food-apps</link><guid isPermaLink="true">https://ansible-realtime-tasks.hashnode.dev/real-time-ansible-use-cases-for-multi-region-eks-finance-food-apps</guid><dc:creator><![CDATA[Aditya Patil]]></dc:creator><pubDate>Wed, 15 Oct 2025 05:25:28 GMT</pubDate><content:encoded><![CDATA[<p>Below are <strong>5 practical playbooks</strong> you can confidently discuss in interviews.<br />Each is <strong>region-aware</strong>, <strong>idempotent</strong>, and maps to a real EKS + AWS multi-region setup.</p>
<hr />
<h3 id="heading-1-bootstrapping-ec2-bastion-hosts-in-multiple-regions">✅ <strong>1. Bootstrapping EC2 Bastion Hosts in Multiple Regions</strong></h3>
<p>📄 <strong>File:</strong> <code>playbooks/bastion_setup.yml</code></p>
<pre><code class="lang-python">---
- name: Install common tools on bastion hosts
  hosts: bastion
  become: yes
  vars:
    aws_region: <span class="hljs-string">"{{ lookup('env', 'AWS_REGION') | default('ca-central-1') }}"</span>
  tasks:
    - name: Install security patches
      yum:
        name: <span class="hljs-string">"*"</span>
        state: latest

    - name: Install AWS CLI
      yum:
        name: awscli
        state: present

    - name: Install Docker
      yum:
        name: docker
        state: present

    - name: Start <span class="hljs-keyword">and</span> enable Docker
      service:
        name: docker
        state: started
        enabled: yes
</code></pre>
<p><strong>💡 What it does:</strong><br />Ensures consistent configuration for bastion EC2 hosts (Docker, AWS CLI, security updates) in <strong>ca-central-1</strong> and <strong>ca-west-1</strong>, so engineers can securely SSH into private EKS or RDS networks.</p>
<hr />
<h3 id="heading-2-automating-rds-parameter-group-configuration">✅ <strong>2. Automating RDS Parameter Group Configuration</strong></h3>
<p>📄 <strong>File:</strong> <code>playbooks/rds_parameter_config.yml</code></p>
<pre><code class="lang-python">---
- name: Configure RDS parameter group across regions
  hosts: localhost
  connection: local
  tasks:
    - name: Set RDS parameter group <span class="hljs-keyword">for</span> MySQL
      community.aws.rds_param_group:
        state: present
        name: finance-rds-param
        engine: mysql
        description: Custom RDS settings <span class="hljs-keyword">for</span> finance app
        region: <span class="hljs-string">"{{ item }}"</span>
        parameters:
          max_connections: <span class="hljs-number">200</span>
          slow_query_log: <span class="hljs-number">1</span>
          character_set_server: utf8mb4
      loop:
        - ca-central<span class="hljs-number">-1</span>
        - ca-west<span class="hljs-number">-1</span>
</code></pre>
<p><strong>💡 What it does:</strong><br />Applies the same RDS parameter tuning (performance, charset, logging) for MySQL across multiple AWS regions.</p>
<hr />
<h3 id="heading-3-provisioning-s3-access-policies-for-media-buckets">✅ <strong>3. Provisioning S3 Access Policies for Media Buckets</strong></h3>
<p>📄 <strong>File:</strong> <code>playbooks/s3_iam_policy.yml</code></p>
<pre><code class="lang-python">---
- name: Create IAM policy <span class="hljs-keyword">for</span> food app S3 access
  hosts: localhost
  connection: local
  tasks:
    - name: Create IAM policy <span class="hljs-keyword">for</span> S3 access
      iam_policy:
        name: foodapp-s3-access
        policy_document: <span class="hljs-string">"{{ lookup('file', 'policies/s3_access.json') }}"</span>
        state: present
</code></pre>
<p>📄 <strong>File:</strong> <code>policies/s3_access.json</code></p>
<pre><code class="lang-python">{
  <span class="hljs-string">"Version"</span>: <span class="hljs-string">"2012-10-17"</span>,
  <span class="hljs-string">"Statement"</span>: [
    {
      <span class="hljs-string">"Effect"</span>: <span class="hljs-string">"Allow"</span>,
      <span class="hljs-string">"Action"</span>: [
        <span class="hljs-string">"s3:GetObject"</span>,
        <span class="hljs-string">"s3:PutObject"</span>
      ],
      <span class="hljs-string">"Resource"</span>: <span class="hljs-string">"arn:aws:s3:::foodapp-media/*"</span>
    }
  ]
}
</code></pre>
<p><strong>💡 What it does:</strong><br />Ensures EKS service accounts or Lambda functions can securely upload and retrieve images/media from S3 buckets used by the food app.</p>
<hr />
<h3 id="heading-4-automated-eks-worker-node-configuration-user-data">✅ <strong>4. Automated EKS Worker Node Configuration (User Data)</strong></h3>
<p>📄 <strong>File:</strong> <code>playbooks/eks_userdata.yml</code></p>
<pre><code class="lang-python">---
- name: Generate EKS user-data <span class="hljs-keyword">for</span> worker nodes
  hosts: localhost
  tasks:
    - name: Create user-data template
      template:
        src: templates/userdata.j2
        dest: /tmp/userdata.sh
</code></pre>
<p>📄 <strong>File:</strong> <code>templates/userdata.j2</code></p>
<pre><code class="lang-python"><span class="hljs-comment">#!/bin/bash</span>
set -xe

echo <span class="hljs-string">"Starting EKS worker node setup..."</span>
yum install -y awslogs

cat &lt;&lt;EOF &gt; /etc/awslogs/awslogs.conf
[general]
state_file = /var/lib/awslogs/agent-state
EOF

systemctl enable awslogsd
systemctl start awslogsd
</code></pre>
<p><strong>💡 What it does:</strong><br />Creates custom <strong>user-data scripts</strong> for new EKS worker nodes — preconfigures logging (FluentBit or CloudWatch), node labels, and taints.</p>
<hr />
<h3 id="heading-5-global-dns-amp-nginx-config-deployment-for-multi-region-ingress">✅ <strong>5. Global DNS &amp; NGINX Config Deployment for Multi-Region Ingress</strong></h3>
<p>📄 <strong>File:</strong> <code>playbooks/nginx_sync.yml</code></p>
<pre><code class="lang-python">---
- name: Sync TLS certs <span class="hljs-keyword">and</span> NGINX configs across regions
  hosts: ingress
  become: yes
  tasks:
    - name: Copy TLS certificates
      copy:
        src: certs/
        dest: /etc/ssl/certs/
        mode: <span class="hljs-string">'0644'</span>

    - name: Update NGINX configuration
      template:
        src: templates/nginx.conf.j2
        dest: /etc/nginx/nginx.conf

    - name: Restart NGINX
      service:
        name: nginx
        state: restarted
</code></pre>
<p>📄 <strong>File:</strong> <code>templates/nginx.conf.j2</code></p>
<pre><code class="lang-python">server {
    listen <span class="hljs-number">443</span> ssl;
    server_name {{ domain_name }};

    ssl_certificate /etc/ssl/certs/{{ domain_name }}.crt;
    ssl_certificate_key /etc/ssl/certs/{{ domain_name }}.key;

    location / {
        proxy_pass http://myapp-service;
    }
}
</code></pre>
<p><strong>💡 What it does:</strong><br />Keeps TLS certs and ingress configuration consistent across <a target="_blank" href="http://financeapp.au"><strong>financeapp.au</strong></a> and <a target="_blank" href="http://foodapp.ca"><strong>foodapp.ca</strong></a> clusters.</p>
<hr />
<h2 id="heading-interview-ready-summary">🎯 <strong>Interview-Ready Summary</strong></h2>
<blockquote>
<p>“We automated most of our regional configurations using Ansible — from bastion setup, RDS tuning, and IAM policies to EKS node initialization and NGINX ingress synchronization.<br />This reduced manual drift and ensured all EKS clusters across <em>ca-central-1</em>, <em>ca-west-1</em>, and <em>ap-southeast-2</em> regions followed identical, compliant configurations for our finance and food platforms.”</p>
</blockquote>
]]></content:encoded></item><item><title><![CDATA[Ansible Interview Questions]]></title><description><![CDATA[Here’s how I’d answer this in an interview setting, reflecting 4 years of experience: ✅ Recently with Ansible, I worked on: Provisioning EC2 Instances…]]></description><link>https://ansible-realtime-tasks.hashnode.dev/ansible-interview-questions</link><guid isPermaLink="true">https://ansible-realtime-tasks.hashnode.dev/ansible-interview-questions</guid><category><![CDATA[ansible]]></category><category><![CDATA[ansible-playbook]]></category><category><![CDATA[automation]]></category><category><![CDATA[Security]]></category><dc:creator><![CDATA[Aditya Patil]]></dc:creator><pubDate>Thu, 07 Aug 2025 15:42:57 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1754581305224/97a9c5e7-e41f-4835-b25c-aa6254e0c94d.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-q-what-tasks-did-you-recently-did-with-the-ansible">Q What tasks did you recently did with the ansible ?</h3>
<p>Here’s how I’d answer this in an interview setting, reflecting 4 years of experience:</p>
<hr />
<p>✅ <strong>Recently with Ansible, I worked on:</strong></p>
<ol>
<li><p><strong>Provisioning EC2 Instances (Ad-hoc + Playbooks)</strong></p>
<ul>
<li>Wrote playbooks to launch EC2 instances with required tags, security groups, and AMIs for QA testing.</li>
</ul>
</li>
<li><p><strong>Installing and Configuring NGINX &amp; Java App</strong></p>
<ul>
<li>Automated installation of NGINX and deployed a WAR file for a Spring Boot app with systemd setup.</li>
</ul>
</li>
<li><p><strong>Patch Management</strong></p>
<ul>
<li>Used Ansible to apply critical security updates to dev and staging servers.</li>
</ul>
</li>
<li><p><strong>Dynamic Inventory with AWS EC2 Plugin</strong></p>
<ul>
<li>Integrated dynamic inventory to manage EC2 instances by tags/regions.</li>
</ul>
</li>
<li><p><strong>Vault Usage</strong></p>
<ul>
<li>Used <code>ansible-vault</code> to secure credentials like DB passwords and API tokens in playbooks.</li>
</ul>
</li>
<li><p><strong>CI/CD Integration</strong></p>
<ul>
<li>Integrated Ansible playbooks in Jenkins pipeline to automate post-deployment steps like restarting services and config updates.</li>
</ul>
</li>
</ol>
<hr />
<h3 id="heading-q-what-sort-of-automations-you-do-with-ansible-recently-that-still-you-think-was-innovative">Q. What sort of automations you do with ansible recently that still you think was innovative ?</h3>
<p>✅ <strong>One automation I recently did with Ansible that I found innovative:</strong></p>
<blockquote>
<p>I automated <strong>blue/green deployment of a Java-based microservice</strong> using Ansible on EC2 instances.</p>
</blockquote>
<hr />
<h3 id="heading-what-i-did">🔧 <strong>What I Did:</strong></h3>
<ol>
<li><p><strong>Used Tags in EC2:</strong><br /> Tagged instances as <code>blue</code> and <code>green</code> to identify environments.</p>
</li>
<li><p><strong>Playbook Logic:</strong><br /> Wrote a playbook that:</p>
<ul>
<li><p>Detected the idle environment (<code>green</code> if <code>blue</code> is live).</p>
</li>
<li><p>Deployed the latest artifact there.</p>
</li>
<li><p>Ran health checks.</p>
</li>
<li><p>Swapped the ALB target group to point to the new environment.</p>
</li>
</ul>
</li>
<li><p><strong>Rollback Strategy:</strong><br /> If health check failed, it auto-rolled back to previous (live) environment.</p>
</li>
<li><p><strong>Slack Notification:</strong><br /> Sent deployment status to Slack with job ID, commit hash, and deployment environment.</p>
</li>
</ol>
<hr />
<h3 id="heading-why-its-innovative">💡 Why It’s Innovative:</h3>
<ul>
<li><p><strong>No downtime deployments</strong> using just Ansible and EC2 — without external deployment tools.</p>
</li>
<li><p>Made deployments safer, automated, and auditable with Slack logs.</p>
</li>
</ul>
<hr />
<h3 id="heading-q-so-you-wrote-playbooks-or-roles">Q. So you wrote playbooks or roles ?</h3>
<p>✅ <strong>Yes, I wrote both — playbooks and modular roles.</strong></p>
<hr />
<h3 id="heading-in-real-time-projects">👇 In Real-Time Projects:</h3>
<ul>
<li><p>I <strong>wrote roles</strong> for reusable components like:</p>
<ul>
<li><p><code>install_java</code></p>
</li>
<li><p><code>deploy_artifact</code></p>
</li>
<li><p><code>update_alb_target_group</code></p>
</li>
<li><p><code>health_check</code></p>
</li>
<li><p><code>rollback</code></p>
</li>
<li><p><code>notify_slack</code></p>
</li>
</ul>
</li>
<li><p>Then used those roles inside a <strong>main playbook</strong> for the full deployment workflow.</p>
</li>
</ul>
<hr />
<p>🎯 <strong>Why Roles?</strong></p>
<ul>
<li><p>Reusability across environments (Dev, QA, Prod).</p>
</li>
<li><p>Easier to test and maintain.</p>
</li>
<li><p>Cleaner structure when team members contribute.</p>
</li>
</ul>
<hr />
<p>🧠 Example Playbook Structure:</p>
<pre><code class="lang-plaintext">- hosts: target_hosts
  roles:
    - install_java
    - deploy_artifact
    - health_check
    - update_alb_target_group
    - notify_slack
</code></pre>
<h3 id="heading-q-write-a-playbooks-for-all-of-the-above">Q .Write a playbooks for all of the above ?</h3>
<p>Sure! Here's a <strong>complete Ansible playbook</strong> that uses modular roles like:</p>
<ul>
<li><p><code>install_java</code></p>
</li>
<li><p><code>deploy_artifact</code></p>
</li>
<li><p><code>health_check</code></p>
</li>
<li><p><code>update_alb_target_group</code></p>
</li>
<li><p><code>rollback</code></p>
</li>
<li><p><code>notify_slack</code></p>
</li>
</ul>
<hr />
<h3 id="heading-folder-structure-best-practice">📁 Folder Structure (Best Practice)</h3>
<pre><code class="lang-plaintext">site.yml
inventory/
  hosts.ini
roles/
  install_java/
  deploy_artifact/
  health_check/
  update_alb_target_group/
  rollback/
  notify_slack/
</code></pre>
<hr />
<h3 id="heading-siteyml-main-playbook">✅ <code>site.yml</code> — Main Playbook</h3>
<pre><code class="lang-plaintext">
- name: Complete CI/CD Deployment Workflow
  hosts: app_servers
  become: true

  roles:
    - install_java
    - deploy_artifact
    - health_check
    - update_alb_target_group
    - notify_slack
</code></pre>
<hr />
<h3 id="heading-role-installjavatasksmainyml">🧩 Role: <code>install_java/tasks/main.yml</code></h3>
<pre><code class="lang-plaintext">
- name: Install OpenJDK 17
  apt:
    name: openjdk-17-jdk
    state: present
    update_cache: yes
</code></pre>
<hr />
<h3 id="heading-role-deployartifacttasksmainyml">🧩 Role: <code>deploy_artifact/tasks/main.yml</code></h3>
<pre><code class="lang-plaintext">
- name: Copy JAR to server
  copy:
    src: /path/to/your-app.jar
    dest: /opt/your-app.jar
    mode: '0755'

- name: Start application service
  systemd:
    name: your-app
    state: restarted
    enabled: yes
</code></pre>
<hr />
<h3 id="heading-role-healthchecktasksmainyml">🧩 Role: <code>health_check/tasks/main.yml</code></h3>
<pre><code class="lang-plaintext">
- name: Wait for App to respond on port 8080
  uri:
    url: http://localhost:8080/health
    status_code: 200
    timeout: 30
    validate_certs: no
  register: result
  retries: 5
  delay: 10
  until: result.status == 200
</code></pre>
<hr />
<h3 id="heading-role-updatealbtargetgrouptasksmainyml">🧩 Role: <code>update_alb_target_group/tasks/main.yml</code></h3>
<pre><code class="lang-plaintext">
- name: Register Instance to Target Group (AWS)
  community.aws.elb_target:
    state: present
    target_group_arn: "{{ target_group_arn }}"
    targets:
      - Id: "{{ ansible_default_ipv4.address }}"
        Port: 8080
  when: ansible_default_ipv4.address is defined
</code></pre>
<hr />
<h3 id="heading-role-notifyslacktasksmainyml">🧩 Role: <code>notify_slack/tasks/main.yml</code></h3>
<pre><code class="lang-plaintext">
- name: Send Deployment Notification to Slack
  community.general.slack:
    token: "{{ slack_token }}"
    msg: "✅ Deployment of `your-app` to {{ inventory_hostname }} completed."
    channel: "#deployments"
    username: "ansible-bot"
</code></pre>
<hr />
<h3 id="heading-optional-rollbacktasksmainyml">🧩 Optional: <code>rollback/tasks/main.yml</code></h3>
<pre><code class="lang-plaintext">
- name: Rollback to previous JAR version
  shell: |
    cp /opt/your-app-backup.jar /opt/your-app.jar
    systemctl restart your-app
</code></pre>
<hr />
<h3 id="heading-q-how-would-your-inventory-looks-like">Q. How would your inventory looks like ?</h3>
<p>Here’s how your <strong>Ansible inventory file</strong> (<code>hosts.ini</code>) would typically look for this setup:</p>
<hr />
<h3 id="heading-inventoryhostsini">✅ <code>inventory/hosts.ini</code></h3>
<pre><code class="lang-plaintext">[app_servers]
dev-app01 ansible_host=10.0.1.10 ansible_user=ubuntu
dev-app02 ansible_host=10.0.1.11 ansible_user=ubuntu

[app_servers:vars]
ansible_ssh_private_key_file=~/.ssh/devops-key.pem
ansible_python_interpreter=/usr/bin/python3
target_group_arn=arn:aws:elasticloadbalancing:ap-south-1:123456789012:targetgroup/dev-apps-tg/abcd1234efgh5678
slack_token=xoxb-1234567890-0987654321-abcdefghijklmnopqrst
</code></pre>
<hr />
<h3 id="heading-explanation">🔍 Explanation</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Section</td><td>Description</td></tr>
</thead>
<tbody>
<tr>
<td><code>[app_servers]</code></td><td>Group of servers you're targeting (e.g. dev environment nodes).</td></tr>
<tr>
<td><code>ansible_host</code></td><td>Private or public IP/hostname of EC2 or VM.</td></tr>
<tr>
<td><code>ansible_user</code></td><td>OS-level user (like <code>ubuntu</code> for Ubuntu EC2).</td></tr>
<tr>
<td><code>ansible_ssh_private_key_file</code></td><td>SSH key to connect to target servers.</td></tr>
<tr>
<td><code>ansible_python_interpreter</code></td><td>Ensures Ansible uses correct Python version.</td></tr>
<tr>
<td><code>target_group_arn</code></td><td>Used in the ALB update step to register instances.</td></tr>
<tr>
<td><code>slack_token</code></td><td>Token used by the Slack notification role.</td></tr>
</tbody>
</table>
</div><hr />
<h3 id="heading-q-why-ini-format-and-not-yaml">Q. Why INI format and not yaml ?</h3>
<h6 id="heading-chatgpt-said">ChatGPT said:</h6>
<p>Great question 👇 (Answer in <strong>interview-style</strong> as per your request):</p>
<hr />
<p><strong>Why use</strong> <code>.ini</code> over <code>.yaml</code> for Ansible inventory?</p>
<blockquote>
<p>By default, Ansible uses <code>INI</code>-style static inventory (<code>hosts.ini</code>) because it’s simple, easy to maintain, and supported out of the box. It's good for small to medium environments where hosts don’t change frequently.</p>
</blockquote>
<hr />
<p><strong>When would I use YAML?</strong></p>
<blockquote>
<p>I’d prefer YAML inventory for:</p>
</blockquote>
<ul>
<li><p>Better readability in complex group hierarchies</p>
</li>
<li><p>Dynamic inventory scripts (combined with plugins like AWS EC2)</p>
</li>
<li><p>Consistency when everything else (like playbooks and vars) is in YAML</p>
</li>
</ul>
<hr />
<p><strong>Follow-up (if asked):</strong><br />Yes, starting from Ansible 2.4+, YAML-style inventory is supported using <code>.yaml</code> or <code>.yml</code> files, but it needs to be explicitly structured and not all features are as straightforward as in <code>ini</code>.</p>
<h3 id="heading-q-so-dont-you-manage-configuration-for-dev-qa-stage-and-prod-servers-using-ansible-because-i-havent-seen-them-in-your-ini-file-or-do-you-prefer-a-different-strategy">Q. So, don't you manage configuration for dev, QA, stage, and prod servers using Ansible? Because I haven't seen them in your INI file. Or do you prefer a different strategy?</h3>
<p>✅ <strong>Interview-style answer:</strong></p>
<p>Yes, I do manage configurations across <strong>Dev, QA, Staging, and Prod</strong> environments using Ansible.</p>
<p>🔹 In our case, we follow a <strong>directory-based inventory strategy</strong>, not just a flat <code>.ini</code> file.</p>
<hr />
<h3 id="heading-example-folder-structure">✅ Example Folder Structure:</h3>
<pre><code class="lang-plaintext">inventories/
├── dev/
│   ├── hosts.ini
│   └── group_vars/
├── qa/
│   ├── hosts.ini
│   └── group_vars/
├── staging/
│   ├── hosts.ini
│   └── group_vars/
├── prod/
│   ├── hosts.ini
│   └── group_vars/
</code></pre>
<blockquote>
<p>Each environment has its own <code>hosts.ini</code> and <code>group_vars</code> for environment-specific configurations.</p>
</blockquote>
<hr />
<h3 id="heading-why-not-put-all-environments-in-one-ini">🔄 Why not put all environments in one <code>.ini</code>?</h3>
<p>To avoid risk of <strong>accidental cross-environment deployment</strong>, we separate inventories.<br />Also, our CI/CD pipeline triggers the right inventory based on environment passed as input.</p>
<hr />
<h3 id="heading-bonus-tip-if-asked">🧠 Bonus Tip (if asked):</h3>
<p>We also use <code>ansible.cfg</code> to dynamically point to the right inventory during runtime, like:</p>
<pre><code class="lang-plaintext">[defaults]
inventory = inventories/dev/hosts.ini
</code></pre>
<h3 id="heading-q-so-how-would-it-look-like-the-above-structure-and-content-in-it-and-how-you-run-any-specific-playbook-on-any-env-servers">Q. So how would it look like the above structure and content in it and how you run any specific playbook on any env servers ?</h3>
<p>✅ <strong>Interview-style answer</strong>:<br />Here’s how our <strong>Ansible inventory directory structure</strong> and content looks like, and how we execute playbooks per environment.</p>
<hr />
<h3 id="heading-directory-structure">📁 <strong>Directory Structure:</strong></h3>
<pre><code class="lang-plaintext">ansible/
├── playbooks/
│   ├── deploy_app.yml
│   ├── install_docker.yml
│   └── harden_os.yml
├── inventories/
│   ├── dev/
│   │   ├── hosts.ini
│   │   └── group_vars/
│   │       └── all.yml
│   ├── qa/
│   │   ├── hosts.ini
│   │   └── group_vars/
│   │       └── all.yml
│   ├── staging/
│   │   ├── hosts.ini
│   │   └── group_vars/
│   │       └── all.yml
│   └── prod/
│       ├── hosts.ini
│       └── group_vars/
│           └── all.yml
└── ansible.cfg
</code></pre>
<hr />
<h3 id="heading-sample-hostsini-for-qa">📄 <strong>Sample</strong> <code>hosts.ini</code> (for QA):</h3>
<pre><code class="lang-plaintext">[web]
web-qa-01 ansible_host=10.0.1.11

[db]
db-qa-01 ansible_host=10.0.1.22
</code></pre>
<hr />
<h3 id="heading-sample-groupvarsallyml">📄 <strong>Sample</strong> <code>group_vars/all.yml</code>:</h3>
<pre><code class="lang-plaintext">app_port: 8080
env: qa
docker_version: "20.10"
</code></pre>
<hr />
<h3 id="heading-ansiblecfg-partial">🔧 <strong>ansible.cfg</strong> (partial):</h3>
<pre><code class="lang-plaintext">[defaults]
inventory = inventories/dev/hosts.ini
remote_user = ec2-user
private_key_file = ~/.ssh/id_rsa
</code></pre>
<hr />
<h3 id="heading-run-a-playbook-for-qa-environment">▶️ <strong>Run a playbook for QA environment:</strong></h3>
<pre><code class="lang-plaintext">ansible-playbook -i inventories/qa/hosts.ini playbooks/deploy_app.yml
</code></pre>
<hr />
<h3 id="heading-how-this-helps">💡 How this helps:</h3>
<ul>
<li><p>Clear separation of envs avoids cross-deployments</p>
</li>
<li><p>Each env has its own config via <code>group_vars</code></p>
</li>
<li><p>Easy integration into CI/CD (pass <code>-i</code> dynamically)</p>
</li>
</ul>
<h3 id="heading-q-how-you-use-roles-here-and-why">Q. How you use roles here and why ?</h3>
<p>✅ <strong>Interview-style answer:</strong></p>
<p>We use <strong>Ansible roles</strong> to modularize and reuse code across environments. Roles help keep the playbooks clean, maintainable, and scalable — especially when the same logic is used in Dev, QA, Stage, and Prod.</p>
<hr />
<h3 id="heading-our-updated-directory-structure-with-roles">📁 <strong>Our Updated Directory Structure (with roles):</strong></h3>
<pre><code class="lang-plaintext">ansible/
├── playbooks/
│   ├── deploy_app.yml
│   └── install_docker.yml
├── roles/
│   ├── docker/
│   │   ├── tasks/
│   │   │   └── main.yml
│   │   ├── handlers/
│   │   │   └── main.yml
│   │   └── defaults/
│   │       └── main.yml
│   ├── app_deploy/
│   │   ├── tasks/
│   │   │   └── main.yml
│   │   └── templates/
│   │       └── nginx.conf.j2
├── inventories/
│   ├── dev/
│   ├── qa/
│   └── prod/
└── ansible.cfg
</code></pre>
<hr />
<h3 id="heading-sample-deployappyml">📄 <strong>Sample</strong> <code>deploy_app.yml</code>:</h3>
<pre><code class="lang-plaintext">- name: Deploy Application
  hosts: web
  become: yes
  roles:
    - docker
    - app_deploy
</code></pre>
<hr />
<h3 id="heading-why-we-use-roles">🔁 <strong>Why we use roles:</strong></h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Purpose</td><td>Role Benefit</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Code reusability</strong></td><td>Avoid repeating logic like Docker install</td></tr>
<tr>
<td><strong>Environment override</strong></td><td>Override role defaults with <code>group_vars</code></td></tr>
<tr>
<td><strong>Easy testing</strong></td><td>Test roles individually with Molecule</td></tr>
<tr>
<td><strong>CI/CD friendly</strong></td><td>Integrates cleanly into Jenkins/GitHub Actions</td></tr>
</tbody>
</table>
</div><hr />
<h3 id="heading-how-we-run-it-for-qa">▶️ <strong>How we run it for QA:</strong></h3>
<pre><code class="lang-plaintext">ansible-playbook -i inventories/qa/hosts.ini playbooks/deploy_app.yml
</code></pre>
<hr />
]]></content:encoded></item></channel></rss>