Showing posts with label Playbooks. Show all posts
Showing posts with label Playbooks. Show all posts

Saturday, February 10, 2024

How to setup Jenkins on Ubuntu using Ansible Role | Setup Java, Jenkins, Maven on Ubuntu EC2 using Ansible Role

Here below are the Ansible Roles for installing Java, Jenkins, Maven on Ubuntu EC2 instance using Ansible. You need to install Java first (first link below) and then do the steps in the second link for installing Jenkins, third link for installing Maven.

Click here if you would like to create a new Ubuntu EC2 instance using Ansible Playbook.

You can watch this lab on YouTube:

Tuesday, January 23, 2024

Create Ansible Role to create a new EC2 instance | Ansible Role for provisioning infrastructure in AWS | Refactor Ansible playbook into Ansible Role

We will learn how to create Ansible Role for provisioning a new EC2 instance in AWS cloud. We will pick a playbook which has all the logic and we will refactor into reusable ansible role.


What is Ansible Role?
Ansible also lets you organize tasks in a directory structure called a Role. Using Ansible roles you can break down complex playbooks into smaller and manageable chunks. Ansible role enables reuse and share our Ansible code efficiently.

How to create Ansible Role?

Using ansible galaxy command, we can create Ansible role. This will create the below directory with all the files. 

directory structure of Ansible role
aws-infra-role/
├── README.md
├── create.yml
├── defaults
│   └── main.yml
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── tasks
│   ├── create-ec2.yml
│   └── create-sg.yml
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

Directory structure explained
tasks - contains the main list of tasks to be executed by the role.
handlers - handlers are typically used to start, reload, restart, and stop services.
defaults - default variables for the role.
vars - other variables for the role. Vars has the higher priority than defaults.
meta - defines some data / information about this role (author, dependency, versions, examples, etc,.)

tests - test cases if you have any.

Pre-requisites:
Steps to create EC2 instance using Ansible Role:

Login to EC2 instance using Git bash or ITerm/putty where you installed Ansible. Execute the below command:

Create an Inventory file first

sudo mkdir /etc/ansible

Edit Ansible hosts or inventory file
sudo vi /etc/ansible/hosts

Add the below two lines in the end of the file:
[localhost]
local


cd ~
mkdir roles  
cd roles

Create Ansible Role

ansible-galaxy role init aws-infra-role


We will convert this playbook into ansible role.
So all the variables will go inside vars folder.

vars
    └── main.yml

sudo vi aws-infra-role/vars/main.yml
(copy below content)
keypair: myNov2023Key
instance_type: t2.small
image: ami-007855ac798b5175e
wait: yes
group: webserver
region: us-east-1
security_group: my-jenkins-security-grp1

Save the file and come out of it.

So all the tasks will go inside tasks folder. let's create security group first.

sudo vi aws-infra-role/tasks/create-sg.yml
---
  - include_vars: "vars/main.yml"
    tags: create

# tasks file for security group
  - name: configuring security group for the instance
    ec2_group:
        name: "{{ security_group }}"
        description: my-ajenkin-security_groAup
        region: "{{ region }}"
        rules:
            - proto: tcp
              from_port: 22
              to_port: 22
              cidr_ip: 0.0.0.0/0
            - proto: tcp
              from_port: 80
              to_port: 80
              cidr_ip: 0.0.0.0/0
            - proto: tcp
              from_port: 8080
              to_port: 8080
              cidr_ip: 0.0.0.0/0
        rules_egress:
            - proto: all
              cidr_ip: 0.0.0.0/0

Let's create a task for ec2 instance creation.

sudo vi aws-infra-role/tasks/create-ec2.yml

---
  - include_vars: "vars/main.yml"
    tags: create
  - name: creating ec2 instance
    ec2_instance:
        security_group: "{{ security_group }}"
        name: test-stan
        key_name: "{{ keypair }}"
        instance_type: "{{ instance_type}}"
        image_id: "{{ image }}"
        region: "{{ region }}"
        wait_timeout: 2   


Let's create a task for creating s3 bucket.

sudo vi aws-infra-role/tasks/create-s3.yml                                                                                                     ---
  - include_vars: "vars/main.yml"
    tags: create
  - name: creating s3

    s3_bucket:
      name: myansibles3bucket1234
      state: present
      region: "{{ region }}"
      versioning: yes
      tags:
        name: myansiblebucket
        type: example
    register: s3_url

  - name: Display s3 url
    debug: var=s3_url       

Let's create Ansible main playbook.
sudo vi aws-infra-role/main.yml
---
# This Playbook creates infra in aws cloud

- hosts: local
  connection: local
  gather_facts: False
  tags: ec2_create

  tasks:
  - include: tasks/create-sg.yml
  - include: tasks/create-ec2.yml
  - include: tasks/create-s3.yml 

