<?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>Forem: Efosa Oviawe</title>
    <description>The latest articles on Forem by Efosa Oviawe (@efosa-dev).</description>
    <link>https://forem.com/efosa-dev</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%2F1722640%2F3ed6f359-1948-48ed-9c0e-21a986bb5fa5.jpg</url>
      <title>Forem: Efosa Oviawe</title>
      <link>https://forem.com/efosa-dev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/efosa-dev"/>
    <language>en</language>
    <item>
      <title>MID INTERNSHIP TASK: Building devopsfetch for Server Information Retrieval and Monitoring.</title>
      <dc:creator>Efosa Oviawe</dc:creator>
      <pubDate>Tue, 23 Jul 2024 02:29:33 +0000</pubDate>
      <link>https://forem.com/efosa-dev/mid-internship-task-building-devopsfetch-for-server-information-retrieval-and-monitoring-2bj2</link>
      <guid>https://forem.com/efosa-dev/mid-internship-task-building-devopsfetch-for-server-information-retrieval-and-monitoring-2bj2</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Welcome to the documentation for DevOpsFetch, a powerful yet straightforward monitoring tool designed to meet the requirements of HNG Internship Task 5. At first glance, the task of creating a system to monitor various aspects of a server environment may seem simple. However, achieving this requires close attention to detail and a comprehensive understanding of different system components.&lt;/p&gt;

&lt;p&gt;DevOpsFetch encapsulates this challenge, providing an intuitive solution that monitors:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Active network ports&lt;/li&gt;
&lt;li&gt;Docker container statuses&lt;/li&gt;
&lt;li&gt;Nginx configurations and domains&lt;/li&gt;
&lt;li&gt;User login activities&lt;/li&gt;
&lt;li&gt;System activities within specified time ranges&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This documentation will guide you through the installation, configuration, and usage of DevOpsFetch. With clear instructions and practical examples, you'll find that what initially seemed complex can be managed efficiently with the right approach and tools. Let's dive in and simplify your monitoring tasks with DevOpsFetch.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation and Configuration
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Ensure your system has the necessary dependencies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;net-tools&lt;/li&gt;
&lt;li&gt;nginx&lt;/li&gt;
&lt;li&gt;docker.io&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Installation Steps
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Clone and Run the Installer Script:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone https://github.com/Oviawe007/HNG-Task-5.git
cd HNG-Task-5.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make the script executable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chmod +x devopsfetch.sh
chmod +x install.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the script:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Check the Installation:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Verify that the &lt;code&gt;devopsfetch.sh&lt;/code&gt; script is installed in 
&amp;gt; /opt/devopsfetch/
and is executable.&lt;/li&gt;
&lt;li&gt;Confirm that a symbolic link to &lt;code&gt;devopsfetch.sh&lt;/code&gt; is created at 
&amp;gt; /usr/local/bin/devopsfetch.&lt;/li&gt;
&lt;li&gt;Ensure the &lt;code&gt;devopsfetch.service&lt;/code&gt; file is created in 
&amp;gt; /etc/systemd/system/.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Check the Service:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl status devopsfetch.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6f1dfbsv2ys67hlmn1po.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6f1dfbsv2ys67hlmn1po.png" alt="service info" width="800" height="226"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuration
&lt;/h2&gt;

&lt;p&gt;The service is configured to run &lt;code&gt;devopsfetch.sh&lt;/code&gt; every hour by default. You can modify the service configuration in &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;/etc/systemd/system/devopsfetch.service if needed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Usage Examples
&lt;/h2&gt;

