<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Anuj Dube</title>
    <description>The latest articles on DEV Community by Anuj Dube (@anujsd).</description>
    <link>https://dev.to/anujsd</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1179976%2F2bd79e71-959e-4534-9b08-076297bf0b46.jpg</url>
      <title>DEV Community: Anuj Dube</title>
      <link>https://dev.to/anujsd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anujsd"/>
    <language>en</language>
    <item>
      <title>Learning AWS Lambda, API Gateway, and S3 by Building Your Own QR Code Generation API</title>
      <dc:creator>Anuj Dube</dc:creator>
      <pubDate>Tue, 31 Oct 2023 17:39:55 +0000</pubDate>
      <link>https://dev.to/anujsd/learning-aws-lambda-api-gateway-and-s3-by-building-your-own-qr-code-generation-api-3b1a</link>
      <guid>https://dev.to/anujsd/learning-aws-lambda-api-gateway-and-s3-by-building-your-own-qr-code-generation-api-3b1a</guid>
      <description>&lt;p&gt;Step 1: Creating S3 Bucket&lt;br&gt;
Step 2: Creating a Lambda Function&lt;br&gt;
Step 3: Attaching API gateway to lambda function&lt;br&gt;
Step 4: Integrating the Created API into a Website&lt;/p&gt;



&lt;p&gt;In this tutorial, we will develop a custom QR code generator API and then integrate it into a basic website.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bPE8lN2X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698772065283/45772850-c1c9-49e8-8931-36bb49b459b6.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bPE8lN2X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698772065283/45772850-c1c9-49e8-8931-36bb49b459b6.gif" alt="" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We'll build the API using Node.js and deploy it on AWS Lambda. The API endpoints will be established through API Gateway, and the generated QR codes will be stored in Amazon S3. The only requirement is having an AWS account. If you're within the free tier, following this tutorial should incur no costs, as we'll stay within the free tier limits. However, it's important to delete all AWS resources once you're done practicing to prevent any unexpected charges.&lt;/p&gt;



&lt;p&gt;AWS Lambda is a serverless compute service provided by AWS (Amazon Web Services) that allows developers to run code in response to events without having to manage servers or runtime environments. Lambda Supports lots of Runtime environments like Node.js, Python, Go, Ruby, Java, .NET etc. It supports custom runtime environments also. You only pay for the compute time you consume - there is no charge when your code is not running.&lt;/p&gt;

&lt;p&gt;Under Free Tier AWS Gives 1,000,000 free requests per month which is 3.2 million seconds of free compute time per month for 12 months.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 1: Creating S3 Bucket
&lt;/h2&gt;

&lt;p&gt;We will be creating an S3 bucket. and making content inside it publically accessible so we can access our generated QR codes.&lt;/p&gt;

&lt;p&gt;Login into AWS and search s3 in the search bar then click on the first result as shown in the image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--p5EaAwEr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698763680300/e8e2590c-f706-4538-b4fa-bd5aa4515933.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--p5EaAwEr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698763680300/e8e2590c-f706-4538-b4fa-bd5aa4515933.png" alt="" width="800" height="197"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on Create Bucket&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--k367ZzrW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698763737106/064dbfb5-d0ab-4efe-9819-41981628d8f3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k367ZzrW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698763737106/064dbfb5-d0ab-4efe-9819-41981628d8f3.png" alt="" width="800" height="353"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now Enter the Bucket name it must be unique. Keep all options as default. and then click on the "create bucket" button at the bottom.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IPjAXzAd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698763810857/1b7ee744-070a-4aa7-98e2-07d6c6c5479c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IPjAXzAd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698763810857/1b7ee744-070a-4aa7-98e2-07d6c6c5479c.png" alt="" width="800" height="460"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now After Your bucket is created click on the "Permissions" tab. Click on the Edit button on "Block public access".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rC4FavOv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698763916446/f5830657-5850-4454-adb3-6f17abb1fc5b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rC4FavOv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698763916446/f5830657-5850-4454-adb3-6f17abb1fc5b.png" alt="" width="800" height="353"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;uncheck all options and click on "Save Changes". You will be asked to confirm. type confirm.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9zOiZIvQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698764009642/5e5c23d4-fb46-4561-9279-2608b82836ef.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9zOiZIvQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698764009642/5e5c23d4-fb46-4561-9279-2608b82836ef.png" alt="" width="800" height="356"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now Move down and click on Edit button in "Bucket policy".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xrRIisF---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698764101055/df84c6d8-ae53-459a-9751-8fddf964f649.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xrRIisF---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698764101055/df84c6d8-ae53-459a-9751-8fddf964f649.png" alt="" width="800" height="376"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Update Bucket Policy like below. Keep in mind to add Your Bucket ARN. You can take it just from above.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": "*",
      "Action": "s3:GetObject",
      "Resource": "&amp;lt;Your ARN&amp;gt;/*"
    }
  ]
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--c5WWiw_V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698764209695/67840c69-423a-47ec-9af0-39220570e45c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c5WWiw_V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698764209695/67840c69-423a-47ec-9af0-39220570e45c.png" alt="" width="800" height="299"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now Click on "Save Changes"&lt;br&gt;&lt;br&gt;
Now Our S3 Bucket is ready. All files put inside this bucket now will be publically accessible.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 2: Creating a Lambda Function
&lt;/h2&gt;

&lt;p&gt;Search lambda in the search bar then click on the first result as shown in the image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YsKeiMP7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698761537367/3ecdfe6f-6cbb-443e-a695-51cc48aea5ae.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YsKeiMP7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698761537367/3ecdfe6f-6cbb-443e-a695-51cc48aea5ae.png" alt="" width="800" height="306"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now Click on Create Function&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MXrjCbGn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698761675684/7b494b79-007a-4d48-87e4-b487a7d0341f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MXrjCbGn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698761675684/7b494b79-007a-4d48-87e4-b487a7d0341f.png" alt="" width="800" height="347"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then Select the First Option "Author from scratch" and then Enter the Function name. Also, Runtime should be selected as Node.js. Keep Everything default. and Just click on the "Create Function" button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--25SAaCwC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698761717180/5085b7f3-a18b-4ddb-a67f-1f4c2c555199.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--25SAaCwC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698761717180/5085b7f3-a18b-4ddb-a67f-1f4c2c555199.png" alt="" width="800" height="352"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You Will come across a window like below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kaYoFZ8m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698761898791/757eed7c-f190-4901-a451-de7295e6c17c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kaYoFZ8m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698761898791/757eed7c-f190-4901-a451-de7295e6c17c.png" alt="" width="800" height="351"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can explore this window to learn more.&lt;/p&gt;

&lt;p&gt;Go to link &lt;a href="https://github.com/Anujsd/lambda-qr-code"&gt;https://github.com/Anujsd/lambda-qr-code&lt;/a&gt;&lt;br&gt;&lt;br&gt;
And Clone this Repo in your PC.&lt;br&gt;&lt;br&gt;
Open that in your Favourite IDE I will be using vs code.&lt;br&gt;&lt;br&gt;
Open index.mjs go to line number 9 and update the bucket name with your bucket name we just created.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GosDYT30--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698764856868/e2b06c90-4c92-4da3-8242-83fc330a5750.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GosDYT30--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698764856868/e2b06c90-4c92-4da3-8242-83fc330a5750.png" alt="" width="800" height="301"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now Run the Command "npm install" to install node dependencies&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XNs0OHNU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698764992349/e3a89018-6858-438d-b482-e1ccef23aeb2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XNs0OHNU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698764992349/e3a89018-6858-438d-b482-e1ccef23aeb2.png" alt="" width="800" height="276"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now We are going to create a ZIP of this whole "lambda-qr-code" folder.&lt;br&gt;&lt;br&gt;
For Windows open it in File Explorer select all, right-click and click on compress to zip.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Rl_ozFCf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698765100952/0a6f3425-a3e4-4f2c-bcc3-181c6c29452e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Rl_ozFCf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698765100952/0a6f3425-a3e4-4f2c-bcc3-181c6c29452e.png" alt="" width="800" height="466"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;name zip file "lambda-qr-code".&lt;br&gt;&lt;br&gt;
For the Sake of this tutorial, we will be uploading code using zip.&lt;br&gt;&lt;br&gt;
This ZIP file contains all code and node modules required for the lambda function. As we are using custom packages we need to upload node modules in zip format. otherwise, we could have directly written code on the web interface.&lt;/p&gt;

