/Ansible_Playbooks

This is a repository that houses ansible's playbooks to launch an instance, start an iinstance, stop an instance and terminate an instance.

Primary LanguagePython

Below are step-by-step instructions for launching, stopping, starting, and terminating AWS EC2 instances using Ansible:

  1. Launching EC2 Instances:

    Step 1: Set up your Ansible environment:

    • Make sure you have Ansible installed on your local machine.
    • Configure your AWS credentials either by setting environment variables or using AWS CLI configuration.
    • Create an inventory file (e.g., inventory.ini) with the details of the EC2 instances you want to manage.

    Step 2: Write the Ansible playbook:

    # launch_ec2_instances.yml
    
    ---
    - name: Launch EC2 Instances
      hosts: localhost
      gather_facts: no
      tasks:
        - name: Launch EC2 instances
          ec2_instance:
            count: 1
            instance_type: t2.micro
            image: {YOUR AMI} # Replace with your desired AMI ID
            key_name: {YOUR KEYPAIR}
            security_group: {YOUR SECURITY GROUP}
            wait: yes
          register: ec2_result

    Step 3: Run the playbook:

    ansible-playbook -i inventory.ini launch_ec2_instances.yml
  2. Stopping EC2 Instances:

    Step 1: Write the Ansible playbook:

    # stop_ec2_instances.yml
    
    ---
    - name: Stop EC2 Instances
      hosts: localhost
      gather_facts: no
      tasks:
        - name: Stop EC2 instances
          local_action:
            module: ec2
            args:
              instance_ids:
                - "{YOUR INSTANCE ID}"  # Replace with your instance ID
              state: stopped
          register: stopped_instances

    Step 2: Run the playbook:

    ansible-playbook -i inventory.ini stop_ec2_instances.yml
  3. Starting EC2 Instances:

    Step 1: Write the Ansible playbook:

    # start_ec2_instances.yml
    
    ---
    - name: Start EC2 Instances
      hosts: localhost
      gather_facts: no
      tasks:
        - name: Start EC2 instances
          local_action:
            module: ec2
            args:
              instance_ids:
                - "{YOUR INSTANCE ID}"  # Replace with your instance ID
              state: started
          register: started_instances

    Step 2: Run the playbook:

    ansible-playbook -i inventory.ini start_ec2_instances.yml
  4. Terminating EC2 Instances:

    Step 1: Write the Ansible playbook:

    # terminate_ec2_instances.yml
    
    ---
    - name: Terminate EC2 Instances
      hosts: localhost
      gather_facts: no
      tasks:
        - name: Terminate EC2 instances
          local_action:
            module: ec2
            args:
              instance_ids:
                - "{YOUR INSTANCE ID}"  # Replace with your instance ID
              state: absent
          register: terminated_instances

    Step 2: Run the playbook:

    ansible-playbook -i inventory.ini terminate_ec2_instances.yml

By following these steps, you'll be able to perform each action (launching, stopping, starting, and terminating) on your AWS EC2 instances using Ansible playbooks. Make sure to replace placeholder values such as AMI IDs and instance IDs with actual values from your AWS environment.