now execute the ansible playbook by
ansible-playbook aws-infra-role/main.yml


If everything is good, you should see the new instance, S3 bucket created on AWS console. make sure you are able to connect to that instance.

That's it!! That is how you create a new EC2 instance using Ansible role in AWS cloud. 
Please watch steps in YouTube channel:

Tuesday, March 7, 2023

Ansible playbook for AWS S3 bucket creation | How to create S3 bucket using Ansible in AWS Cloud

We will learn how to create new S3 bucket using Ansible playbook and automate the execution using Jenkins Pipeline. 


Pre-requisites:


  • Playbook for creating new S3 bucket needs to be created but you can refer my GitHub Repo

Ansible playbook for AWS S3 bucket creation

Steps:

1. Create Ansible playbook for S3 bucket creation

(Sample playbook is available in my GitHub Repo, you can use that as a reference)

2. Create Jenkins Pipeline 

pipeline {
    agent any
    stages {
        
        stage ("checkout") {
            steps {
                        checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [],                                                     userRemoteConfigs: [[url: 'https://github.com/akannan1087/myAnsibleInfraRepo']]])         
            }
        }
        stage('execute') {
            steps {
                //to suppress warnings when you execute playbook    
                sh "pip install --upgrade requests==2.20.1"
                // execute ansible playbook
                ansiblePlaybook 'create-s3.yml'
            }
        }
    }
}

Execute Pipeline


Pipeline Console output


Playbook for creating S3 for your reference:

create-s3.yml

---
 - name:  provisioning S3 Bucket using Ansible playbook
   hosts: localhost
   connection: local
   gather_facts: False
   tags: provisioning

   tasks:
     - name: create S3 bucket
       s3_bucket:
         name: myansibles3bucket312
         state: present
         region: us-east-1
         versioning: yes
         tags:
           name: myansiblebucket
           type: example
       register: s3_url

     - name: Display s3 url
       debug: var=s3_url

Wednesday, January 4, 2023

How to run Ansible playbook from Jenkins pipeline job | Automate EC2 provisioning in AWS using Jenkins and Ansible Playbook | Create new EC2 instance in AWS cloud using Ansible Playbook and Jenkins Pipeline

We will learn how to create new EC2 instance using Ansible playbook and automate using Jenkins Pipeline. 


Watch Steps in YouTube Channel:

Pre-requisites:

  • Ansible is installed and Boto is also installed on Jenkins instance
  • Ansible plug-in is installed in Jenkins. 
  • Make sure you create an IAM role with AmazonEC2FullAccess policy and attach the role to Jenkins EC2 instance.
  • Playbook for creating new EC2 instance needs to be created but you can refer my GitHub Repo
Steps:

Create Ansible playbook for provisioning EC2 instance

(Sample playbook is available in my GitHub Repo, you can use that as a reference)

Create Jenkins Pipeline 
pipeline {
    agent any

    stages {
        
        stage ("checkout") {
            steps {
                        checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [],                                                     userRemoteConfigs: [[url: 'https://github.com/akannan1087/myAnsibleInfraRepo']]])         
            }
        }
        stage('execute') {
            steps {
                //to suppress warnings when you execute playbook    
                sh "pip install --upgrade requests==2.20.1"
                // execute ansible playbook
                ansiblePlaybook playbook: 'create-EC2.yml'
            }
        }
    }
}

Execute Pipeline


Pipeline Console output




Tuesday, December 27, 2022

Ansible playbook for Tomcat Installation on Ubuntu 18.0.4/20.0.4 | Ansible Tomcat Playbook on Ubuntu 18.0.4/20.0.4

Ansible Playbook for installing Tomcat 9 on Ubuntu 18.0.4

sudo vi installTomcat.yml
---
- hosts: My_Group
  tasks:
    - name: Task # 1 Update APT package manager repositories cache
      become: true
      apt:
        update_cache: yes
    - name: Task # 2 - Install Tomcat using Ansible
      become: yes
      apt:
        name: "{{ packages }}"
        state: present
      vars:
        packages:
           - tomcat9
           - tomcat9-examples
           - tomcat9-docs

sudo ansible-playbook installTomcat.yml
This is the execution result of Ansible playbook.


Now access Tomcat on port 8080 in the target machine where you have installed it.



Wednesday, May 13, 2020

Ansible playbook for LAMP Installation on Ubuntu | Install LAMP stack using Ansible on Ubuntu 22.0.4