&lt;p&gt;Now to upload this ZIP go to lambda in AWS in the "code" section and click on "upload from".&lt;/p&gt;

&lt;p&gt;select option ".zip file" and upload that zip file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B1LrHWj6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698762353241/9ef66b8c-0155-46ab-94f6-de36c4228bc6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B1LrHWj6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698762353241/9ef66b8c-0155-46ab-94f6-de36c4228bc6.png" alt="" width="800" height="367"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After Uploading the File You will see an interface like below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jbkLoMWR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698762433474/a15b7b31-19ec-4088-83a6-f3b67fada244.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jbkLoMWR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698762433474/a15b7b31-19ec-4088-83a6-f3b67fada244.png" alt="" width="800" height="353"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code won't be visible in the web interface because the file is too large; however, it's quite straightforward, and you can view it below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import AWS from 'aws-sdk';
import QRCode from 'qrcode';

// Configure AWS SDK
AWS.config.update({ region: 'us-east-1' });
const s3 = new AWS.S3();

// Define S3 bucket and key
const bucketName = '&amp;lt;s3 bucket name&amp;gt;';
const key = `qr-codes/${Date.now()}.png`;

export const handler = async (event) =&amp;gt; {
  console.log(JSON.stringify(event));

  // Process POST request
  try {
    let requestBody;
    if (typeof event.body === 'string') {
      requestBody = JSON.parse(event.body);
    } else {
      requestBody = event.body; // Assuming it's already parsed
    }

    const { url } = requestBody;

    if (!url) {
      return {
        statusCode: 400,
        body: JSON.stringify({ message: 'URL is required', body: requestBody }),
      };
    }

    // Generate QR code
    const qrCodeBuffer = await QRCode.toBuffer(url);

    // Upload QR code to S3
    await s3
      .putObject({
        Bucket: bucketName,
        Key: key,
        Body: qrCodeBuffer,
        ContentType: 'image/png',
      })
      .promise();

    // Construct the public URL of the QR code
    const publicUrl = `https://${bucketName}.s3.amazonaws.com/${key}`;

    // Return response with CORS headers
    return {
      statusCode: 200,
      body: JSON.stringify({ qrCodeUrl: publicUrl }),
    };
  } catch (error) {
    console.error('Error generating QR code:', error);
    return {
      statusCode: 500,
      body: JSON.stringify({ message: 'Internal Server Error' }),
    };
  }
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now We will be giving our lambda function permission to add generated QR in s3.&lt;/p&gt;

&lt;p&gt;for that go to the "Configuration" Tab then "Permissions" and click on the "Role name" link. It will be opened in a new tab.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1FSt132f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698768689900/269788a5-d16b-47c5-9bf4-b94c18768ebf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1FSt132f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698768689900/269788a5-d16b-47c5-9bf4-b94c18768ebf.png" alt="" width="800" height="191"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now in a newly opened window. click on "Add permissions" and then "Attach policies".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mQYJaLlc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698768783421/9e0f926e-7426-49c9-8861-8c952b844ae9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mQYJaLlc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698768783421/9e0f926e-7426-49c9-8861-8c952b844ae9.png" alt="" width="800" height="292"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;a new window will be opened. Search "s3" in that and select "&lt;a href="https://us-east-1.console.aws.amazon.com/iamv2/home#/policies/details/arn%3Aaws%3Aiam%3A%3Aaws%3Apolicy%2FAmazonS3FullAccess"&gt;AmazonS3FullAccess&lt;/a&gt;" policy. and click on the "add permissions" button at the bottom.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1b1ywu_M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698768860667/d497edc2-7464-4191-be03-20f23466b6ac.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1b1ywu_M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698768860667/d497edc2-7464-4191-be03-20f23466b6ac.png" alt="" width="800" height="244"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now go to lambda Function.&lt;/p&gt;

&lt;p&gt;We will be testing If it's working or not.&lt;br&gt;&lt;br&gt;
Click on the "Test" Section. Select options like below and add below JSON which is our test payload.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "body": {
    "url": "https://anujdube.com/"
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LI5FL3vB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698762937086/bb144d31-8d48-476b-8534-94f01155994d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LI5FL3vB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698762937086/bb144d31-8d48-476b-8534-94f01155994d.png" alt="" width="800" height="387"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on "Save" and then click on the "Test" button.&lt;br&gt;&lt;br&gt;
After Successful execution, you will see the output like below. To see detailed logs you can click on the "logs" link.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_OuVg-C_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698769022990/44e65374-3713-4f65-bb64-2d213a0326cb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_OuVg-C_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698769022990/44e65374-3713-4f65-bb64-2d213a0326cb.png" alt="" width="800" height="169"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now Our lambda function is ready we are going to add an API gateway to this lambda function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Attaching API gateway to lambda function
&lt;/h2&gt;

&lt;p&gt;Search API Gateway in the search bar then click on the first result as shown in the image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--L217QXcE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698769249545/a0d7ac82-fb1c-4d9a-b46b-4e11375b8b83.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--L217QXcE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698769249545/a0d7ac82-fb1c-4d9a-b46b-4e11375b8b83.png" alt="" width="800" height="234"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;after opening it. Click on the "build" option in HTTP API.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3X92zmXb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698769299119/af25091f-5670-4e66-9bc6-1861742f6c3f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3X92zmXb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698769299119/af25091f-5670-4e66-9bc6-1861742f6c3f.png" alt="" width="800" height="362"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In "Intergrations" select our lambda function as shown below. and add name to API.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HtGBq7lB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698769372592/58afa018-d823-4491-a6cd-68587c8710ba.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HtGBq7lB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698769372592/58afa018-d823-4491-a6cd-68587c8710ba.png" alt="" width="800" height="517"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;click on next. in routes. add a POST route like below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oJgxbHeR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698769431464/b2a53d9b-b43e-4572-a956-5bddb08c6113.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oJgxbHeR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698769431464/b2a53d9b-b43e-4572-a956-5bddb08c6113.png" alt="" width="800" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now Click on next. keep everything default. and click on the "Create" button.&lt;/p&gt;

&lt;p&gt;You will see your API created like below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SraxbSJ1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698769612496/eccdcf50-8554-428c-ad27-bf87796913ac.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SraxbSJ1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698769612496/eccdcf50-8554-428c-ad27-bf87796913ac.png" alt="" width="800" height="273"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now open your API. On the left-hand side click on the section after "VPC links".&lt;/p&gt;

&lt;p&gt;and in there you can see the invoke URL. this is your API URL.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TSp887pG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698769662116/73aff34b-1662-4126-856f-b5c8ae47cbea.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TSp887pG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698769662116/73aff34b-1662-4126-856f-b5c8ae47cbea.png" alt="" width="800" height="358"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can use this URL and do a POST request using POSTMAN. It will be successful.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KbQRNyk4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698769788090/dbff412b-8205-4770-b4c3-70e2f3b9aeee.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KbQRNyk4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698769788090/dbff412b-8205-4770-b4c3-70e2f3b9aeee.png" alt="" width="800" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But if you try to use this URL in any application you might get CORS errors like below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--terX4-Ev--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698769949072/4ee473e6-3de6-4ce0-806d-3427fd2e7cc3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--terX4-Ev--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698769949072/4ee473e6-3de6-4ce0-806d-3427fd2e7cc3.png" alt="" width="800" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To resolve these errors click on CORS on the left-hand side. and click on "configure"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ggocm784--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698769981198/8bdf30c0-1140-4cdd-9f93-894e555ccaf5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ggocm784--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698769981198/8bdf30c0-1140-4cdd-9f93-894e555ccaf5.png" alt="" width="800" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add values as below and click on "Save"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Access-Control-Allow-Origin&lt;/strong&gt; : &lt;em&gt;*&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Access-Control-Allow-Headers&lt;/strong&gt; : &lt;em&gt;Content-Type, x-requested-with&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Access-Control-Allow-Methods&lt;/strong&gt; : &lt;em&gt;OPTIONS, POST&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;At the moment, we are permitting access from all origins. However, for enhanced security in a production environment, it's advisable to restrict access to just your domain name.&lt;/p&gt;

&lt;p&gt;Click on the "save" button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lgFJCJxB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698770293550/641a1dcc-9022-40d9-81ca-be414d80014e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lgFJCJxB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698770293550/641a1dcc-9022-40d9-81ca-be414d80014e.png" alt="" width="800" height="461"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now You will not face any CORS Errors While using your API in any app.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Integrating the Created API into a Website
&lt;/h2&gt;

&lt;p&gt;Now we will be trying to use the created API in a simple app.&lt;/p&gt;

&lt;p&gt;Click on the below link and clone the repo&lt;br&gt;&lt;br&gt;
&lt;a href="https://github.com/Anujsd/qr-code-front-end"&gt;https://github.com/Anujsd/qr-code-front-end&lt;/a&gt;&lt;br&gt;&lt;br&gt;
it just has 3 files.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--msi4pO5A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698770519594/924b8bd2-b94f-4f34-b847-44709c9da54e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--msi4pO5A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698770519594/924b8bd2-b94f-4f34-b847-44709c9da54e.png" alt="" width="800" height="269"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;open script.js&lt;/p&gt;

&lt;p&gt;and on line number 1. update your API endpoint&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fijUoGKC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698770577898/c1ee76ac-c1f4-439f-b398-03b05db39ca8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fijUoGKC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698770577898/c1ee76ac-c1f4-439f-b398-03b05db39ca8.png" alt="" width="800" height="106"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;now if you open index.html&lt;/p&gt;

&lt;p&gt;you will see UI like below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MGKqya4q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698770628869/1bbb0ec2-3b00-42c3-a8e6-f95fb0d9ef71.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MGKqya4q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698770628869/1bbb0ec2-3b00-42c3-a8e6-f95fb0d9ef71.png" alt="" width="800" height="463"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;enter any link to shorten and you will see a QR code generated for that link&lt;br&gt;&lt;br&gt;
you can test if the QR code is correct or not.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0KJlI_Hu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698770703619/71d4fe76-6b9a-4c74-9a57-d06b6d17ac07.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0KJlI_Hu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1698770703619/71d4fe76-6b9a-4c74-9a57-d06b6d17ac07.png" alt="" width="800" height="536"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now You can host this website online if needed.&lt;/p&gt;




&lt;p&gt;In this way, We developed our custom QR Code generation API utilizing AWS Lambda. If you encounter any issues or spot any errors in the article, please don't hesitate to contact me.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>lambda</category>
      <category>s3</category>
    </item>
    <item>
      <title>Ansible Overview</title>
      <dc:creator>Anuj Dube</dc:creator>
      <pubDate>Sun, 22 Oct 2023 17:21:10 +0000</pubDate>
      <link>https://dev.to/anujsd/ansible-overview-27ma</link>
      <guid>https://dev.to/anujsd/ansible-overview-27ma</guid>
      <description>&lt;p&gt;In the Field of IT, Automation and Configuration management have become indispensable. As systems grow in complexity, the need to manage and provision them in a reproducible and scalable manner is crucial. Here Ansible can help it's a simple, agentless IT automation tool that has become a go-to solution for many administrators and developers.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is Ansible?
&lt;/h2&gt;

&lt;p&gt;Ansible is an open-source automation tool that helps in software provisioning, configuration management, and application deployment. Developed in Python, it uses a YAML to describe system configurations, making it readable and easy to implement. With its agentless architecture, all you need is SSH access and Python installed on the target nodes. In the case of Windows, It can use Winrm to connect to the Windows host and execute automations. Not only servers you can manage network devices like routers, switches etc with Ansible.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ansible Architecture
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XAb_sEYN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://pbs.twimg.com/media/F88nPJBXAAAXeqm%3Fformat%3Djpg%26name%3D900x900" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XAb_sEYN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://pbs.twimg.com/media/F88nPJBXAAAXeqm%3Fformat%3Djpg%26name%3D900x900" alt="Ansible Architecture Central node pushing configuration to managed nodes." width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ansible's design is straightforward and Easy to understand:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Control Node:&lt;/strong&gt; This is the machine where Ansible is installed and from which all tasks and playbooks are run.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Managed Nodes:&lt;/strong&gt; These are the systems that are managed by the control node. No agents are installed on these systems.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Inventory:&lt;/strong&gt; A file that lists all managed nodes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Playbooks:&lt;/strong&gt; Written in YAML, these define the desired states of systems, describing tasks to be executed on managed nodes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Modules:&lt;/strong&gt; These are units of code Ansible executes. With over a thousand modules, Ansible can manage almost any IT component.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Ansible communicates with its managed nodes using SSH. but, for Windows nodes, it utilizes WinRM.&lt;/p&gt;

&lt;p&gt;One of the standout features of Ansible is its agentless nature. This means there's no need to install any additional software on the managed nodes. Instead, Ansible is only installed on the control node. The configurations/Automation Steps are stored in files called playbooks written in YAML, while the list of nodes it manages is documented in an inventory file, typically in INI format. To ensure security, the credentials used to connect to the managed nodes are stored securely in the Ansible Vault, where they are encrypted.&lt;/p&gt;

&lt;p&gt;When it's time to execute tasks on the managed nodes, Ansible converts these tasks from the playbooks into small Python scripts called modules. These modules are then dispatched to the managed nodes for execution. This process showcases Ansible's "push" mechanism, where configurations are actively sent to the managed nodes.&lt;/p&gt;

&lt;p&gt;For IT devices like networking equipment that have limited computing power and constraints, Ansible runs modules on the control node instead of directly on the devices. These modules then produce specific commands tailored for each device or initiate API requests to automate various tasks.&lt;/p&gt;

&lt;p&gt;Automation Execution is idempotent means if you run an Ansible playbook once or multiple times, the end state of the target system will remain the same. This means repeated playbook runs won't unintentionally change systems, making Ansible's automation dependable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use Ansible?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Open Source&lt;/strong&gt; : Being open-source, Ansible benefits from collective expertise, allowing for rapid improvements.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Simplicity&lt;/strong&gt; : Ansible's declarative language (YAML) means that even those unfamiliar with programming can understand and write playbooks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Agentless&lt;/strong&gt; : There's no need to install any software on the managed nodes. This reduces overhead and potential attack vectors.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Versatility&lt;/strong&gt; : From managing configurations and deploying applications to orchestrating complex workflows, Ansible can handle it all. It can even manage Cloud Infrastructure like AWS, Azure, Google Cloud, VMware, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Community Support&lt;/strong&gt; : A large, active community ensures that new modules are continually added, and existing ones are maintained.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Extensibility&lt;/strong&gt; : With its modular architecture, it's easy to extend Ansible to support custom or niche requirements. Lots of pre-created modules are present for various use cases. You can write your custom modules using languages like Python, bash, PowerShell, etc., returning JSON.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Community Ansible Vs RedHat Ansible Automation Platform
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RjG61Eg8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1697993848826/81144417-fb25-45d7-8bbb-32afc1f278e7.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RjG61Eg8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1697993848826/81144417-fb25-45d7-8bbb-32afc1f278e7.webp" alt="" width="800" height="330"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Community Ansible is open-source, while the Red Hat Ansible Automation Platform is a commercial offering by Red Hat, which builds on the foundation of Community Ansible.&lt;/p&gt;

&lt;p&gt;Community Ansible is perfect for those looking for a free, open-source automation tool with a broad ecosystem.&lt;/p&gt;

&lt;p&gt;Red Hat Ansible Automation Platform Provides Professional support, A curated collection of certified modules, Regular updates, better integration with other Redhat products and additional features such as advanced analytics, logging, and integrations making it suitable for enterprises with critical workloads and requirements.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ansible vs Puppet vs Chef
&lt;/h2&gt;

&lt;p&gt;Comparison of Ansible with other automation tools.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--V99-gx7d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1697989176519/d51b111a-eafe-4cfc-94b9-5c8e17265b46.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--V99-gx7d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1697989176519/d51b111a-eafe-4cfc-94b9-5c8e17265b46.png" alt="Comparison between Ansible Vs Puppet Vs Chef" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In the ever-evolving realm of IT, the importance of efficient and scalable management cannot be overlooked. Ansible emerges as a robust solution, offering a blend of simplicity, versatility, and security in its automation capabilities. it's a game-changer in the landscape of IT automation.&lt;/p&gt;

</description>
      <category>automation</category>
      <category>ansible</category>
      <category>puppet</category>
      <category>chef</category>
    </item>
    <item>
      <title>Configuring Ansible For AWS Management</title>
      <dc:creator>Anuj Dube</dc:creator>
      <pubDate>Sun, 08 Oct 2023 13:18:25 +0000</pubDate>
      <link>https://dev.to/anujsd/configuring-ansible-for-aws-management-3ad</link>
      <guid>https://dev.to/anujsd/configuring-ansible-for-aws-management-3ad</guid>
      <description>&lt;p&gt;Amazon Web Services (AWS) offers a scalable environment for deploying applications, but as the infrastructure grows, so does the complexity of managing it. Organizations need an easy way to manage this infrastructure. they are using different tools to keep the infrastructure as code. Ansible is such an open-source tool. it can help you to create and manage your infrastructure in different cloud environments.&lt;/p&gt;

&lt;p&gt;In this article, we will look into how to create and manage AWS ec2 instances using Ansible.&lt;/p&gt;




&lt;h2&gt;
  
  
  Creating Ansible Control Node
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: Create an Ubuntu EC2 Instance on AWS
&lt;/h3&gt;

&lt;p&gt;After the Creation of the instance SSH into it. we will be using this as a control node to create and manage other AWS resources&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Install Ansible
&lt;/h3&gt;

&lt;p&gt;Update Repository and Upgrade Packages&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt update
sudo apt upgrade

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sometimes You will be asked to restart&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo reboot

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install Ansible&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo pip install ansible

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Install amazon.aws Ansible Collection
&lt;/h3&gt;

&lt;p&gt;Install amazon.aws ansible collection&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ansible-galaxy collection install amazon.aws

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;amazon.aws Ansible Collection needs boto3 and botocore packages to connect to AWS for creating and managing infrastructure&lt;/p&gt;

&lt;p&gt;First Install PIP&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt install python3-pip

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install boto3 and botocore packages&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install boto3 botocore

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 4: Install and Configure AWS CLI
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt install awscli

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Configure AWS credentials using the AWS CLI&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aws configure

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will be prompted to enter some details below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oSX7tM_Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696764712986/fc5a6390-2090-4120-99b7-1f7937ff17f7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oSX7tM_Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696764712986/fc5a6390-2090-4120-99b7-1f7937ff17f7.png" alt="" width="800" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To Get These Details First go to AWS.&lt;/p&gt;

&lt;p&gt;On the Right side, Click on your Profile. In Drop Down Menu You will see an option called "security credentials". Click On it&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1xsEUAZe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696764894472/12f80996-b91c-4275-b31a-fa9692765fbd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1xsEUAZe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696764894472/12f80996-b91c-4275-b31a-fa9692765fbd.png" alt="" width="707" height="642"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On this page if you move down You will see an option called "Access Keys".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uvSNZ4E---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696765106386/6e5a4416-7c50-448c-94cf-9db6dd2783da.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uvSNZ4E---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696765106386/6e5a4416-7c50-448c-94cf-9db6dd2783da.png" alt="" width="800" height="359"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on it and select first option "Command Line Interface"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SmW9UMB4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696765155260/a9e08823-4f49-467a-aaea-047d25f4009b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SmW9UMB4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696765155260/a9e08823-4f49-467a-aaea-047d25f4009b.png" alt="" width="800" height="355"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then Just Click next and create your access key.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4gb9wLq1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696765235778/63958e6c-f4da-4bd2-914b-c4bbcfd78813.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4gb9wLq1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696765235778/63958e6c-f4da-4bd2-914b-c4bbcfd78813.png" alt="" width="800" height="355"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now You have "Access Key ID" and "Secret Access Key".&lt;/p&gt;

&lt;p&gt;Please do not use keys from images They will not work I will be deleting this user and all his keys😉&lt;/p&gt;

&lt;p&gt;To Get the Default Region name click on Drop Down on the left side of the profile. I have taken the region Mumbai so the name will be ap-south-1&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pN-fdE0M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696765744852/b7ac7299-a6e4-4fc5-a13b-e9ff3ecb4698.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pN-fdE0M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696765744852/b7ac7299-a6e4-4fc5-a13b-e9ff3ecb4698.png" alt="" width="800" height="378"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After Entering all the details, We will check if we can connect to AWS or not&lt;/p&gt;

&lt;p&gt;Run Below Command It will show information about your EC2 instances&lt;/p&gt;

&lt;p&gt;if the below command fails then you must have made some mistake while entering configuration information please try again&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aws ec2 describe-instances

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Our Control Node is ready&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating and Managing EC2 Instances
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Example 1: Create a Single Ubuntu EC2 Instance with a Public IP Address
&lt;/h3&gt;

&lt;p&gt;For This, we are going to use the module "ec2_instance"&lt;/p&gt;

&lt;p&gt;Create a YAML File called create_ec2.yml&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
- name: Create Single Ubuntu EC2 Instance
  hosts: localhost
  connection: local
  gather_facts: no
  tasks:
    - name: Create Ubuntu EC2 instace with public ip
      amazon.aws.ec2_instance:
        name: "Created Using Ansible"
        key_name: "newUbuntu"
        vpc_subnet_id: subnet-0bd155c67558454b1
        instance_type: t2.micro
        security_group: sg-076a5ea097f046d8e
        network:
          assign_public_ip: true
        image_id: ami-0f5ee92e2d63afc18
        tags:
          Environment: Testing
      register: ec2_node_info
    - name: Display information
      debug:
        msg: "{{ec2_node_info}}"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;"key_name": SSH Key you will be using to login into the created server, You can use the same key that we are using to log into the control node&lt;/p&gt;

&lt;p&gt;You Can use the Same details as we are using in the control node&lt;/p&gt;

&lt;p&gt;Image_Id: In Details Tab&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3ibQlCFP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696767771383/0f7d7ab8-3442-4523-8c2e-bc6f73e325ca.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3ibQlCFP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696767771383/0f7d7ab8-3442-4523-8c2e-bc6f73e325ca.png" alt="" width="800" height="355"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;to create another type of instance you can go to "AMI catalog" and you can get image_id from there.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VIj1V-5f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696768226505/80826478-19e2-44b8-8f32-61ab7cc1b917.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VIj1V-5f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696768226505/80826478-19e2-44b8-8f32-61ab7cc1b917.png" alt="" width="800" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;vpc_subnet_id: In Networking Tab&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XWH-k3Ix--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696767889194/fe51361c-8949-4309-99c2-993a6ff4438c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XWH-k3Ix--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696767889194/fe51361c-8949-4309-99c2-993a6ff4438c.png" alt="" width="800" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;security_group: In the Security tab&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T-SqQ4oi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696767990066/5fc3fcd8-5a7b-494a-a75b-2fc4e06cab60.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T-SqQ4oi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696767990066/5fc3fcd8-5a7b-494a-a75b-2fc4e06cab60.png" alt="" width="800" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;instance_type: In Details Tab&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ukN2i0Xo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696768514824/b82dae23-280f-453b-80b9-3e082a634fa2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ukN2i0Xo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696768514824/b82dae23-280f-453b-80b9-3e082a634fa2.png" alt="" width="800" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To Get other instance types go to the "Instance Types" tab&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zuQFaVVB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696768458620/09947019-7273-4c8d-ae28-41186085f1d4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zuQFaVVB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696768458620/09947019-7273-4c8d-ae28-41186085f1d4.png" alt="" width="800" height="362"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To Run the playbook use the below command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ansible-playbook create_ec2.yml

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we are running it in localhost those options are mentioned in yml. so you can ignore warnings.&lt;/p&gt;

&lt;p&gt;You will see output like the below as we are printing created instance details&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fGlam0QM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696768954570/8912b1c5-cc1a-4bff-9753-0d379372f648.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fGlam0QM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696768954570/8912b1c5-cc1a-4bff-9753-0d379372f648.png" alt="" width="800" height="217"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now Go To AWS EC2 Console You will see a new instance with the name "Created Using Ansible" will be created.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OFUk_xY9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696769033521/6b104b9b-d9eb-4e77-b9ee-848d78b46b28.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OFUk_xY9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696769033521/6b104b9b-d9eb-4e77-b9ee-848d78b46b28.png" alt="" width="800" height="352"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 2: Get a List of all running Instances
&lt;/h3&gt;

&lt;p&gt;For This, We are going to "ec2_instance_info"&lt;/p&gt;

&lt;p&gt;Create a YAML File called get_all_ec2.yml&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
- name: Get All Running EC2 instances
  hosts: localhost
  connection: local
  gather_facts: no
  tasks:
    - name: Gather information about instances in states "running"
      amazon.aws.ec2_instance_info:
        filters:
          instance-state-name: "running"
      register: ec2_node_info
    - name: Get Running Instances Count
      debug:
        msg: "Total running instances: {{ ec2_node_info.instances | length }}"
    - name: Display information
      debug:
        msg: "{{ec2_node_info}}"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run Using Command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ansible-playbook get_all_ec2.yml

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will see output like below giving all running ec2 instances details&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Db7PwqDb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696769346426/f634fd45-ce24-48c7-b654-304bdc83a052.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Db7PwqDb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696769346426/f634fd45-ce24-48c7-b654-304bdc83a052.png" alt="" width="800" height="298"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 3: Terminate Every Instance From the Current AWS Region
&lt;/h3&gt;

&lt;p&gt;This will terminate the control node also.&lt;/p&gt;

&lt;p&gt;Create a File called terminate_all_ec2.yml&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
- name: Terminate Every Instance in Current AWS Region
  hosts: localhost
  connection: local
  gather_facts: no
  tasks:
    - name: Terminate every running instance in a region. Use with EXTREME caution.
      amazon.aws.ec2_instance:
        state: absent
        filters:
          instance-state-name: running

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Run this using below command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ansible-playbook terminate_all_ec2.yml

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will see That Your Control node is also terminated so you might be disconnected&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--na4tczZP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696769770374/cb002bbe-239f-4a75-b374-fe3a900a54fc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--na4tczZP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696769770374/cb002bbe-239f-4a75-b374-fe3a900a54fc.png" alt="" width="800" height="358"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;You can Manage and control lots of other AWS Resources like S3, VPC, Lambda, RDS etc. using Ansible.&lt;/p&gt;

&lt;p&gt;You can learn more about that using the below link&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.ansible.com/ansible/latest/collections/amazon/aws/index.html"&gt;AWS Ansible Documentation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Happy automating! 🤖&lt;/p&gt;

</description>
      <category>ansible</category>
      <category>aws</category>
      <category>devops</category>
      <category>automation</category>
    </item>
    <item>
      <title>Setting Up a Practical Ansible Lab on AWS: A Step-by-Step Guide</title>
      <dc:creator>Anuj Dube</dc:creator>
      <pubDate>Sun, 01 Oct 2023 14:58:28 +0000</pubDate>
      <link>https://dev.to/anujsd/setting-up-a-practical-ansible-lab-on-aws-a-step-by-step-guide-nca</link>
      <guid>https://dev.to/anujsd/setting-up-a-practical-ansible-lab-on-aws-a-step-by-step-guide-nca</guid>
      <description>&lt;p&gt;Ansible is a powerful, easy-to-use and easy-to-learn configuration management tool. If you are trying to learn Ansible then the first hurdle for you will be setting up a lab environment to practice Ansible.&lt;/p&gt;

&lt;p&gt;In this article, we are going to create a lab environment on AWS. You can use any other cloud platform like Azure, or Google Cloud or you can create vm on Virtual Box. I would suggest using AWS or Azure (here we will use AWS) as they offer 12 months of free trial you can play with lots of different OSs also. not suggesting Google Cloud as they don't offer a Windows server on a free trial.&lt;/p&gt;




&lt;p&gt;Now Let's get started, We are going to create 3 servers. All of them will be Ubuntu servers. We are going to use one server as the control node and the remaining two as target nodes.&lt;/p&gt;

&lt;p&gt;we can use Windows servers as the target node but not as the control node. here for setting up the lab, we are going to use Ubuntu servers only, as using a Windows server as a target has some extra steps. you can learn how to set up a Windows server for Ansible management &lt;a href="https://dev.to/anujsd/setting-up-a-windows-server-for-ansible-management-5bn7-temp-slug-5412652"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bewdxKsl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696161048416/dcae42f5-1ca2-461a-a97e-efff942bead5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bewdxKsl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696161048416/dcae42f5-1ca2-461a-a97e-efff942bead5.png" alt="" width="681" height="493"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Get an AWS Free Tier Account
&lt;/h2&gt;

&lt;p&gt;To get this account you might need to provide them with a credit/debit card. No need to worry they will not charge but remember to put a billing alert and close all resources after using them so you don't get any charges.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Create EC2 Instances
&lt;/h2&gt;

&lt;p&gt;Search EC2 on the Search Bar and click on that&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--A2qUyZfb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696161821700/03da2a22-e4f1-4bd3-8a0c-10d006589023.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--A2qUyZfb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696161821700/03da2a22-e4f1-4bd3-8a0c-10d006589023.png" alt="" width="800" height="356"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;you will see windows like the above. Click on Instances(running)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3Y6t5_9F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696161909893/467c3764-f5ee-416b-9fa7-f857764c4e88.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3Y6t5_9F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696161909893/467c3764-f5ee-416b-9fa7-f857764c4e88.png" alt="" width="800" height="355"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;now click on the Launch Instances Button at the right-top corner. and enter details below&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Name and tags:&lt;/strong&gt; Server1&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Application and OS Images:&lt;/strong&gt; Select Ubuntu do not change anything&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Instance type:&lt;/strong&gt; Keep Default Free Tier instance type&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key pair:&lt;/strong&gt; Click on "Create new key pair" and create a new key it will be downloaded automatically after creation keep it safe&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Network settings:&lt;/strong&gt; allows SSH, HTTP, HTTPS&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3Xv72doB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696162757381/4d181c11-9fe4-405a-857f-525342864c40.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3Xv72doB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696162757381/4d181c11-9fe4-405a-857f-525342864c40.png" alt="" width="800" height="527"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Keep Every other thing Default.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--A03Ok7QP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696163080022/1110c16d-4ede-4654-a020-52a306132b92.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--A03Ok7QP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696163080022/1110c16d-4ede-4654-a020-52a306132b92.png" alt="" width="800" height="357"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;on the right-hand side, you will see the summary. In "Number of instances" enter 3. then click on "Launch instance".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kb6LSc_I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696163219947/78228416-3349-4e8f-8571-0e0a446c5da5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kb6LSc_I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696163219947/78228416-3349-4e8f-8571-0e0a446c5da5.png" alt="" width="800" height="355"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now change their names as above&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Setting Up EC2 Instances
&lt;/h2&gt;

&lt;p&gt;I am using Windows So I will be using MobaXtrem as an SSH Client. If you are on Mac or Linux you don't need to install anything SSH Client is pre-installed on those OS.&lt;/p&gt;

&lt;p&gt;I have Stored My Key Pair named "newUbuntu.pem" which was generated while creating instances on location "C:\temp\aws".&lt;/p&gt;

&lt;p&gt;First, we will go through the steps for Mac/Linux users and then for Windows.&lt;/p&gt;

&lt;h3&gt;
  
  
  Steps For Mac/Linux Users
&lt;/h3&gt;

&lt;p&gt;Go to the location where you have stored your key pair and open the terminal there&lt;/p&gt;

&lt;p&gt;Now On the AWS console, Select "Client" Server and Click on the Connect button at the top. then Go to SSH Client and copy the example SSH command&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ad1rT8Qb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696164123614/ae662d32-d2de-4361-99c8-2ffe7a4b1d3e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ad1rT8Qb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696164123614/ae662d32-d2de-4361-99c8-2ffe7a4b1d3e.png" alt="" width="800" height="506"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Paste That command in the terminal and press enter. it will ask for yes/no enter yes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2-IOH1O_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696164761874/56902403-889f-41e4-a5d0-eb1e0f2b084f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2-IOH1O_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696164761874/56902403-889f-41e4-a5d0-eb1e0f2b084f.png" alt="" width="800" height="481"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you are logged in to the control node. Now logout enter "exit"&lt;/p&gt;

&lt;p&gt;We are going to copy our private SSH Key into the control node. for that use the below command. do not forget to update the command with your values&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# scp -i path/to/your/private/key.pem path/to/your/local/file user@public-dns-name:/path/to/destination/directory
scp -i newUbuntu.pem newUbuntu.pem ubuntu@ec2-3-110-197-196.ap-south-1.compute.amazonaws.com:~/

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4SXYdDUH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696170480911/66612e9a-5af8-436e-9621-62eaea767fd7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4SXYdDUH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696170480911/66612e9a-5af8-436e-9621-62eaea767fd7.png" alt="" width="800" height="70"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;now again log into the server and you can see your private key is copied like below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qQO8219R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696169948814/2864ac44-8a32-4b9d-89ec-a929b5f4091c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qQO8219R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696169948814/2864ac44-8a32-4b9d-89ec-a929b5f4091c.png" alt="" width="578" height="50"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now Type the below command to ensure your key is not publicly viewable&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chmod 400 newUbuntu.pem

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now we will try to see if our control node can SSH into server1 and server2 or not.&lt;/p&gt;

&lt;p&gt;Go to AWS Console select server1, click on the connect button, and copy the ssh command like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh -i "newUbuntu.pem" ubuntu@ec2-3-110-197-196.ap-south-1.compute.amazonaws.com

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now paste the command into the terminal of the control node and press enter&lt;/p&gt;

&lt;p&gt;it might ask for yes/no. press yes. now you are in server1.&lt;/p&gt;

&lt;p&gt;now type "exit" to exit from server1 to the control node.&lt;/p&gt;

&lt;p&gt;Copy the SSH command for server2 also and try SSH into it.&lt;/p&gt;

&lt;p&gt;You will be able to SSH into server2 also.&lt;/p&gt;

&lt;p&gt;now you can directly go to the "4. Installing Ansible" step.&lt;/p&gt;

&lt;h3&gt;
  
  
  Steps For Windows Users
&lt;/h3&gt;

&lt;p&gt;Download MobaXterm From &lt;a href="https://mobaxterm.mobatek.net/download.html"&gt;here&lt;/a&gt; and Install it.&lt;/p&gt;

&lt;p&gt;When MobaXterm is first started click on Settings -&amp;gt; Configuration&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VSMZM__L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696164368994/1857f1a3-50fb-48cd-9691-10215e1891d3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VSMZM__L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696164368994/1857f1a3-50fb-48cd-9691-10215e1891d3.png" alt="" width="800" height="258"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Change Home and Root Directory to the location where the key is stored. in my case it is C:\temp\aws. Click on OK. It will ask for a restart Click on Yes. MobaXStream will restart.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lALPujW_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696164480475/73984c7f-9a26-454c-8302-72440c66c34a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lALPujW_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696164480475/73984c7f-9a26-454c-8302-72440c66c34a.png" alt="" width="800" height="563"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now Click On Start Local Terminal&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JYskdOHS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696164632761/355d0a3b-aa5c-442e-ac89-2c72091f144c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JYskdOHS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696164632761/355d0a3b-aa5c-442e-ac89-2c72091f144c.png" alt="" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we run "ls" it should show your key in the current directory&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QLBa3Qy_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696164679407/8ef31c9a-b63d-45b3-b53d-94d59b2266a0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QLBa3Qy_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696164679407/8ef31c9a-b63d-45b3-b53d-94d59b2266a0.png" alt="" width="800" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now On the AWS console, Select "Client" Server and Click on the Connect button at the top. then Go to SSH Client and copy the example SSH command&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ad1rT8Qb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696164123614/ae662d32-d2de-4361-99c8-2ffe7a4b1d3e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ad1rT8Qb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696164123614/ae662d32-d2de-4361-99c8-2ffe7a4b1d3e.png" alt="" width="800" height="506"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Paste That command in the terminal and press enter. it will ask for yes/no enter yes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2-IOH1O_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696164761874/56902403-889f-41e4-a5d0-eb1e0f2b084f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2-IOH1O_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696164761874/56902403-889f-41e4-a5d0-eb1e0f2b084f.png" alt="" width="800" height="481"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you are logged in to the control node. On the left-hand side, you will see SSH Browser.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SCRfCby6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696170665032/1bf4b751-f393-4a7c-a2a1-09085fb5605a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SCRfCby6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696170665032/1bf4b751-f393-4a7c-a2a1-09085fb5605a.png" alt="" width="418" height="598"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Upload your private key&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GNIO2nxr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696170715287/3341916d-eb9f-47a1-8eb8-6aa9e080c1fb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GNIO2nxr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696170715287/3341916d-eb9f-47a1-8eb8-6aa9e080c1fb.png" alt="" width="421" height="572"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now Type the below command to ensure your key is not publicly viewable&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chmod 400 newUbuntu.pem

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now we will try to see if our control node can SSH into server1 and server2 or not.&lt;/p&gt;

&lt;p&gt;Go to AWS Console select server1, click on the connect button, and copy the ssh command like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh -i "newUbuntu.pem" ubuntu@ec2-3-110-197-196.ap-south-1.compute.amazonaws.com

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now paste the command into the terminal of the control node and press enter&lt;/p&gt;

&lt;p&gt;it might ask for yes/no. press yes. now you are in server1.&lt;/p&gt;

&lt;p&gt;now type "exit" to exit from server1 to the control node.&lt;/p&gt;

&lt;p&gt;Copy the SSH command for server2 also and try SSH into it.&lt;/p&gt;

&lt;p&gt;You will be able to SSH into server2 also.&lt;/p&gt;

&lt;p&gt;Windows Users can also use SCP to copy private SSH key to control the server. But Using MobaXterm is much easier.&lt;/p&gt;

&lt;p&gt;As Learning Experience you can try SCP also.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Installing Ansible
&lt;/h2&gt;

&lt;p&gt;Update Package list and upgrade Packages&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt update
sudo apt upgrade

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sometimes you may be asked to reboot then you can reboot using the below command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo reboot

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install Ansible&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt install ansible

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Let's Create a Folder Called "ansiblePractice" and go into that folder&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir ansiblePractice
cd ansiblePractice

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a File called "inventory" and enter the public IP address of server1 and server2 in there like below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nano inventory

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0oUgsD_6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696166874073/adad6db7-cdc8-43b4-a090-6423db29bda8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0oUgsD_6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696166874073/adad6db7-cdc8-43b4-a090-6423db29bda8.png" alt="" width="800" height="454"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ctrl + O to Save -&amp;gt; Press Enter -&amp;gt; Ctrl + X to exit.&lt;/p&gt;

&lt;p&gt;Now we will execute our first ansible ad-hoc command. Sometimes it can ask for yes/no. press yes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ansible all --key-file newUbuntu.pem -i inventory -m ping

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--o_kvKpv6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696167180456/059166db-8761-435a-a342-607ab3fa7aa9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--o_kvKpv6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696167180456/059166db-8761-435a-a342-607ab3fa7aa9.png" alt="" width="800" height="218"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Now Your Lab Environment Is Ready You can start learning Ansible.&lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-1702900896705970451-655" src="https://platform.twitter.com/embed/Tweet.html?id=1702900896705970451"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1702900896705970451-655');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1702900896705970451&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;p&gt;Happy automating! 🤖&lt;/p&gt;

</description>
      <category>ansible</category>
      <category>aws</category>
      <category>automation</category>
      <category>devops</category>
    </item>
    <item>
      <title>Setting up a Windows Server for Ansible Management</title>
      <dc:creator>Anuj Dube</dc:creator>
      <pubDate>Sun, 24 Sep 2023 10:31:53 +0000</pubDate>
      <link>https://dev.to/anujsd/setting-up-a-windows-server-for-ansible-management-1ag8</link>
      <guid>https://dev.to/anujsd/setting-up-a-windows-server-for-ansible-management-1ag8</guid>
      <description>&lt;p&gt;Ansible, a widely used configuration management tool, is not just for Unix-like systems. With the right setup, it can also be used to manage Windows servers. This article will guide you through the process of setting up a Windows server to be controlled using Ansible.&lt;/p&gt;

&lt;p&gt;For this demonstration, we'll be setting up two instances of "Microsoft Windows Server 2022 Base" as child nodes. Additionally, we'll create an Ubuntu instance to serve as our control node.&lt;/p&gt;

&lt;p&gt;Ansible Uses SSH for connecting to Linux systems, And for Windows systems it uses Winrm.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Steps to Configure a Windows Server&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Open Powershell in Administrator mode and run the below command. it will run a script that will do all the necessary configurations. yes, that's it on the Window side.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Invoke-WebRequest -Uri https://raw.githubusercontent.com/rallabandisrinivas/winrm_ansible/main/README.md -UseBasicParsing | Select-Object -ExpandProperty Content | Invoke-Expression

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--b7_jFokh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1695548596995/d2d5bf96-e994-4ecd-ba74-9f0cd0fa5afc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--b7_jFokh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1695548596995/d2d5bf96-e994-4ecd-ba74-9f0cd0fa5afc.png" alt="" width="800" height="195"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can refer to the official documentation below if you face any issues&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.ansible.com/ansible/latest/os_guide/windows_setup.html"&gt;Ansible Windows Setup Link&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Steps to Configure a Ubuntu Server&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Update Package list and upgrade Packages&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt update
sudo apt upgrade
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sometimes you may be asked to reboot then you can reboot using the below command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo reboot
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Install python3-pip and then install pywinrm package.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt install python3-pip
pip install pywinrm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Let's install ansible&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt install ansible
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4: Now create an inventory file and add IP and authentication info about Windows servers in it.&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;inventory&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; [windows]
 server1 ansible_host=65.2.122.214 ansible_user=Administrator ansible_password=3zA)--5TI$4pFng6*=qnAVLudgYtTqRP
 server2 ansible_host=15.206.194.23 ansible_user=Administrator ansible_password=3zA)--5TI$4pFng6*=qnAVLudgYtTqRP

 [windows:vars]
 ansible_connection=winrm
 ansible_winrm_server_cert_validation=ignore
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yes, Storing Passwords in plain text is a security risk we can use ansible-vault. But for the sake of the demo, we are storing it in plain text.&lt;/p&gt;

