Showing posts with label Ansible. Show all posts
Showing posts with label Ansible. Show all posts

Tuesday, April 23, 2024

Automate Azure App Service setup using Ansible and Azure DevOps pipeline | How to integrate Ansible with Azure DevOps | How to Create WebApp in Azure Cloud using Ansible

Ansible is an open-source, configuration management tool that automates cloud provisioning, configuration management, and application deployments. 
Ansible Playbooks
Ansible playbooks allow you to direct Ansible to configure your environment. Playbooks are coded using YAML so as to be human-readable. 
Watch steps in YouTube channel:

Automate Azure Web App setup using Ansible and Azure pipeline




Integrate Ansible with Azure Cloud
Integrating Ansible with Microsoft Azure allows you to automate and manage your Azure infrastructure using Ansible playbooks and modules. Ansible provides a collection of Azure-specific modules that enable you to provision and configure resources in Azure.


To configure Azure credentials, you need the following information:

  • Your Azure subscription ID and tenant ID
  • The service principal application ID and secret

Pre-requisites:

Login to Azure

az login

Enter Microsoft credentials

Create Azure Service Principal

Run the following commands to create an Azure Service Principal:

az ad sp create-for-rbac --name ansible-azure-sp --role Contributor --scopes /subscriptions/<subscription_id>
Save the above output in a file as you will not be able retrieve later.
Create an Ansible playbook - create-linux-app-svc.yml

Create a simple playbook to create resource group in Azure and also a Azure App Service. Make sure you modify the name of the resource group, Azure WebApp and location below.


- hosts: localhost
connection: local
vars:
resource_group: myResourceGroup
webapp_name: myfirstAwesomeWebApp
plan_name: myAppServicePlan
location: eastus
tasks:
- name: Ensure resource group exists
azure_rm_resourcegroup:
name: myResourceGroup
location: East US
register: rg_result
- debug:
var: rg_result
- name: Create App Service on Linux with Java Runtime
azure_rm_webapp:
resource_group: "{{ resource_group }}"
name: "{{ webapp_name }}"
plan:
resource_group: "{{ resource_group }}"
name: "{{ plan_name }}"
is_linux: true
sku: S1
number_of_workers: 1
frameworks:
- name: "java"
version: "8"
settings:
java_container: tomcat
java_container_version: 9.0

Create Azure YAML build pipeline:

Login to Azure Devops --> https://dev.azure.com

Select project dashboard.

Go to Pipelines -> New pipeline --> Click on Azure Repos Git or any SCM where you have playbooks stored. Select repo, click on Starter pipeline.

Add below four pipeline variables with value received from service principal creation.

AZURE_SUBSCRIPTION_ID
AZURE_CLIENT_ID
AZURE_SECRET
AZURE_TENANT
Add below tasks:
  • Install Ansible on build agent
  • Install Ansible rm module on build agent
  • Execute Ansible playbook for creating resource group in Azure cloud.
trigger:
- main
pr: none # Disable PR triggers, can be adjusted as needed
pool:
vmImage: 'ubuntu-latest'
steps:
- script: |
# Install Ansible
pip3 install "ansible==2.9.17"
displayName: 'Install Ansible'
- script: |
# Install Ansible rm module
pip3 install ansible[azure]
displayName: 'Install Ansible rm module'
- script: |
# Run Ansible playbook to create Azure App Service
ansible-playbook create-linux-app-svc.yml
displayName: 'Run Ansible Playbook'
env:
AZURE_SUBSCRIPTION_ID: $(AZURE_SUBSCRIPTION_ID)
AZURE_CLIENT_ID: $(AZURE_CLIENT_ID)
AZURE_SECRET: $(AZURE_SECRET)
AZURE_TENANT: $(AZURE_TENANT)

Save the pipeline and run it.


Now Login to Azure cloud to see if the App Service have been created.

Clean up service principal & Resource Group

az ad sp list --display-name ansible-azure-sp --output table

az ad sp delete --id <pass_the_id>

az group delete --name myResourceGroup --yes --no-wait

Delete Resource group and App Service using Ansible playbook: delete-linux-app-svc.yml
- name: Delete Azure App Service
  hosts: localhost
  connection: local
  vars:
    resource_group: myResourceGroup
    webapp_namemyfirstAwesomeWebApp
  tasks:
  - name:
    azure_rm_webapp:
      name: "{{ webapp_name }}"
      resource_group: "{{ resource_group }}"
      state: absent

Wednesday, February 21, 2024