LAMP Stack comprises the following open-source software applications.

    • Linux – This is the operating system hosting the Applications.
    • Apache – Apache HTTP is a free and open-source cross-platform web server.
    • MySQL– Open Source relational database management system.
    • PHP – Programming/Scripting Language used for developing Web applications.
    Watch the steps in YouTube Channel:

    Pre-requisites:
    Steps to setup SSH keys:
    1. Login to Ansible management server/machine. Create SSH keys in Ansible host machine by executing the below command: (if you already have keys created, please skip this step)
    ssh-keygen 

    enter three times..now you will see keys successfully created.
    2.  Execute the below command on Ansible management node and copy the public key content:
    sudo cat ~/.ssh/id_rsa.pub

    copy the above output.
    3. Now login into target node where you want to install LAMP stack, execute the below command to open the file
    sudo vi /home/ubuntu/.ssh/authorized_keys
    type shift A and then enter now 
        and paste the key in the above file. please do not delete any existing values in this file.

    4. Now go back to Ansible mgmt node, do changes in /etc/ansible/hosts file to include the node you will be installing software. Make sure you add public or private IP address of target node as highlighted below in red color:
    sudo vi /etc/ansible/hosts
    [My_Group]  
    xx.xx.xx.xx ansible_ssh_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa  ansible_python_interpreter=/usr/bin/python3

    Ansible playbook for installing LAMP(Linux Apache MySQL PHP) stack on Ubuntu

    sudo vi installLAMP.yml
    ---
    - hosts: My_Group
      tasks:
        - name: Task # 1 - Update APT package manager repositories cache
          become: true
          apt:
            update_cache: yes
        - name: Task # 2 - Install LAMP stack using Ansible
          become: yes
          apt:
            name: "{{ packages }}"
            state: present
          vars:
            packages:
               - apache2
               - mysql-server
               - php

    ansible-playbook installLAMP.yml


    This is the execution result of the playbook.

    Now go to browser and use target node DNS to confirm if Apache is installed. make sure port 80 is opened in security firewall rules.


    Now login to target EC2 instance, type below commands to verify PHP and MySql versions:

    php --version

    mysql --version

    Install Tomcat using Ansible playbook on Ubuntu - Install Tomcat on Ubuntu using Ansible playbook

    Playbook for installing Tomcat 9 on Ubuntu using Ansible Playbook

    sudo vi installTomcat.yml
    ---
    - hosts: My_Group
      tasks:
        - name: Install Tomcat 9 on Ubuntu
          become: yes
          apt: pkg={{ item }} state=latest update_cache=yes cache_valid_time=3600
          with_items:
            - tomcat9

    sudo ansible-playbook installTomcat.yml


    This is the execution result of Ansible playbook.

    Sunday, April 26, 2020

    Ansible playbook for Java 11 Installation on Ubuntu - Ansible Java 11 Playbook Ubuntu 22.0.4

    1. Login to Ansible management server/machine. Create SSH keys in Ansible host machine by executing the below command: (if you already have keys created, please skip this step)
    ssh-keygen 

    enter three times..now you will see keys successfully created.
    2.  Execute the below command on Ansible management node and copy the public key content:
    sudo cat ~/.ssh/id_rsa.pub

    copy the above output.
    3. Now login to target node, execute the below command to open the file
    sudo vi /home/ubuntu/.ssh/authorized_keys
    type shift A and then enter now 
        and paste the key in the above file. please do not delete any existing values in this file.

    4. Now go back to Ansible mgmt node, do changes in /etc/ansible/hosts file to include the node you will be installing software. Make sure you add public IP address of target node as highlighted below in red color:
    sudo vi /etc/ansible/hosts
    [My_Group]  
    xx.xx.xx.xx ansible_ssh_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa  ansible_python_interpreter=/usr/bin/python3

    5. let's make sure Ansible Control node is able to connect to target node

    ansible -m ping all

    ansible all -a "whoami"



    6. make changes in playbooks as given below,
    cd ~/playbooks

    sudo vi installJava11.yml

    ---
    - hosts: My_Group
      tasks:
        - name: Task - 1 Update APT package manager repositories cache
          become: true
          apt:
            update_cache: yes
        - name: Task -2 Install Java using Ansible
          become: yes
          apt:
            name: "{{ packages }}"
            state: present
          vars:
            packages:
               - openjdk-11-jdk

    6. Execute Ansible playbook
    ansible-playbook installJava11.yml

    now after successfully executing, enter below command to make sure Java is installed in target node:

    java -version
    openjdk version "11.0.7" 2020-04-14
    OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-2ubuntu218.04)
    OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-2ubuntu218.04, mixed mode, sharing)

    Wednesday, April 24, 2019

    Ansible playbook to install OpenJDK 8 on Ubuntu - Install OpenJDK 8 using Ansible playbook

     Here below is ansible playbook to install Open JDK 8 on Ubuntu:

    ---
    - hosts: Java_Group

      tasks:
      - name: Update APT package manager repositories cache
        become: true
        apt:
          update_cache: yes

      - name: Install OpenJDK Java
        become: yes
        apt:
          name: "{{ item }}"
          state: present
        with_items:
         openjdk-8-jdk

    Thursday, January 24, 2019

    Install Maven using Ansible playbook on Ubuntu - Install Maven on Ubuntu using Ansible playbook

    Playbook for installing Maven on Ubuntu using Ansible Playbook

    sudo vi installMaven.yml
    ---
    - hosts: My_Group
      tasks:
        - name: Install Maven using Ansible
          become: yes
          apt:
            name: "{{ packages }}"
            state: present
          vars:
            packages:
               - maven

    ansible-playbook installMaven.yml


    This is the execution result of Ansible playbook.

    Friday, January 11, 2019

    Ansible Playbook to install Java 8 on Ubuntu - How to install Java 8 using Ansible Playbook


    Find below Ansible playbook to install Java 8 on Ubuntu

    Step 1: Create the playbook first with name. for e.g, installJava.xml

    ---
    - hosts: Java_Group

      tasks:
      - name: Update APT package manager repositories cache
        become: true
        apt:
          update_cache: yes

      - name: Install OpenJDK Java
        become: yes
        apt:
          name: "{{ item }}"
          state: present
        with_items:
         openjdk-8-jdk

    2. sudo vi /etc/ansible/hosts
    make sure you add below entry with target node IP changed (in red color).
    [Java_Group]  
    xx.xx.xx.xx ansible_ssh_user=ubuntu ansible_ssh_private_key_file=~
    /.ssh/id_rsa  ansible_python_interpreter=/usr/bin/python3

    3. sudo ansible-playbook installJava.xml

    now after successfully executing, enter below command in target node to make sure Java is installed:

    java -version

    Click here to learn how to create more playbooks in Ansible for installing Maven and Jenkins.

    Saturday, December 15, 2018

    Ansible Playbook for provisioning a new EC2 instance in AWS - Create a new EC2 Using Ansible Playbook

    Please find the Ansible Playbook for provisioning a new EC2 instance. Please follow the below steps in the machine where you installed Ansible.

    Steps to create EC2 instance using Ansible:


    1. Login to AWS console, click on username and go to My security credentials.
    2. Continue on security credentials, click on access keys
    3. Create a new access key if you dont have one. Make sure you download the keys.
    4. Login to EC2 instance using Git bash or ITerm where you installed Ansible.

    execute the below command

    sudo vi ~/.boto

    add below three lines in the above file, replace the ?? with access key and secret key values.
    [Credentials]
    aws_access_key_id = ??
    aws_secret_access_key = ??





    5. Edit Ansible hosts or inventory file
    sudo vi /etc/ansible/hosts 
    Add the below two lines in the end of the file:
    [localhost]
    local

    6. cd ~
    7. mkdir playbooks  
    8. cd playbooks

    Create Ansible playbook
    9. sudo vi create_jenkins_ec2.yml 
    (copy the below content in green color)
    edit the create_jenkins_ec2.yml to make sure you update the key which is red marked below:
    ---
     - name:  provisioning EC2 Lab Exercises using Ansible
       hosts: localhost
       connection: local
       gather_facts: False
       tags: provisioning

       vars:
         keypair: MyEC2Key
         instance_type: t2.small
         image: ami-07c1207a9d40bc3bd
         wait: yes
         group: webserver
         count: 1
         region: us-east-2
         security_group: my-jenkins-security-grp
       
       tasks:

         - name: Create my security group
           local_action: 
             module: ec2_group
             name: "{{ security_group }}"
             description: Security Group for webserver Servers
             region: "{{ region }}"
             rules:
                - proto: tcp
                  from_port: 22
                  to_port: 22
                  cidr_ip: 0.0.0.0/0
                - proto: tcp
                  from_port: 8080
                  to_port: 8080
                  cidr_ip: 0.0.0.0/0
                - proto: tcp
                  from_port: 80
                  to_port: 80
                  cidr_ip: 0.0.0.0/0
             rules_egress:
                - proto: all
                  cidr_ip: 0.0.0.0/0
           register: basic_firewall
         - name: Launch the new EC2 Instance
           local_action:  ec2 
                          group={{ security_group }} 
                          instance_type={{ instance_type}} 
                          image={{ image }} 
                          wait=true 
                          region={{ region }} 
                          keypair={{ keypair }}
                          count={{count}}
           register: ec2
         - name: Add Tagging to EC2 instance
           local_action: ec2_tag resource={{ item.id }} region={{ region }} state=present
           with_items: "{{ ec2.instances }}"
           args:
             tags:
               Name: MyTargetEc2Instance


    
    
    
    
    10. now execute the ansible playbook by
    sudo ansible-playbook create_jenkins_ec2.yml



    Fix the warnings by executing below command
    pip install --upgrade requests==2.20.1

    If everything is good, you should see the new instance created on AWS console. make sure you are able to connect to that instance.

    That's it!! That is how you create a new EC2 instance using Ansible.