&lt;p&gt;In Production environments, We can Use Ansible Vault to securely store and use passwords.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5: Now We will be running the below Adhoc command to test the connectivity&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ansible windows -m win_ping -i inventory
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if you have created instances on the cloud and have not opened Winrm ports you might get the below error.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6gzoNZNh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1695547185222/f3b37944-f565-4bdf-bf85-64473ad786d8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6gzoNZNh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1695547185222/f3b37944-f565-4bdf-bf85-64473ad786d8.png" alt="" width="800" height="106"&gt;&lt;/a&gt;&lt;br&gt;
Open those ports and run the command again&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T_Jcgkkd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1695547246780/56c0be98-57b0-4fa4-93ff-373eaae3fc36.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T_Jcgkkd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1695547246780/56c0be98-57b0-4fa4-93ff-373eaae3fc36.png" alt="" width="800" height="166"&gt;&lt;/a&gt;&lt;br&gt;
Successful execution will look like the below&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lHgGboUg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1695549971121/1acd5798-6ae6-4252-a8a7-a15d34b744bb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lHgGboUg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1695549971121/1acd5798-6ae6-4252-a8a7-a15d34b744bb.png" alt="" width="800" height="181"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Now, with all the Configurations in place, you can fully automate a myriad of tasks on your Windows servers.&lt;/p&gt;

