Kode Kloud: Ansible

 Date: 23 Feb 2025

Tasks: Creating Soft Links Using Ansible

The Nautilus DevOps team is practicing some of the Ansible modules and creating and testing different Ansible playbooks to accomplish tasks. Recently they started testing an Ansible file module to create soft links on all app servers. Below you can find more details about it.

Write a playbook.yml under /home/thor/ansible directory on jump host, an inventory file is already present under /home/thor/ansible directory on jump host itself. Using this playbook accomplish below given tasks:

  • Create an empty file /opt/itadmin/blog.txt on app server 1; its user owner and group owner should be tony. Create a symbolic link of source path /opt/itadmin to destination /var/www/html.
  • Create an empty file /opt/itadmin/story.txt on app server 2; its user owner and group owner should be steve. Create a symbolic link of source path /opt/itadmin to destination /var/www/html.
  • Create an empty file /opt/itadmin/media.txt on app server 3; its user owner and group owner should be banner. Create a symbolic link of source path /opt/itadmin to destination /var/www/html.

Note: Validation will try to run the playbook using command ansible-playbook -i inventory playbook.yml so please make sure playbook works this way without passing any extra arguments.

Solution : 

# vi /home/thor/ansible/playbook.yml 
---
- name: Create files and symbolic links on app servers
  hosts: all
  become: yes
  tasks:
    - name: Create blog.txt on app server 1
      file:
        path: /opt/itadmin/blog.txt
        state: touch
        owner: tony
        group: tony
      when: inventory_hostname == 'stapp01'
    - name: Create symbolic link on app server 1
      file:
        src: /opt/itadmin
        dest: /var/www/html
        state: link
      when: inventory_hostname == 'stapp01'
    - name: Create story.txt on app server 2
      file:
        path: /opt/itadmin/story.txt
        state: touch
        owner: steve
        group: steve
      when: inventory_hostname == 'stapp02'
    - name: Create symbolic link on app server 2
      file:
        src: /opt/itadmin
        dest: /var/www/html
        state: link
      when: inventory_hostname == 'stapp02'
    - name: Create media.txt on app server 3
      file:
        path: /opt/itadmin/media.txt
        state: touch
        owner: banner
        group: banner
      when: inventory_hostname == 'stapp03'
    - name: Create symbolic link on app server 3
      file:
        src: /opt/itadmin
        dest: /var/www/html
        state: link
      when: inventory_hostname == 'stapp03'

# ansible-playbook -i inventory playbook.yml

 Date: 24 Feb 2025

Tasks: Managing ACLs Using Ansible

There are some files that need to be created on all app servers in Stratos DC. The Nautilus DevOps team want these files to be owned by user root only however, they also want that the app specific user to have a set of permissions on these files. All tasks must be done using Ansible only, so they need to create a playbook. Below you can find more information about the task.


Create a playbook named playbook.yml under /home/thor/ansible directory on jump host, an inventory file is already present under /home/thor/ansible directory on Jump Server itself.

Create an empty file blog.txt under /opt/finance/ directory on app server 1. Set some acl properties for this file. Using acl provide read '(r)' permissions to group tony (i.e entity is tony and etype is group).

Create an empty file story.txt under /opt/finance/ directory on app server 2. Set some acl properties for this file. Using acl provide read + write '(rw)' permissions to user steve (i.e entity is steve and etype is user).

Create an empty file media.txt under /opt/finance/ on app server 3. Set some acl properties for this file. Using acl provide read + write '(rw)' permissions to group banner (i.e entity is banner and etype is group).


Note: Validation will try to run the playbook using command ansible-playbook -i inventory playbook.yml so please make sure the playbook works this way, without passing 

Solutions: 