&lt;p&gt;Run &lt;code&gt;devopsfetch.sh&lt;/code&gt; as a root user with the appropriate flags to fetch the desired information.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Show Help:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ./devopsfetch.sh -h
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbdg3ju009ticpgzkwo9q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbdg3ju009ticpgzkwo9q.png" alt="devopsfetch help" width="800" height="187"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Get Active Ports:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ./devopsfetch.sh -p
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2hixrx98849pbtdfgr1z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2hixrx98849pbtdfgr1z.png" alt="port info" width="800" height="149"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Get Specific Port Info:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ./devopsfetch.sh -p [PORT]
# Example: sudo devopsfetch -p 4369
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fidu3yayz9vsyncj647vz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fidu3yayz9vsyncj647vz.png" alt="port info" width="800" height="44"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Get Docker Info:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ./devopsfetch.sh -d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ckms5f0ch0e3et0vgwe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ckms5f0ch0e3et0vgwe.png" alt="docker info" width="800" height="180"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Get Specific Docker Container Info:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ./devopsfetch.sh -d [CONTAINER_NAME]
# Example: sudo ./devopsfetch.sh -d my_container
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Get Nginx Info:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ./devopsfetch.sh -n
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Get Specific Nginx Domain Info:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ./devopsfetch.sh -n [DOMAIN]
# Example: sudo ./devopsfetch.sh -n example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo7j9xrq2bngbhj2kpzvr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo7j9xrq2bngbhj2kpzvr.png" alt="nginx info" width="800" height="112"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Get User Logins:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ./devopsfetch.sh -u
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw483ymyjujswphc97t4p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw483ymyjujswphc97t4p.png" alt="login info" width="800" height="248"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Get Specific User Info:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ./devopsfetch.sh -u [USER]
# Example: sudo ./devopsfetch.sh -u ubuntu
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fju2drs0vdzthaj75ndp4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fju2drs0vdzthaj75ndp4.png" alt="ubuntu info" width="800" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Get Activities in Time Range:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ./devopsfetch.sh -t [START_TIME] [END_TIME]
# Example: sudo ./devopsfetch.sh -t "2024-07-22 10:00:00" "2024-07-22 11:00:00"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8vvhc83zrs4xwnxll0i8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8vvhc83zrs4xwnxll0i8.png" alt="Time info" width="800" height="166"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Logging Mechanism&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Log Rotation Configuration
The installation script sets up log rotation for &amp;gt; /var/log/syslog
to ensure logs do not consume excessive disk space. The log rotation configuration is defined in &amp;gt; /etc/logrotate.d/devopsfetch.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;View Logs&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To retrieve and view logs, you can use the &lt;code&gt;tail -f&lt;/code&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tail -f /var/log/devopsfetch.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will display the logs for the DevOpsFetch service, allowing you to monitor its activities and debug if necessary.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;DevOpsFetch provides a comprehensive solution for monitoring various system and application metrics, including active ports, Docker containers, Nginx configurations, and user logins. By following the installation steps and using the provided command-line flags, you can effectively gather and analyze the information needed to maintain and troubleshoot your system. The logging mechanism ensures that you have a record of all activities for further analysis and auditing.&lt;/p&gt;

&lt;p&gt;Leave your questions and feedback in the comments.&lt;br&gt;
Thank you.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>bash</category>
      <category>learning</category>
      <category>docker</category>
    </item>
    <item>
      <title>Provision, Authentication, and Configuration on AWS Using Ansible</title>
      <dc:creator>Efosa Oviawe</dc:creator>
      <pubDate>Sun, 21 Jul 2024 10:03:07 +0000</pubDate>
      <link>https://forem.com/efosa-dev/provision-authentication-and-configuration-on-aws-using-ansible-31bp</link>
      <guid>https://forem.com/efosa-dev/provision-authentication-and-configuration-on-aws-using-ansible-31bp</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://docs.ansible.com/" rel="noopener noreferrer"&gt;Ansible&lt;/a&gt;, a powerful configuration management and deployment tool, offers robust capabilities for managing infrastructure. One critical aspect of server management is the ability to gracefully shut down systems based on specific criteria. This article delves into leveraging Ansible's conditional logic and powerful modules to automate the shutdown of Ubuntu instances. By combining Ansible's flexibility with precise conditions, we'll explore how to create tailored shutdown procedures that align with your infrastructure's need.&lt;/p&gt;

&lt;p&gt;We'll examine the core concepts, provide practical code examples, and discuss best practices for implementing Ansible-based server shutdown automation.&lt;/p&gt;

&lt;p&gt;Key areas we will cover include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Understanding Ansible's conditional logic&lt;/li&gt;
&lt;li&gt;Effectively utilizing the command module for shutdown operations&lt;/li&gt;
&lt;li&gt;Incorporating shutdown criteria based on factors like operating system, hostname, and uptime&lt;/li&gt;
&lt;li&gt;Best practices for ensuring reliable and efficient shutdown processes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By the end of this article, you'll gain a solid understanding of how to harness Ansible to automate server shutdown, enhancing your infrastructure management capabilities.&lt;/p&gt;

&lt;h2&gt;
  
  
  Requirements
&lt;/h2&gt;