&lt;p&gt;Happy automating! 🤖&lt;/p&gt;

</description>
      <category>ansible</category>
      <category>automation</category>
      <category>windowsserver</category>
      <category>setup</category>
    </item>
    <item>
      <title>Ansible Conditionals</title>
      <dc:creator>Anuj Dube</dc:creator>
      <pubDate>Sun, 17 Sep 2023 04:36:10 +0000</pubDate>
      <link>https://dev.to/anujsd/ansible-conditionals-2hje</link>
      <guid>https://dev.to/anujsd/ansible-conditionals-2hje</guid>
      <description>&lt;p&gt;In Ansible, conditionals offer control in playbooks, allowing tasks, plays, or even entire roles to be executed or skipped based on certain conditions. Here's an overview of the most used conditionals and their uses:&lt;/p&gt;

&lt;h2&gt;
  
  
  1. when
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The most commonly used conditional. It determines if a task or play should be executed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You have to write an expression that evaluates to a boolean (true/false).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;You want some tasks to be executed only on some specific servers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---

- hosts: all
  become: true
  tasks:
  - name: update repository index Ubuntu
    apt:
      update_cache: yes
    when: ansible_distribution == "Ubuntu"

  - name: update repository index Centos
    yum:
      update_cache: yes
    when: ansible_distribution == "CentOS"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here while executing the task. Ansible will first check the when condition. If it's true only then it will execute the main task.&lt;/p&gt;