# vi playbook.yml
---
- name: Create files and set ACL permissions on app servers
  hosts: all
  become: yes
  tasks:
    - name: Create blog.txt on app server 1
      file:
        path: /opt/finance/blog.txt
        state: touch
        owner: root
        group: root
        mode: '0644'
      when: inventory_hostname == "stapp01"
    - name: Set ACL for blog.txt (Read permission for group tony)
      acl:
        path: /opt/finance/blog.txt
        entity: tony
        etype: group
        permissions: r
        state: present
      when: inventory_hostname == "stapp01"
    - name: Create story.txt on app server 2
      file:
        path: /opt/finance/story.txt
        state: touch
        owner: root
        group: root
        mode: '0644'
      when: inventory_hostname == "stapp02"
    - name: Set ACL for story.txt (Read + Write for user steve)
      acl:
        path: /opt/finance/story.txt
        entity: steve
        etype: user
        permissions: rw
        state: present
      when: inventory_hostname == "stapp02"
    - name: Create media.txt on app server 3
      file:
        path: /opt/finance/media.txt
        state: touch
        owner: root
        group: root
        mode: '0644'
      when: inventory_hostname == "stapp03"
    - name: Set ACL for media.txt (Read + Write for group banner)
      acl:
        path: /opt/finance/media.txt
        entity: banner
        etype: group
        permissions: rw
        state: present
      when: inventory_hostname == "stapp03"

# ansible-playbook -i inventory playbook.yml

Date: 27 Feb 2025

Tasks: Ansible Manage Services

Developers are looking for dependencies to be installed and run on Nautilus app servers in Stratos DC. They have shared some requirements with the DevOps team. Because we are now managing packages installation and services management using Ansible, some playbooks need to be created and tested. As per details mentioned below please complete the task:

  • On jump host create an Ansible playbook /home/thor/ansible/playbook.yml and configure it to install httpd on all app servers.
  • After installation make sure to start and enable httpd service on all app servers.
  • The inventory /home/thor/ansible/inventory is already there on jump host.
  • Make sure user thor should be able to run the playbook on jump host.

Note: Validation will try to run playbook using command ansible-playbook -i inventory playbook.yml so please make sure playbook works this way, without passing any extra arguments.

Solution: 
# cat webserver-installation.yml
---
   - name: Web Server Installation.
     hosts: All
     become: true
     become_user: root
     gather_facts: false
     tasks:
     - name: Installation of Apache Package
       yum:
         name: httpd
         state: present
     - name: HTTP Service
       service:
         name: httpd
         state: started
         enabled: true
ansible-playbook -i inventory playbook.yml

Date: 15 Mar 2025

Tasks: Ansible Lineinfile Module

The Nautilus DevOps team want to install and set up a simple httpd web server on all app servers in Stratos DC. They also want to deploy a sample web page using Ansible. Therefore, write the required playbook to complete this task as per details mentioned below.
We already have an inventory file under /home/thor/ansible directory on jump host. Write a playbook playbook.yml under /home/thor/ansible directory on jump host itself. Using the playbook perform below given tasks:
  1. Install httpd web server on all app servers, and make sure its service is up and running.
  2. Create a file /var/www/html/index.html with content:
This is a Nautilus sample file, created using Ansible!
    3. Using lineinfile Ansible module add some more content in /var/www/html/index.html file. Below is the content:
Welcome to Nautilus Group! 
Also make sure this new line is added at the top of the file.
    4. The /var/www/html/index.html file's user and group owner should be apache on all app servers.
    5. The /var/www/html/index.html file's permissions should be 0655 on all app servers.
Note: Validation will try to run the playbook using command ansible-playbook -i inventory playbook.yml so please make sure the playbook works this way without passing any extra arguments.
Solution: 
thor@jumphost ~/ansible$ cat inventory 
stapp01 ansible_host=172.16.238.10 ansible_ssh_pass=Ir0nM@n ansible_user=tony
stapp02 ansible_host=172.16.238.11 ansible_ssh_pass=Am3ric@ ansible_user=steve
stapp03 ansible_host=172.16.238.12 ansible_ssh_pass=BigGr33n ansible_user=bannerthor@jumphost ~/ansible$ 
thor@jumphost ~/ansible$ vi playbook.yml
---
- name: Configure web server and manage file on all app servers
  hosts: all
  become: yes
  tasks:
    - name: Install httpd web server
      yum:
        name: httpd
        state: present
    - name: Ensure httpd service is started and enabled
      service:
        name: httpd
        state: started
        enabled: yes
    - name: Create /var/www/html/index.html with initial content
      copy:
        dest: /var/www/html/index.html
        content: |
          This is a Nautilus sample file, created using Ansible!
        owner: apache
        group: apache
        mode: '0655'
    - name: Add additional content to /var/www/html/index.html at the top
      lineinfile:
        path: /var/www/html/index.html
        line: "Welcome to Nautilus Group!"
        insertafter: BOF
        owner: apache
        group: apache
        mode: '0655'
