Table of Contents

Lab1: Creating file on destination servers using ansible playbook

amaster@Rushi-Infotech:/opt$ sudo nano file.yml
---
- name: Create a file using Ansible
  hosts: client
  become: true
  tasks:
    - name: Create a file
      file:
        path: /opt/myfile.txt
        state: touch

Run the above playbook by using below commands

amaster@Rushi-Infotech:~$ ansible-playbook file.yml

Lab2: Creating Directory on Destination servers

amaster@Rushi-Infotech:~$ sudo nano directory.yml
---
- name: Create a Directory using Ansible
  hosts: client
  become: true
  tasks:
    - name: Create a Directory
      file:
        path: /opt/Devops
        state: directory
amaster@Rushi-Infotech:~$ ansible-playbook directory.yml

Lab3: Create a directory with specific owner and group permissions using Ansible

amaster@Rushi-Infotech:~$ nano directory2.yml
---
- name: Create a Directory using Ansible
  hosts: client
  become: true
  tasks:
    - name: Create a Directory
      file:
        path: /opt/Devops2
        state: directory
        owner: client1
        group: client1
        mode: 0755
amaster@Rushi-Infotech:~$ ansible-playbook directory2.yml

Lab4: To create multiple directories using Ansible, you can utilize the file module along with a loop.

amaster@Rushi-Infotech:~$ ansible-playbook multiple-directory.yml
#SYNTAX
---
- name: Create Multiple Directories using Ansible
  hosts: target_hosts
  become: yes  # To ensure we have elevated permissions
  tasks:
    - name: Create directories
      file:
        path: "{{ item }}"
        state: directory
        mode: '0755'
      loop:
        - /path/to/directory1
        - /path/to/directory2
        - /path/to/directory3
        # ... add more directories as needed

#Live Example

---
- name: Create Multiple Directories using Ansible
  hosts: client
  become: yes  
  tasks:
    - name: Create directories
      file:
        path: "{{ item }}"
        state: directory
        mode: '0755'
      loop:
        - /opt/directory1
        - /opt/directory2
        - /opt/directory3
        # ... add more directories as needed

The below output shows we have successfully created multiple directory on destination servers

Lab5: Create user in Destination server using ansible playbook

amaster@Rushi-Infotech:~$ sudo nano user.yml
#Syntax
---
- name: Create a User on Destination Server
  hosts: target_hosts
  become: yes  # Use elevated permissions to create a user
  tasks:
    - name: Create user 'newuser'
      user:
        name: johndoe
        password: "{{ 'password_here' | password_hash('sha512') }}"
        shell: /bin/bash
        home: /home/newuser
        createhome: yes
        state: present

---
- name: Create a User on Destination Server
  hosts: client
  become: yes  # Use elevated permissions to create a user
  tasks:
    - name: Create user 'rushi'
      user:
        name: rushi
        password: "{{ 'Test@123' | password_hash('sha512') }}"
        shell: /bin/bash
        home: /home/rushi
        createhome: yes
        state: present
amaster@Rushi-Infotech:~$ ansible-playbook user.yml

Example6: Delete a user from a destination server using Ansible, you can use the user module with the state: absent parameter

---
- name: Delete a User from Destination Server
  hosts: target_hosts
  become: yes  # Use elevated permissions to delete a user
  tasks:
    - name: Remove user 'rushi'
      user:
        name: rushi
        state: absent
        remove: yes  # Optional: This removes the user's home directory and mail spool

Lab 8: Creating a file with specific content on destination servers using ansible playbook

amaster@Rushi-Infotech:~$ sudo nano copycontent.yml
---
- name: Copy Content to a File
  hosts: client
  become: true
  tasks:
    - name: Add content to a file
      copy:
        content: |
          This is line 1 of my content.
          This is line 2.
        dest: /opt/destination_file.txt
amaster@Rushi-Infotech:~$ ansible-playbook copycontent.yml

Lab9: Copying a file from the control node to the target hosts:

If instead you wanted to copy a file from your Ansible control node to the target hosts, you would use the following:

amaster@Rushi-Infotech:~$ sudo nano copy.yml
---
- name: Copy a File to the Target Hosts
  hosts: client
  become: true
  tasks:
    - name: Copy file from control node to target hosts
      copy:
        src: /opt/apache2.yml
        dest: /opt/apache2.yml
amaster@Rushi-Infotech:~$ ansible-playbook copy.yml

Lab10: How to use the replace module to replace the word “oldword” with “newword” in a specific file

amaster@Rushi-Infotech:~$ sudo nano replace.yml
#Syntax
---
- name: Replace Word in File Using Ansible
  hosts: target_hosts
  tasks:
    - name: Replace "oldword" with "newword" in a file
      replace:
        path: /path/to/your/file.txt
        regexp: 'oldword'
        replace: 'newword'
#Example for Replace module
---
- name: Replace Word in File Using Ansible
  hosts: client
  tasks:
    - name: Replace "oldword" with "newword" in a file
      replace:
        path: /opt/newdata
        regexp: 'hi'
        replace: 'hello'