&lt;p&gt;You can see it's skipping if the condition is not true.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--i9yo8rB9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694444459657/caa1634a-ef45-486d-aff5-d796877bc97d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--i9yo8rB9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694444459657/caa1634a-ef45-486d-aff5-d796877bc97d.png" alt="" width="800" height="246"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;From where we are getting this "ansible_os_family" variable?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;while running every Ansible playbook. By default, Ansible runs the gather_facts task. that task gathers all information about servers. which gives us access to these variables.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. changed_when
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Modifies the behavior of a task to report a "changed" status.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You have to write an expression that evaluates to a boolean (true/false).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;Updating the repository is a basic task Suppose you don't want to consider that as a change then you can write it like below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---

- hosts: all
  become: true
  tasks:

  - name: update repository index
    apt:
      update_cache: yes
    when: ansible_distribution == "Ubuntu"
    changed_when: false

  - name: update repository index
    yum:
      update_cache: yes
    when: ansible_distribution == "CentOS"
    changed_when: false

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will give you a changed=0&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rjiesEf9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694442429192/2a25385a-c982-48fb-8a88-870a71cb7836.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rjiesEf9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694442429192/2a25385a-c982-48fb-8a88-870a71cb7836.png" alt="" width="800" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. failed_when
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Modifies the behavior of a task to report a "failed" status based on custom conditions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example: You want to check disk usage and want to mark it as a failure if usage is more than 90%.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---