thor@jumphost ~/ansible$ ansible-playbook -i inventory playbook.yml


 Date: 2 April 2025

Tasks: Ansible Replace Module

There is some data on all app servers in Stratos DC. The Nautilus development team shared some requirement with the DevOps team to alter some of the data as per recent changes they made. The DevOps team is working to prepare an Ansible playbook to accomplish the same. Below you can find more details about the task.

Write a playbook.yml under /home/thor/ansible on jump host, an inventory is already present under /home/thor/ansible directory on Jump host itself. Perform below given tasks using this playbook:

  • We have a file /opt/dba/blog.txt on app server 1. Using Ansible replace module replace string xFusionCorp to Nautilus in that file.
  • We have a file /opt/dba/story.txt on app server 2. Using Ansiblereplace module replace the string Nautilus to KodeKloud in that file.
  • We have a file /opt/dba/media.txt on app server 3. Using Ansible replace module replace string KodeKloud to xFusionCorp Industries in that file.

Note: Validation will try to run the playbook using command ansible-playbook -i inventory playbook.yml so please make sure the playbook works this way without passing any extra arguments.

Solution: 
thor@jumphost ~/ansible$ cat playbook.yml 
---
- name: Replace strings in files on app servers
  hosts: all
  become: yes
  tasks:
    - name: Replace xFusionCorp with Nautilus in blog.txt on app server 1
      replace:
        path: /opt/dba/blog.txt
        regexp: 'xFusionCorp'
        replace: 'Nautilus'
      when: inventory_hostname == "stapp01"

    - name: Replace Nautilus with KodeKloud in story.txt on app server 2
      replace:
        path: /opt/dba/story.txt
        regexp: 'Nautilus'
        replace: 'KodeKloud'
      when: inventory_hostname == "stapp02"

    - name: Replace KodeKloud with xFusionCorp Industries in media.txt on app server 3
      replace:
        path: /opt/dba/media.txt
        regexp: 'KodeKloud'
        replace: 'xFusionCorp Industries'
      when: inventory_hostname == "stapp03"

thor@jumphost ~/ansible$ 
thor@jumphost ~/ansible$ ansible-playbook -i inventory playbook.yml

PLAY [Replace strings in files on app servers] ******************************************************

TASK [Gathering Facts] ******************************************************************************
ok: [stapp01]
ok: [stapp02]
ok: [stapp03]

TASK [Replace xFusionCorp with Nautilus in blog.txt on app server 1] ********************************
skipping: [stapp02]
skipping: [stapp03]
ok: [stapp01]

TASK [Replace Nautilus with KodeKloud in story.txt on app server 2] *********************************
skipping: [stapp01]
skipping: [stapp03]
ok: [stapp02]

TASK [Replace KodeKloud with xFusionCorp Industries in media.txt on app server 3] *******************
skipping: [stapp01]
skipping: [stapp02]
ok: [stapp03]

PLAY RECAP ******************************************************************************************
stapp01                    : ok=2    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
stapp02                    : ok=2    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
stapp03                    : ok=2    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   

thor@jumphost ~/ansible$ 

Explanation:
  • replace module: This module is used to search for a specific string in a file and replace it with another string.
  • path: Specifies the file where the replacement will occur.
  • regexp: Defines the string to search for in the file.
  • replace: Defines the string to replace the matched pattern.
  • when condition: Ensures the task is executed only on the specified app server based on its hostname.
This playbook ensures the string replacements are performed correctly on the respective app servers.

No comments:

Post a Comment