Day-7-Ansible

Mastering Ansible: A Comprehensive Guide to Provisioning and Configuration Management

TLDR: In this blog post, we explore a practical Ansible project that covers provisioning EC2 instances on AWS, setting up passwordless authentication, and automating the shutdown of instances. We delve into Ansible loops and conditionals, providing a step-by-step guide to successfully complete the tasks, making it ideal for beginners and interview preparation.

Welcome to the seventh episode of the Ansible Zero to Hero series. In this post, we will tackle a real-time Ansible project that was presented as an assignment during an interview. This project will help you understand how to use Ansible for provisioning and configuration management, focusing on loops and conditionals.

Overview of the Project

The project consists of three main tasks:

  1. Provisioning EC2 Instances: Create three EC2 instances on AWS, using Ansible loops.

  2. Setting Up Passwordless Authentication: Configure passwordless SSH access to the created instances.

  3. Automating Instance Shutdown: Write an Ansible playbook to shut down the Ubuntu instances automatically.

Task 1: Provisioning EC2 Instances

In this task, we will create three EC2 instances:

  • Two instances will run Ubuntu.

  • One instance will run Amazon Linux.

To achieve this, we will use Ansible loops to simplify the process. The control node will be our laptop, which has Ansible installed. We will use the Ansible AWS collection to interact with AWS APIs, as direct SSH connections are not possible with AWS.

Steps to Provision EC2 Instances

  1. Create an IAM User: We need an IAM user with permissions to create EC2 instances. In the AWS console, navigate to the IAM service and create a user with EC2 full access.

  2. Install Required Packages: Ensure you have Ansible and the Boto3 library installed. You can install Boto3 using the command:

     pip install boto3
    
  3. Create the Playbook: Create a YAML file named ec2_create.yaml and define the tasks to provision the instances. Use the following structure:

     ---
     - hosts: localhost
       connection: local
       tasks:
         - name: Create EC2 instances
           ec2:
             key_name: your_key_name
             instance_type: t2.micro
             image: '{{ item.image }}'
             region: ap-south-1
             wait: yes
             count: 1
             tags:
               Name: '{{ item.name }}'
           loop:
             - { name: 'AnsibleInstance1', image: 'ami-xxxxxxxx' }
             - { name: 'AnsibleInstance2', image: 'ami-yyyyyyyy' }
             - { name: 'AnsibleInstance3', image: 'ami-zzzzzzzz' }
    
  4. Run the Playbook: Execute the playbook using the command:

     ansible-playbook ec2_create.yaml
    

Task 2: Setting Up Passwordless Authentication

After provisioning the instances, we need to set up passwordless SSH access. This can be done using the ssh-copy-id command, which allows you to copy your public key to the remote instances.

Steps to Set Up Passwordless Authentication

  1. Use SSH Copy ID: Run the following command for each instance:

     ssh-copy-id -i path_to_your_pem_file.pem ec2-user@instance_ip
    
  2. Verify Access: Test the passwordless authentication by SSHing into the instances without a password:

     ssh ec2-user@instance_ip
    

Task 3: Automating Instance Shutdown

The final task is to automate the shutdown of the Ubuntu instances. We will write an Ansible playbook that checks the OS family and shuts down only the Ubuntu instances.

Steps to Automate Shutdown

  1. Create the Inventory File: Create an inventory file listing the IP addresses of the instances.

  2. Write the Playbook: Create a YAML file named ec2_stop.yaml with the following structure:

     ---
     - hosts: all
       become: true
       tasks:
         - name: Shutdown Ubuntu instances
           command: /sbin/shutdown -h now
           when: ansible_facts['os_family'] == 'Debian'
    
  3. Run the Playbook: Execute the shutdown playbook using:

     ansible-playbook -i inventory_file ec2_stop.yaml
    

Conclusion

In this blog post, we covered a comprehensive Ansible project that included provisioning EC2 instances, setting up passwordless authentication, and automating instance shutdown. By utilizing Ansible loops and conditionals, we streamlined the process and ensured efficient management of our AWS resources. This project not only enhances your Ansible skills but also prepares you for real-world scenarios and interviews. Thank you for following along, and feel free to reach out with any questions!