- hosts: all
  vars:
    disk_usage_threshold: 90
  tasks:
    - name: Check root filesystem disk usage
      command: df -h /
      register: df_output
      changed_when: false
      failed_when: &amp;gt;
        (ansible_distribution == "CentOS" and (df_output.stdout_lines[-1].split()[4]|replace('%',''))|int &amp;gt; disk_usage_threshold) or
        (ansible_distribution == "Ubuntu" and (df_output.stdout_lines[1].split()[4]|replace('%',''))|int &amp;gt; disk_usage_threshold)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output will be like the below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HSosWObU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694443349704/f920d39d-5dbc-40ea-b284-720e9bddc15b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HSosWObU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694443349704/f920d39d-5dbc-40ea-b284-720e9bddc15b.png" alt="" width="800" height="197"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;as my disks are not filled let me fill the disk of one server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VacrgN8f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694444270759/7abfac68-bdc4-493a-a7c5-faba2af99896.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VacrgN8f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694444270759/7abfac68-bdc4-493a-a7c5-faba2af99896.png" alt="" width="800" height="227"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you can see that for one server script has failed showing disk is filled 94%.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. until
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Used for retrying a task until a certain condition is met or a certain number of retries is reached.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example: Retry a task until a file exists.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
- hosts: all
  tasks:
    - name: Check if file exists
      stat:
        path: "/tmp/file.txt"
      register: file_status
      until: file_status.stat.exists
      retries: 5
      delay: 3

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see playbook is trying to check if file exist at that location or not. while it's running i logged into one server and created file. you can see it's showing file found for one server. it kept trying for another server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FLtc6rcP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694445715462/1c190158-2d07-445d-917d-522d767ed820.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FLtc6rcP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694445715462/1c190158-2d07-445d-917d-522d767ed820.png" alt="" width="800" height="277"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  5. assert
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Used for failing a play if certain conditions are not met.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example: Fail if the free memory is less than 10 GB.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
- hosts: all
  tasks:
    - name: Get filesystem details for /
      ansible.builtin.setup:
        filter: ansible_mounts

    - name: Assert minimum disk space requirements
      assert:
        that:
          - item.size_available &amp;gt;= 10000000000 # 10 GB in bytes
        fail_msg: "Not enough disk space on {{ item.mount }}. At least 10GB required."
        success_msg: "{{ item.mount }} has sufficient space."
      with_items: "{{ ansible_mounts }}"
      when: item.mount == "/"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7tUF_MOh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694532686908/a8a0f46c-373b-4852-8935-7bb419760066.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7tUF_MOh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694532686908/a8a0f46c-373b-4852-8935-7bb419760066.png" alt="" width="800" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XZq3_TdH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694532708709/164f9239-d904-41fd-8155-8e0850ad7571.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XZq3_TdH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694532708709/164f9239-d904-41fd-8155-8e0850ad7571.png" alt="" width="800" height="439"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n_e5-ThC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694532732436/7dc06c1f-82e8-411e-80e3-0229e3535abf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n_e5-ThC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694532732436/7dc06c1f-82e8-411e-80e3-0229e3535abf.png" alt="" width="800" height="291"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see in the above image it failed for one server having insufficient space and was successful for another.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. block and rescue
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;block&lt;/code&gt; groups tasks together.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;rescue&lt;/code&gt; defines tasks to be executed if any task in the &lt;code&gt;block&lt;/code&gt; fails.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It's like try and catch in javascript, java etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example: Attempt to install a package and handle failures.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
- hosts: all
  become: yes
  vars:
    package_name: example_package
  tasks:
    - name: Attempt to install a package
      block:
        - name: Install the "{{ package_name }}"
          package:
            name: "{{ package_name }}"
            state: present
      rescue:
        - name: Handle the failure
          debug:
            msg: "Failed to install {{ package_name }} . Check if the package name is correct or if there's a repo issue."
      always:
        - name: This always gets executed
          debug:
            msg: "This message will always be displayed, regardless of success or failure in the block above."

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see above script will fail to install a package called example_package as no such package exists. still, we are not shown any failure(failed=0). You can see the task in rescue is executed correctly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d9UlKeW0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694453046234/303d6d79-2231-4b2b-b378-c301aae705b8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d9UlKeW0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694453046234/303d6d79-2231-4b2b-b378-c301aae705b8.png" alt="" width="800" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;if we put package_name=wget. which is actually a real package name. task will succeed. It will look like below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xA9t0_uB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694453165789/1666e167-667d-42a8-b016-71f7de3f5aeb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xA9t0_uB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694453165789/1666e167-667d-42a8-b016-71f7de3f5aeb.png" alt="" width="800" height="305"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Conditional imports and includes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Conditionally import playbooks or include tasks/roles based on certain conditions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example: install web server depending on user input&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;web_setup.yml&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
- hosts: all
  become: true
  vars_prompt:
    - name: webserver_type
      prompt: "Which web server do you want to install? (apache/nginx)"
      private: no

  tasks:
    - name: Include tasks based on web server choice
      include_tasks: "install_{{ webserver_type }}.yml"
      when: webserver_type in ['apache', 'nginx']

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;install_apache.yml&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
- name: Install Apache
  package:
    name: apache2
    state: present
  when: ansible_distribution == "Ubuntu"

