Skip to main content

Ansible with Azure

 

Problem statement

Create a Virtual Machine with the following configurations by using Ansible Script.

Region: (US) East US
Availability options: No infrastructure redundancy required
Security type: Standard
Image: Ubuntu Server 20.04 LTS - Gen2
Size: Standard_B2ms
Authentication type: SSH public key
SSH public key source: Generate new key pair
OS disk type: Standard HDD
Security Group:

  • Inbound rule: Allow SSH, HTTP, and HTTPS
  • Outbound rule: Allow All

Notes
Launch Both Azure Basics Lab and Ubuntu Challenge Lab in new tabs.
You must click on the Credentials button on the top right corner of Ubuntu Challenge Lab to allow typing within the environment.
Utilize Ubuntu Challenge Lab to perform an Ansible Script.
Ansible and Python3 are already Installed in the Playground. Install rest of the required dependencies for ansible.
Use the credentials given in the Azure Basics Lab to log in to the Azure Portal.
After completing the hands-on, delete all the resources created.
Once you complete the challenge click on the Power button on the top right corner to terminate the playground.

Ansible playbook that creates a Virtual Machine in Azure with the given configurations:
create 'create_vm.yml' and write the below code


---
- name: Create Azure Virtual Machine
  hosts: localhost
  gather_facts: no
  tasks:
    - name: Create Resource Group
      azure_rm_resourcegroup:
        name: myResourceGroup
        location: eastus
        state: present

    - name: Create Virtual Network
      azure_rm_virtualnetwork:
        name: myVNet
        resource_group: myResourceGroup
        location: eastus
        address_prefixes: "10.0.0.0/16"
        state: present

    - name: Create Subnet
      azure_rm_subnet:
        name: mySubnet
        resource_group: myResourceGroup
        virtual_network_name: myVNet
        address_prefix: "10.0.0.0/24"
        state: present

    - name: Create Public IP
      azure_rm_publicipaddress:
        name: myPublicIP
        resource_group: myResourceGroup
        location: eastus
        allocation_method: Static
        state: present

    - name: Create Network Security Group
      azure_rm_securitygroup:
        name: myNSG
        resource_group: myResourceGroup
        location: eastus
        security_rules:
          - name: AllowSSH
            protocol: Tcp
            destination_port_range: "22"
            access: Allow
            direction: Inbound
          - name: AllowHTTP
            protocol: Tcp
            destination_port_range: "80"
            access: Allow
            direction: Inbound
          - name: AllowHTTPS
            protocol: Tcp
            destination_port_range: "443"
            access: Allow
            direction: Inbound
        state: present

    - name: Create Virtual Network Interface
      azure_rm_networkinterface:
        name: myNIC
        resource_group: myResourceGroup
        location: eastus
        subnet_name: mySubnet
        public_ip_name: myPublicIP
        security_group_name: myNSG
        state: present

    - name: Generate SSH Key
      ansible.builtin.ssh_keypair:
        path: "{{ playbook_dir }}/id_rsa"
        size: 2048

    - name: Create Virtual Machine
      azure_rm_virtualmachine:
        name: myVM
        resource_group: myResourceGroup
        location: eastus
        vm_size: Standard_B2ms
        admin_username: myAdmin
        ssh_password_enabled: no
        ssh_public_keys:
          - path: /home/myAdmin/.ssh/authorized_keys
            key_data: "{{ lookup('file', playbook_dir + '/id_rsa.pub') }}"
        os_type: Linux
        image:
          offer: UbuntuServer
          publisher: Canonical
          sku: 20.04-LTS
          version: latest
        os_disk_caching: ReadOnly
        data_disks: []
        network_interfaces: [myNIC]
        state: present

Comments

Popular posts from this blog

Kubernetes2

  Challenge 2 ConfigMaps: Step-1: To create a ConfigMap named 'fresco-config' with the key-value pair SERVER_URL= https://www.fresco.me , use the following command: kubectl create configmap fresco-config --from-literal=SERVER_URL=https://www.fresco.me To verify if the ConfigMap is created, use the following command: kubectl get configmap fresco-config Step-2: To create an nginx pod with the environmental variable SERVER_URL_ENV using the ConfigMap created earlier, use the following YAML template: apiVersion: v1 kind: Pod metadata: name: fresco-nginx-pod spec: containers: - name: fresco-nginx-container image: nginx env: - name: SERVER_URL_ENV valueFrom: configMapKeyRef: name: fresco-config key: SERVER_URL Deploy the above file you can name it deployment.yaml and use the command: kubectl apply -f deployment.yaml To test your configuration by executing the following command:

Kubernetes1

  Challenge 1 Welcome to the Kubernetes challenge, your task is to follow the below steps and complete them successfully. Environment Setup Check whether docker & minikube are properly installed and configured. Start Minikube and execute this command to sync host docker with minikube docker minikube -p minikube docker-env and eval $(minikube docker-env) Step-1 Create a pod object using kubectl run command with google's sample image: gcr.io/google-samples/kubernetes-bootcamp:v1 and expose it on port 8080, name the pod as firstapp. Check if the pod creation is successful by running the command: kubectl get pod firstapp Step-2 Expose the application to the local VM by creating a Service object of type NodePort. Check if the service is created by running the command: kubectl get svc firstapp Step-3 Create another deployment using a 'YAML' file, create a deployment.yaml file that contains information of the number of replicas and the images to be

Docker + Docker Compose + Ansible

 Docker + Docker Compose + Ansible ----------------------------------------------------------------------------------------------------------------------------- Flask Application Deployment using Ansible roles and Docker-Compose 1. Run the "setup.sh" file given in the path to install the required dependencies before starting the challenge. 2. A Flask application named "app.py" is given in the path . 3. Create an Ansible role named "Installation" in the path "/etc/ansible/roles" to install docker-compose. 4. Write an Ansible playbook named "creation.yaml" in the path , to perform the following tasks: 1. Using "file" and "copy" modules, create a Multi-stage "Dockerfile" in the path to  - Dockerize the given Flask application with "python:alpine3.7" as its base image, using the given 'requirements.txt' file.  - Build an image using "postgres" as its base image. 2. Using &q