&lt;p&gt;The below requirements are needed on the host that executes this module as stated in &lt;a href="https://docs.ansible.com/ansible/latest/collections/amazon/aws/ec2_instance_module.html" rel="noopener noreferrer"&gt;Ansible docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;python &amp;gt;= 3.6&lt;br&gt;
boto3 &amp;gt;= 1.26.0&lt;br&gt;
botocore &amp;gt;= 1.29.0&lt;/p&gt;
&lt;h2&gt;
  
  
  Tasks
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create three(3) EC2 instances on AWS using Ansible loops

&lt;ul&gt;
&lt;li&gt;2 Ubuntu Instances&lt;/li&gt;
&lt;li&gt;1 Amazon Linux Instance&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Set up passwordless authentication between Ansible control node and newly created instances.&lt;/li&gt;
&lt;li&gt;Automate the shutdown of Ubuntu Instances only using Ansible Conditionals&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Step One - Installations
&lt;/h2&gt;

&lt;p&gt;Install Ansible on Mac&lt;/p&gt;

&lt;p&gt;&lt;code&gt;brew install ansible&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fea36di58l4sdkrdh62qp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fea36di58l4sdkrdh62qp.png" alt="Ansible installation" width="800" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Install boto3&lt;br&gt;
&lt;code&gt;pip install boto3&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb0cojbj85hmu05x9q13q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb0cojbj85hmu05x9q13q.png" alt="pip installation" width="800" height="453"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step Two - Setup Vault
&lt;/h2&gt;

&lt;p&gt;Whenever you are dealing with Ansible and you have API tokens, passwords, secret keys and access keys or any secured template, make sure you put them in your &lt;code&gt;ansible-vault&lt;/code&gt; and protect them with your password. &lt;/p&gt;

&lt;p&gt;Create a password for vault:&lt;br&gt;
&lt;code&gt;openssl rand -base64 2048 &amp;gt; vault.pass&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Ensure you have configured your AWS User credential (secret key and access key) that have AWSEC2FullAccess as the role&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Add your AWS credentials using the below vault command&lt;br&gt;
&lt;code&gt;ansible-vault create group_vars/all/pass.yml --vault-password-file vault.pass&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo872be3daxu1ztdhrs19.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo872be3daxu1ztdhrs19.png" alt="vault-pass" width="800" height="56"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step Three - Write Ansible Playbook
&lt;/h2&gt;

&lt;p&gt;Ensure you are in the directory where you have initiated the project. create a file &lt;code&gt;ec2_creation.yml&lt;/code&gt;, copy and paste the below code:&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 EC2 Instances 
  hosts: localhost
  connection: local
  become: false

  vars:
    instance_types:
      - ami: ami-07c8c1b18ca66bb07 # Replace with your desired AMI
        instance_type: t3.micro
        count: 2
        distro: ubuntu
      - ami: ami-0249211c9916306f8 # Replace with your desired AMI
        instance_type: t3.micro
        count: 1
        distro: centos

  tasks:
    - name: Create EC2 instances
      amazon.aws.ec2_instance:
        key_name: ec2_mac_key  # Replace with your key pair name
        image_id: "{{ item.ami }}"
        instance_type: "{{ item.instance_type }}"
        region: eu-north-1
        count: "{{ item.count }}"  # Specify count directly
        ebs_optimized: true
        tags:
          Name: "{{ item.distro }}-server"
      loop: "{{ instance_types }}"
      loop_control:
        loop_var: item

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

&lt;/div&gt;



&lt;p&gt;Save and run the below command:&lt;br&gt;
&lt;code&gt;ansible-playbook ec2_creation.yml --vault-password-file vault.pass&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpzy231ee35639jpiyngd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpzy231ee35639jpiyngd.png" alt="ansible success output" width="800" height="141"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0vuzexbkahhsb34toyaa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0vuzexbkahhsb34toyaa.png" alt="AWS dashboard output" width="800" height="136"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Setup Passwordless Authentication Between Ansible Control Node And Newly Created Instances
&lt;/h2&gt;