amaster@Rushi-Infotech:~$ ansible-playbook replace.yml

Lab11: To create a zip archive of files or directories using Ansible, you would typically use the archive module. Here’s an example playbook that demonstrates how to use this module to archive files into a .zip format:

#Syntax:
---
- name: Archive Files into Zip Format
  hosts: target_hosts
  tasks:
    - name: Archive /path/to/source into /path/to/destination.zip
      ansible.builtin.archive:
        path: /path/to/source_directory_or_file
        dest: /path/to/destination.zip
        format: zip
#Syntax:
---
- name: Archive Files into Zip Format
  hosts: client
  tasks:
    - name: Archive /path/to/source into /path/to/destination.zip
      ansible.builtin.archive:
        path: /opt/file.txt
        dest: /opt/apache2.yml.zip
        format: zip

Lab12: If you need to create a zip archive containing multiple files using Ansible, you can still utilize the archive module. When specifying the path parameter, you can provide a list of files instead of just one file or directory.

Here’s an example playbook that demonstrates how to use the archive module to archive multiple files into a .zip format:

---
- name: Archive Multiple Files into Zip Format
  hosts: target_hosts
  tasks:
    - name: Archive multiple files into /path/to/destination.zip
      ansible.builtin.archive:
        path:
          - /path/to/file1.txt
          - /path/to/folder
          - /path/to/file3.jpg
        dest: /path/to/destination.zip
        format: zip

Lab13: Playbook to install basic packages on destination servers

To install the git package on a target host using Ansible, you’ll likely use the package management module that corresponds to the package management system of the OS you’re targeting. For instance:

  • apt for Debian/Ubuntu systems
  • yum for older Red Hat/CentOS systems
  • dnf for newer Red Hat/CentOS systems
  • pacman for Arch Linux
  • etc.

Here are some examples for various systems:

For Ubuntu/Debian:

---
- name: Install Git on Ubuntu/Debian
  hosts: target_hosts
  become: yes  # to run tasks as sudo
  tasks:
    - name: Install Git
      apt:
        name: git
        state: present

For older Red Hat/CentOS:

---
- name: Install Git on Red Hat/CentOS
  hosts: target_hosts
  become: yes
  tasks:
    - name: Install Git
      yum:
        name: git
        state: present

For newer Red Hat/CentOS:

---
- name: Install Git on Red Hat/CentOS
  hosts: target_hosts
  become: yes
  tasks:
    - name: Install Git
      dnf:
        name: git
        state: present

Lab14: To install multiple packages using Ansible, you can provide a list of packages to the appropriate package management module for your system.

For Ubuntu/Debian (using apt module):

---
- name: Install Multiple Packages on Ubuntu/Debian
  hosts: target_hosts
  become: yes
  tasks:
    - name: Install packages
      apt:
        name:
          - package1
          - package2
          - package3
        state: present
---
- name: Install Multiple Packages on Ubuntu/Debian
  hosts: client
  become: yes
  tasks:
    - name: Install packages
      apt:
        name:
          - git
          - maven
          - zip
        state: present

For older Red Hat/CentOS (using yum module):

---
- name: Install Multiple Packages on Red Hat/CentOS
  hosts: target_hosts
  become: yes
  tasks:
    - name: Install packages
      yum:
        name:
          - package1
          - package2
          - package3
        state: present

Lab15: To Uninstall multiple packages using Ansible, you can provide a list of packages to the appropriate package management module for your system.

---
- name: Install Multiple Packages on Red Hat/CentOS
  hosts: target_hosts
  become: yes
  tasks:
    - name: Install packages
      yum:
        name:
          - package1
          - package2
          - package3
        state: absent

Lab16: Ansible Playbook to Install Jenkins on an Ubuntu/Debian Machine:

This playbook performs the following tasks:

  1. Installs prerequisite packages needed to add new repositories over HTTPS.
  2. Adds the Jenkins APT key and repository to the system.
  3. Installs Jenkins.
  4. Ensures the Jenkins service is started and enabled on boot.
amaster@Rushi-Infotech:~$ sudo nano jenkins-install.yml
---
- name: Install Jenkins for CI/CD
  hosts: client
  become: yes
  vars:
    jenkins_apt_key_url: "https://pkg.jenkins.io/debian/jenkins.io.key"
    jenkins_apt_repository: "deb https://pkg.jenkins.io/debian-stable binary/"

  tasks:
    - name: Install prerequisite packages
      apt:
        name: 
          - apt-transport-https
          - ca-certificates
          - curl
          - software-properties-common
        state: present

    - name: Add Jenkins APT Key
      apt_key:
        url: "{{ jenkins_apt_key_url }}"
        state: present

    - name: Add Jenkins Repository
      apt_repository:
        repo: "{{ jenkins_apt_repository }}"
        state: present

    - name: Update apt and install Jenkins
      apt:
        name: jenkins
        state: latest
        update_cache: yes

    - name: Start and enable Jenkins service
      service:
        name: jenkins
        state: started
        enabled: yes
amaster@Rushi-Infotech:~$ ansible-playbook jenkins-install.yml



Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *