Tuesday, February 22, 2022

ASSIGNMENT: Ansible Playbook


 

Create a setup of 3 ubuntu Servers where one of the servers is the Master server where the Ansible is installed whereas the other 2 webservers are managed nodes.

1. Create a playbook to install apache on the webservers

2. Create a playbook to start an apache service on webservers

3. Create a playbook to create a file /tmp/status with dynamic contents on dbservers.(vars_prompt)

4. Create a playbook to copy /tmp/status.txt file to web servers /tmp/status.txt

5. Create a playbook to Stop the apache service and delete /tmp/status.txt and also uninstall the apache service ( create a separate play for uninstalling the apache service).

6. Create a playbook where you define a variable called testvar and the value of the variable should be entered at run time if the value of a variable is Testing then create a file /tmp/testfile.txt and write the content that "Testing is in progress" on webservers.

7. A series of tasks in one playbook:
7.1 Create a playbook to install apache (apache2 for Ubuntu and httpd for CentOs) on webservers ( consider webservers are either CentOs or Ubuntu System).

7.2. Start httpd service on webservers which are CentOs.

7.3. Remove apache from Webserver if the OS family is Debian and OS is Ubuntu.

7.4. Create a user John on Debian based OS webservers. (use user module) and verify that user created.(cat /etc/passwd | grep John| wc -l)


ANSWER:

1. install apache
2. start apache

---
- name: Play for webservers (node2, node3)
  hosts: webservers
  tasks:
        - name: installing apache2 on webservers
          apt: 

                name:apache2 

                state:present


        - name: starting apache2 service on webservers
          service:
                name: apache2
                state: started


3. create file and dynamic value
---
- name: Play for dbservers (node1)
hosts: dbservers
vars_prompt:
        name: data
        prompt: Enter a value
tasks:
        - name: create a .txt file in /tmp
          command: touch /tmp/status.txt

        - name: please add a dynamic text in the .txt file
          copy:
                content: "{{ data }}"
                dest: /tmp/status.txt


4. copy file
---
- name: Play for pushing dbservers file to webservers
hosts: webservers
tasks:
        - name: copying .txt file from dbservers to webservers
          copy:
                src: /tmp/status.txt
                dest: /tmp/status.txt


5.1. stop apache and delete .txt file
---
- name: Play for webservers (node2, node3)
hosts: webservers
tasks:
        - name: stopping apache2 service from webservice
          service:
                name: apache2
                state: stopped
        - name: deleting /tmp/status.txt from webservice
          shell: rm /tmp/status.txt


5.2. uninstall apache
---
- name: Play for webservers(node2, node3)
hosts: webservers
tasks:
        - name: uninstall apache2 service from webservers
          apt: 

                name:apache2 

                state:absent 

                purge:yes


6. prompt in runtime

---
- name: play for assignment playbook 6
  hosts: webservers
  vars_prompt:
            name: testvar
            prompt: enter a value
  tasks:
            - name: creating testfile.txt in /tmp if testvar is Testing
              command: touch /tmp/testfile.txt
              when: testvar == "Testing"

            - name: write content in the .txt file
              shell: echo 'Testing is in progress' >> /tmp/testfile.txt
              when: testvar == 'Testing'



7. A series of tasks in a playbook

---
- name: play for assignment playbook 7
  hosts: webservers
  tasks:

        - name: install apache2 (if Debian family OS)
          apt:
                name: apache2
                state: present
          when: ansible_facts['os_family'] == "Debian"

        - name: install httpd (if Redhat family OS)
          yum:
                name: httpd
                state: present
          when: ansible_facts['os_family'] == "RedHat"

        - name: start httpd service
          service:
                name: httpd
                state: started
          when: ansible_facts['os_family'] == "RedHat" and ansible_distribution=="CentOS"

        - name: remove apache if Debian - Ubuntu
          apt:
                name: apache2
                state: absent
                purge: yes
          when: ansible_facts['os_family'] == "Debian" and ansible_distribution=="Ubuntu"

        - name: create a user (John) on Debian based webserver
          user:
                name: John
          when: ansible_facts['os_family'] == "Debian"

        - name: verify added user
          shell: cat /etc/passwd | grep John| wc -l
          register: output

        - name: printing verification of added user
          debug:
                msg: "A user is added"
          when: output.stdout == "1"

        - name: printing verification if user is not added
          debug:
                msg: "No user is added"
          when: output.stdout == "0"

No comments:

Post a Comment

Fluentd

Open-source log data collector > why logs? - for compliance (auditing, company, business) - for security (transparency, monitoring, admin...