- name: Install httpd
  package:
    name: httpd 
    state: present
  when: ansible_distribution == "CentOS"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;install_nginx.yml&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
- name: Install Nginx
  ansible.builtin.package:
    name: nginx
    state: present

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so depending on user input it will select playbook and run it.&lt;/p&gt;

&lt;p&gt;for apache as input you can see it ran install_apache.yml&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yM7K7_yy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694532223061/bfb14e81-ff58-4660-a8b8-74dc3666ceaa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yM7K7_yy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694532223061/bfb14e81-ff58-4660-a8b8-74dc3666ceaa.png" alt="" width="800" height="314"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For nginx as input, you can see it ran install_nginx.yml&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6a4nzplz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694532299980/a6b8fd40-464a-419d-90c7-5e850d01f60a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6a4nzplz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1694532299980/a6b8fd40-464a-419d-90c7-5e850d01f60a.png" alt="" width="800" height="256"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;In conclusion, Ansible's conditionals provide a powerful way to bring logic and decision-making into playbooks, ensuring that infrastructure automation is both flexible and precise. Like any tool, the key is understanding the available options and applying them judiciously.&lt;/p&gt;

&lt;p&gt;Happy automating! 🤖&lt;/p&gt;

</description>
      <category>ansible</category>
      <category>automation</category>
      <category>ansibleconditionals</category>
    </item>
    <item>
      <title>Copying Files From Local To Remote Using PowerShell</title>
      <dc:creator>Anuj Dube</dc:creator>
      <pubDate>Wed, 16 Aug 2023 14:31:23 +0000</pubDate>
      <link>https://dev.to/anujsd/copying-files-from-local-to-remote-using-powershell-fna</link>
      <guid>https://dev.to/anujsd/copying-files-from-local-to-remote-using-powershell-fna</guid>
      <description>&lt;p&gt;Let's say you want to copy files/folders from a local device to one or more remote Windows servers. We have lots of ways to do this but in this article, we will be looking into how to do that using PowerShell. In your local and remote at both places, you need to enable PowerShell remoting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Check and set the execution policy&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Get-ExecutionPolicy

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if the exception policy is not &lt;strong&gt;RemoteSigned&lt;/strong&gt; set it to RemoteSigned.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Set-ExecutionPolicy RemoteSigned

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Enable PowerShell remoting&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Please keep in mind, When the &lt;strong&gt;SkipNetworkProfileCheck&lt;/strong&gt; parameter is specified, it bypasses the check for network profiles during the configuration process. This allows PowerShell remoting to be enabled even if the network profile is not classified as private or domain.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Enable-PSremoting -SkipNetworkProfileCheck

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now to the main part&lt;/p&gt;