&lt;p&gt;Using Public Key&lt;br&gt;
&lt;code&gt;ssh-copy-id -f "-o IdentityFile &amp;lt;PATH TO PEM FILE&amp;gt;" ubuntu@&amp;lt;INSTANCE-PUBLIC-IP&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ssh-copy-id&lt;/strong&gt;: This is the command used to copy your public key to a remote machine.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;-f&lt;/strong&gt;: This flag forces the copying of keys, which can be useful if you have keys already set up and want to overwrite them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"-o IdentityFile "&lt;/strong&gt;: This option specifies the identity file (private key) for the connection. The -o flag passes this option to the underlying ssh command.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ubuntu@&lt;/strong&gt;: This is the username (ubuntu) and the IP address of the remote server you want to access.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You have to do it for the three IPs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fznnntu5qucqu6wn9f2f2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fznnntu5qucqu6wn9f2f2.png" alt="ips connection" width="800" height="329"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjyiqxl3ww8otsr0g6q5g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjyiqxl3ww8otsr0g6q5g.png" alt="ssh-success" width="783" height="430"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Automate The Shutdown of Ubuntu Instances Only Using Ansible Conditionals
&lt;/h2&gt;

&lt;p&gt;Create another yml file called &lt;code&gt;ec2_shutdown.yml&lt;/code&gt; and paste the below code:&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: Shutdown ubuntu instances only
      ansible.builtin.command: /sbin/shutdown -t now
      when:
       ansible_facts['os_family'] == "Debian"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, create an inventory file called &lt;code&gt;inventory.ini&lt;/code&gt; in which all your servers' IP are listed.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy0esrhkzik1q6rgdu2bv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy0esrhkzik1q6rgdu2bv.png" alt="vscode-inventory.ini" width="800" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Run the command&lt;br&gt;
&lt;code&gt;ansible-playbook -i inventory.ini ec2_shutdown.yml --vault-password-file vault.pass&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Outputs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcltltgk18mxmxlxonjfx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcltltgk18mxmxlxonjfx.png" alt="Ansible success" width="800" height="297"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftinvy6fst18mi44bbsmm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftinvy6fst18mi44bbsmm.png" alt="AWS dashboard" width="800" height="122"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Additional Considerations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Specific Conditions&lt;/strong&gt;: You can customize the &lt;code&gt;when&lt;/code&gt; condition based on your requirements. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Graceful Shutdown&lt;/strong&gt;: Consider using &lt;code&gt;shutdown -h +5&lt;/code&gt; to allow processes to gracefully terminate before shutdown.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error Handling&lt;/strong&gt;: Implement error handling mechanisms to capture potential issues during shutdown.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Idempotency&lt;/strong&gt;: Ensure the playbook is idempotent by using appropriate conditional logic or idempotent modules.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Alternative Shutdown Methods&lt;/strong&gt;: Explore using the &lt;code&gt;reboot&lt;/code&gt; or &lt;code&gt;systemd&lt;/code&gt;modules if needed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example with Multiple Conditions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;when:
  - ansible_os_family == "Debian"
  - ansible_hostname in ['ubuntu_instance1', 'ubuntu_instance2']
  - ansible_uptime &amp;gt; 3600  # Shutdown after 1 hour of uptime

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion: Automating AWS Infrastructure with Confidence
&lt;/h2&gt;

&lt;p&gt;In this article, we've explored leveraging Ansible's powerful capabilities to automate key infrastructure tasks on AWS. We covered:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Provisioning EC2 instances&lt;/strong&gt;: We demonstrated efficient instance creation using Ansible loops, catering to distributions like Ubuntu and Amazon Linux.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Passwordless authentication&lt;/strong&gt;: We explored securing access from your control node to the newly created instances using public key authentication.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Conditional shutdown automation&lt;/strong&gt;: We implemented an Ansible playbook that selectively shuts down Ubuntu instances based on the operating system family.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By combining these techniques, you can significantly streamline your AWS infrastructure management. Customize the playbooks further to match your specific requirements and security protocols.&lt;/p&gt;

&lt;p&gt;Ansible offers a vast range of modules and functionalities for managing your infrastructure.  As you delve deeper, you can explore advanced features like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Inventory management&lt;/strong&gt;: Enhance inventory management to dynamically discover and target your AWS resources.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Configuration management&lt;/strong&gt;: Automate server configuration and application deployment to ensure consistency across your infrastructure.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cloud-specific modules&lt;/strong&gt;: Utilize Ansible's extensive support for managing various cloud platforms like AWS, Azure, and GCP.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By embracing Ansible's automation capabilities, you can free yourself from repetitive tasks and focus on building and scaling your cloud infrastructure with greater efficiency and control.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>ansible</category>
      <category>aws</category>
      <category>cloud</category>
    </item>
  </channel>
</rss>