Ansible Role for LAMP Installation on Ubuntu | Install LAMP stack using Ansible Role 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.


    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 aws-infra-role/tasks/installLAMP.yml
    ---
        - 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


    Create Ansible main playbook

    sudo vi aws-infra-role/setup-lamp.yml
    ---
    # This Playbook installs LAMP stack

    - hosts: My_Group
      gather_facts: False
      tags: LAMP creation

      tasks:
      - include: tasks/installLAMP.yml

    Execute Ansible Role

    ansible-playbook aws-infra-role/setup-lamp.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

    Saturday, February 10, 2024

    Install Jenkins using Ansible Role on Ubuntu | Install Jenkins using Ansible Role | How to Setup Jenkins using Ansible

    Find below Ansible Role for installing Jenkins on a Ubuntu machine.

    Pre-requisites:
    Java needs to be installed already on machine before setting up Jenkins. please Click here for Java Playbook. Here below is the Ansible Role for installing Jenkins using Ansible in Ubuntu EC2:

    cd ~/roles
    sudo vi aws-infra-role/tasks/installJenkins.yml

    Copy the below yellow highlighted in the above file: 

    ---
        - name: ensure the jenkins apt repository key is installed
          apt_key: url=https://pkg.jenkins.io/debian-stable/jenkins.io-2026.key state=present
          become: yes

        - name: ensure the repository is configured
          apt_repository: repo='deb https://pkg.jenkins.io/debian-stable binary/' state=present
          become: yes

        - name: ensure jenkins is installed
          apt: name=jenkins update_cache=yes
          become: yes

        - name: ensure jenkins is running
          service: name=jenkins state=started











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




    Modify Ansible main playbook

    sudo vi aws-infra-role/setup-jenkins.yml
    ---
    # This Playbook creates infra in aws cloud

    - hosts: My_Group
      gather_facts: False
      tags: jenkins creation

      tasks:
      - include: tasks/installJava11.yml
      - include: tasks/installMaven.yml
      - include: tasks/installJenkins.yml

    Execute Ansible Role
    ansible-playbook aws-infra-role/setup-jenkins.yml

    This should install Jenkins on the target node.

    Now enter public ip address or public dns name with port no 8080 of target server by in the browser to see Jenkins up and running.

    http://target_node_public_dns_name:8080

    Install Maven using Ansible Role on Ubuntu - Install Maven on Ubuntu using Ansible Role

    Ansible Role for installing Maven on Ubuntu

    cd ~/roles

    sudo vi aws-infra-role/tasks/installMaven.yml

        - name: Install Maven using Ansible
          become: yes
          apt:
            name: "{{ packages }}"
            state: present
          vars:
            packages:
               - maven

    Modify Ansible main playbook

    sudo vi aws-infra-role/setup-jenkins.yml
    ---
    # This Playbook creates infra in aws cloud

    - hosts: My_Group
      gather_facts: False
      tags: jenkins creation

      tasks:
      - include: tasks/installJava11.yml
      - include: tasks/installMaven.yml

    Execute Ansible Role
    ansible-playbook aws-infra-role/setup-jenkins.yml

    This should install Maven on the  target node.

    Ansible Role for Java 17 Installation on Ubuntu - Ansible Role to Install Java on 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 Ansible Role as given below,
    cd ~/roles

    sudo vi aws-infra-role/tasks/installJava17.yml

    ---
        - 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-17-jdk

    7. Create Ansible main playbook

    sudo vi aws-infra-role/setup-jenkins.yml
    ---
    # This Playbook creates infra in aws cloud

    - hosts: My_Group
      gather_facts: False
      tags: jenkins creation

      tasks:
      - include: tasks/installJava17.yml

    8. Execute Ansible Role
    ansible-playbook aws-infra-role/setup-jenkins.yml

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

    java -version
    openjdk version "17.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)

    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: target-ec2-instance
            key_name: "{{ keypair }}"
            instance_type: "{{ instance_type}}"
            image_id: "{{ image }}"
            region: "{{ region }}"
            wait_timeout: 2   

    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

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


    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 role in AWS cloud. 
    Please watch steps in YouTube channel:

    Monday, January 22, 2024

    Errors during EC2 instance creating using Ansible playbook | Ansible playbook Execution Error during EC2 creation

    No handler was ready to authenticate. 1 handlers were checked. ['HmacAuthV4Handler'] Check your credentials" 

    If you are running into above issue when provisioning EC2 instance using Ansible playbook, please refer the fix below:


    Fix:

    Please refer the below page for the resolution with playbook.

    https://www.coachdevops.com/2024/01/ansible-playbook-for-provisioning-new.html


    Thursday, January 18, 2024

    Ansible Playbook for provisioning a new EC2 in AWS | Create new EC2 instance in AWS cloud using Ansible Playbook

    We will learn how to create a simple Ansible Playbook for provisioning a new EC2 instance in AWS cloud. Please follow the below steps in the machine where you have installed Ansible.

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

    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 playbooks  
    cd playbooks

    Create Ansible playbook
    sudo vi create-ec2.yml 
    (copy the below content in green color)
    edit the create-ec2.yml to make sure you update the key, AMI and region code which is red marked below:

    - name: Ansible ec2 launch
      hosts: localhost
      connection: local
      gather_facts: False
      tags: provisioning

      vars:
        keypair: myNov2023Key
        instance_type: t2.small
        instance_name: test-ec2-instance
        image: ami-007855ac798b5175e
        wait: yes
        group: webserver
        region: us-east-1
        security_group: my-jenkins-security-grp1

      tasks:
      - name: configuring security group for the instance
        ec2_group:
            name: "{{ security_group }}"
            description: my-jenkins-security_group
            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
      - name: creating ec2 instance
        ec2_instance:
            security_group: "{{ security_group }}"
            name: "{{ instance_name }}"
            key_name: "{{ keypair }}"
            instance_type: "{{ instance_type}}"
            image_id: "{{ image }}"
            region: "{{ region }}"
            wait_timeout: 3

    now execute the ansible playbook by
    ansible-playbook create_ec2.yml




    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. 

    Watch steps in YouTube channel:

    🚀 Live AI-Enabled DevSecOps & Cloud Engineering Bootcamp – Sep 2026

    Live AI-Enabled DevSecOps & Cloud Engineering Bootcamp from Coach AK - Sep 2026 Schedule