&lt;h2&gt;
  
  
  Method 1: Using &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/copy-item?view=powershell-7.3"&gt;Copy-Item&lt;/a&gt; with session
&lt;/h2&gt;

&lt;p&gt;The most simple and best way. just use the &lt;strong&gt;ToSession&lt;/strong&gt; parameter provided within copy-item.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$sourcePath = "C:\temp\files"
$destinationPath = "C:\temp\files"
$serverName = "&amp;lt;server name&amp;gt;"
$username = "&amp;lt;username&amp;gt;"

$credentail = Get-Credential -Message "Enter Credentials" -UserName $username
$session = New-PSSession -ComputerName $serverName -Credential $credentail

Copy-Item -Path $sourcePath -Destination $destinationPath -Recurse -ToSession $session

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in the above script, we created a PowerShell session to that remote Windows server and then we are copying the whole folder from source to destination.&lt;/p&gt;

&lt;h2&gt;
  
  
  Method 2: Using Copy-Item with PS Drive
&lt;/h2&gt;

&lt;p&gt;Here we are creating a ps-drive and then doing the copying part. this method helps to interact with the remote location in a way that resembles working with a local drive.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$sourcePath = "C:\temp\files"
$destinationPath = "C:\temp\files"
$serverName = "&amp;lt;server name&amp;gt;"
$username = "&amp;lt;username&amp;gt;"
$driveName = "R"

$credentail = Get-Credential -Message "Enter Credentials" -UserName $username

New-PSDrive -Name $driveName -PSProvider FileSystem -Root $destinationPath -Credential $credentail

Copy-Item -Path $sourcePath -Destination $driveName:\ -Recurse

Remove-PSDrive -Name $driveName

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Method 3: Using &lt;a href="https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy"&gt;Robocopy&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Robocopy&lt;/strong&gt; (Robust File Copy) is a command-line utility built into Windows that provides advanced options for copying files and directories.&lt;/p&gt;

&lt;p&gt;While using robocopy easier way is to create a local drive and then use that for copying files.&lt;/p&gt;

&lt;p&gt;keep in mind drive name should be a single character like A, R, X etc. Not names like "newDrive", "test" etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$sourcePath = "C:\temp\files"
$destinationPath = "C:\temp\files"
$serverName = "&amp;lt;server name&amp;gt;"
$username = "&amp;lt;username&amp;gt;"
$driveName = "R"

$credentail = Get-Credential -Message "Enter Credentials" -UserName $username

robocopy $sourcePath $driveName:\ /E /Z /COPYALL /R:3 /W:1

Remove-PSDrive -Name $driveName

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Meaning of copy options we have passed&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;/E:&lt;/strong&gt; copies subdirectories, including empty ones.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;/Z:&lt;/strong&gt; enables restartable mode.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;/COPYALL:&lt;/strong&gt; copies all file information.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;/R:3:&lt;/strong&gt; specifies 3 retries on failed copies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;/W:1:&lt;/strong&gt; sets the wait time between retries to 1 second.&lt;/p&gt;




</description>
      <category>powershell</category>
      <category>automation</category>
      <category>copy</category>
      <category>robocopy</category>
    </item>
  </channel>
</rss>
