Followers

Ansible Playbook Assignment

  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 webser...

 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. Create a playbook to install apache (apache2 for Ubuntu and httpd for CentOs) on webservers ( consider webservers are either CentOs or Ubuntu System).

8. Start httpd service on webservers which are CentOs.

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

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






COMMENTS

BLOGGER: 30
  1. 1. ---
    - name: answer 1
    hosts: webservers
    tasks:
    - name: installing apache
    apt:
    name: apache2
    state: present

    2. ---
    - name: answer 2
    hosts: webservers
    tasks:
    - name: starting apache service
    service:
    name: apache2
    state: started
    3. ---
    - name: answer 3
    hosts: dbservers
    vars_prompt:
    name: random
    prompt: Enter the content
    tasks:
    - name: creating file
    command: touch /tmp/status.txt
    - name: adding content
    copy:
    content: "{{ random }}"
    dest: /tmp/status.txt
    4. ---
    - name: answer 4
    hosts: webservers
    tasks:
    - name: copying files to webservers
    copy:
    src: /tmp/status.txt
    dest: /tmp/status.txt
    5. ---
    - name: answer 5a
    hosts: webservers
    tasks:
    - name: stop apache
    service:
    name: apache2
    state: stopped
    - name: delete status.txt
    command: rm /tmp/status.txt
    - name: answer 5b
    hosts: webservers
    tasks:
    - name: uninstalling apache
    apt:
    name: apache2
    state: absent
    purge: yes

    ReplyDelete
    Replies
    1. 6.
      ---
      - name: answer 6
      hosts: webservers
      vars_prompt:
      name: testvar
      prompt: Enter value
      tasks:
      - name: conditional testing
      copy:
      content: "Testing in progress"
      dest: /tmp/testfile.txt
      when: testvar == "Testing"
      7.
      ---
      - name: installing apache2 and httpd
      hosts: webservers
      tasks:
      - name: apache2 installation
      apt:
      name: apache2
      state: present
      when: ansible_facts['os_family']=="Debian"
      - name: httpd installation
      yum:
      name: httpd
      state: present
      when: ansible_facts['os_family']=="RedHat"
      8.
      ---
      - name: answer 8
      hosts: webservers
      tasks:
      - name: starting httpd
      service:
      name: httpd
      status: started
      when: ansible_facts['os_family']=="RedHat"
      9.
      ---
      - name: answer 9
      hosts: webservers
      tasks:
      - name: removing apache
      apt:
      name: apache2
      state: absent
      purge: yes
      when: ansible_facts['os_family']=="Debian" and ansible_distribution=="Ubuntu"
      10.
      ---
      - name: answer 10
      hosts: webservers
      tasks:
      - name: adding user
      user:
      name: John
      when: ansible_facts['os_family']=="Debian"
      - name: verifying added user
      shell: cat /etc/passwd | grep John| wc -l
      register: output
      - name: printing output
      debug:
      msg: "user has been added"
      when: output.stdout == "1"

      Delete
    2. Excellent Work Naveen..Keep it up!

      Delete
  2. Mikraj

    ---
    - name: First task
    hosts: webservers
    tasks:
    - name: install apache2
    Shell: apt install apache2 -y
    ---
    - name: Second task
    hosts: webservers
    tasks:
    - name: start apache servie
    Service:
    name: apache2
    state: started
    ---
    - name: Third task
    hosts: dbservers
    vars_prompt:
    name: content data
    prompt: enter content
    tasks:
    - name: create a file
    command: touch /tmp/status.txt
    # Task2
    - name: Add content to the file
    copy:
    content: "{{ data }}"
    dest: /tmp/status.txt
    ---
    - name: Fourth task
    hosts: webservers
    tasks:
    - name: copy file
    copy:
    content:
    src: /tmp/status.txt
    dest: /tmp/status.txt
    ---
    - name: Fifth task
    hosts: webservers
    tasks:
    - name: start apache servie
    Service:
    name: apache2
    state: stopped
    - name: delete /tmp/status.txt file
    Shell: "rm -ifr /tmp/status.txt"
    ---
    - name: Fifth task
    hosts: webservers
    tasks:
    - name: start apache servie
    Service:
    name: apache2
    state: absent
    purge: yes

    ReplyDelete
    Replies
    1. - name: play for conditonal statements
      hosts: webservers
      vars_prompt:
      name: testvar
      prompt: Enter the value
      tasks:
      - name: create file
      command: touch /tmp/testfile.txt && echo "Testing is in progress" >> /tmp/testfile.txt
      when: testvar= "testing"

      ---
      - name: play to install on condition
      hosts: webservers
      tasks:
      - name: install apache2
      apt:
      name: apache2
      state: present
      when: ansible_facts['os']=="Ubuntu"
      - name: install httpd
      yum:
      name: httpd
      state: present
      when: ansible_facts['os']=="Centos"
      ---
      - name: install centos
      hosts: webservers
      tasks:
      - name: starting apache service
      service:
      name: httpd
      state: started

      ---
      - name: uninstall apache conditions
      hosts: webservers
      tasks:
      - name: install apache
      apt:
      name: apache2
      state: absent
      purge: yes
      when: ansible_facts['os_family']=="Debian" and ansible_facts['os']=="Ubuntu"

      ---
      - name: create user
      hosts: webservers
      tasks:
      - name: userid
      user:
      name: John
      uid: john1041
      when: ansible_facts['os_family']=="Debian"
      tasks:
      - name: verify user created
      shell: cat /etc/passwd | grep John| wc -l

      Delete
  3. 1. Create a playbook to install apache on the webservers
    -> vi install.apache.yml
    ---
    - name: play for installing apache2
    hosts: webservers
    tasks:
    - name: installing apache2
    apt: name=apache2 state=present

    2. Create a playbook to start an apache service on webservers
    -> vi start.apache.yml
    ---
    - name: play for starting apache2
    hosts: webservers
    tasks:
    - name: start apache2
    command: sudo systemctl start apache2

    3. Create a playbook to create a file /tmp/status with dynamic contents on dbservers.(vars_prompt)
    -> vi dynamic.yml
    ---
    - name: play for creating dynamic file on dbservers
    hosts: dbservers
    vars_prompt:
    name: data
    prompt: Enter the value
    tasks:
    # Task1
    - name: create a file
    command: touch /tmp/status.txt
    # Task2
    - name: Add content to the file
    copy:
    content: "{{ data }}"
    dest: /tmp/status.txt
    # Task 3
    - name: Read the content
    command: cat /tmp/status.txt
    register: output
    # Task 4
    - name: Print the content
    debug:
    var: output.stdout

    4. Create a playbook to copy /tmp/status.txt file to web servers /tmp/status.txt
    -> vi copy.yml
    ---
    - name: play for copying file from local to remote servers
    hosts: webservers
    tasks:
    - name: copy file
    copy:
    src: /tmp/status.txt
    dest: /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).
    -> vi final.yml
    ---
    - name: play for stopping apache2 and deleting status.txt
    hosts: webservers
    tasks:
    - name: stop apache2
    command: sudo systemctl stop apache2
    name: deleting status.txt
    command: rm /tmp/status.txt
    name: play for uninstalling apache2
    hosts: webservers
    tasks:
    - name: uninstall apache2
    apt: name=apache2 state=absent

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. 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.
      -> vi conditional.yml
      ---
      - name: play for conditional statements
      hosts: webservers
      vars_prompt:
      name: testvar
      prompt: Enter value here
      tasks:
      - name: create file in /tmp/testfile.txt
      command: touch /tmp/testfile.txt
      - name: write content in file
      command: echo "Testing is in progress" >> /tmp/testfile.txt
      when: testvar=="Testing"

      7. Create a playbook to install apache (apache2 for Ubuntu and httpd for CentOs) on webservers ( consider webservers are either CentOs or Ubuntu System).
      -> vi conditional.installation.yml
      ---
      - name: play for conditional installation
      hosts: webservers
      tasks:
      - name: install httpd
      yum:
      name: httpd
      state: present
      when: ansible_distribution=="CentOS"
      - name: install apache2
      apt:
      name: apache2
      state: present
      when: ansible_distribution=="Ubuntu"

      8. Start httpd service on webservers which are CentOs.
      -> vi start.httpd.yml
      ---
      - name: play for starting httpd
      hosts: webservers
      tasks:
      - name: start httpd
      service:
      name: httpd
      state: started
      when: ansible_distribution=="CentOS"

      9. Remove apache from Webserver if the OS family is Debian and OS is Ubuntu.
      -> vi remove.apache.yml
      ---
      - name: play for removing apache2
      hosts: webservers
      tasks:
      - name: remove apache2
      apt: name=apache2 purge=yes
      when: ansible_os_family=="Debian" and ansible_distribution=="Ubuntu"

      10. Create a user John on Debian based OS webservers. (use user module) and verify that user created.(cat /etc/passwd | grep John| wc -l)
      -> vi createuser.yml
      ---
      - name: Play for creating user
      hosts: dbservers
      tasks:
      - name: Adding user
      user:
      name: John
      when: ansible_os_family=="Debian"
      - name: Finding user details
      shell: cat /etc/passwd | grep John | wc -l
      register: output
      - name: Confirming user creation
      debug:
      msg: "User Created"
      when: output.stdout=="1"
      - name: Confirming user not created
      debug:
      msg: "User not created"
      when: output.stdout!="1"

      Delete
  4. ---
    - name: one two
    hosts: webservers
    tasks:
    - name: install apache
    apt:
    name: apache2
    state: present
    - name: start check apache
    service:
    name: apache2
    state: started
    - name: three
    hosts: dbservers
    vars_prompt:
    name: data
    prompt: Enter the value
    tasks:
    - name: create file
    command: touch /tmp/status.txt
    - name: add content
    copy:
    content: "{{data}}"
    dest: /tmp/status.txt
    - name: four
    hosts: webservers
    tasks:
    - name: transfer file
    copy:
    src: /tmp/status.txt
    dest: /tmp
    - name: read file
    command: cat /tmp/status.txt
    register: output
    - name: print output
    debug:
    var: output.stdout
    - name: five
    hosts: webservers
    tasks:
    - name: stop apache
    service:
    name: apache2
    state: stopped
    - name: del file
    command: rm /tmp/status.txt
    - name: uninstall apache
    apt:
    name: apache2
    state: absent
    purge: true

    ReplyDelete
    Replies
    1. Q6 -
      ---
      - name: qn6
      hosts: webservers
      vars_prompt:
      - name: testvar
      prompt: Enter value
      tasks:
      - name: create file when condition met
      copy:
      dest: /tmp/testfile.txt
      content: "Testing is in progress"
      when: testvar == "testing"

      q7 -
      ---
      - name: install apache2 with condition
      hosts: webservers
      tasks:
      - name: install apache2
      apt:
      name: apache2
      state: present
      when: ansible_distribution=="Ubuntu" or ansible_distribution=="CentOS"

      q8 -
      ---
      - name: start httpd service
      hosts: webservers
      tasks:
      - name: start httpd
      yum:
      name: httpd
      state: started
      when: ansible_distribution == "CentOS"

      q9 -
      ---
      - name: remove apache2 service
      hosts: webservers
      tasks:
      - name: rm httpd
      apt:
      name: httpd
      state: absent
      purge: true
      when: ansible_facts['os_family']=="Debian" and ansible_distribution=="Ubuntu"

      q10 -
      ---
      - name: Add the user John in Debian
      hosts: webservers
      tasks:
      - name: add user john
      user:
      name: John
      when: ansible_facts['os_family']=="Debian"
      - name: verify user
      shell: cat /etc/passwd | grep John| wc -l

      Delete
  5. 1. install apache
    2. start apache
    ---
    - name: Play for webservers (node2, node3)
    hosts: webservers
    tasks:
    - name: installing apache2 on webservers
    shell: apt install apache2 -y

    - 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
    shell: apt purge apache2 -y

    ReplyDelete
  6. root@node1:/etc/ansible# vi fourth.yaml
    ---
    - name: play for webservers install apache3
    hosts: webservers
    tasks:
    # Task1
    - name: install apache
    command: sudo apt update
    command: sudo apt install apache2 -y
    ~


    start apache2 status
    ---
    - name: play for webservers start apache
    hosts: webservers
    tasks:
    # Task1
    - name: start apache service
    command: sudo service apache2 start
    ~


    prompt and save to status.txt

    ---
    - name: play for dbservers multiple tasks
    hosts: dbservers
    vars_prompt:
    name: status
    prompt: Enter the value
    tasks:
    # Task1
    - name: create a file
    command: touch /tmp/status.txt
    # Task2
    - name: Add content to the file
    copy:
    content: "{{ status }}"
    dest: /tmp/status.txt
    # Task 3
    - name: Read the content
    command: cat /tmp/status.txt
    register: output
    # Task 4
    - name: Print the content
    debug:
    var: output.stdout


    copy a file
    ---
    - name: play for dbservers multiple tasks
    hosts: webservers
    tasks:
    # Task1
    - name: copy a file
    copy:
    src: /tmp/status.txt
    dest: /tmp/status.txt


    ReplyDelete
    Replies
    1. ---
      - name: play for webservers multiple tasks
      hosts: webservers
      tasks:
      # Task1
      - name: stop apache2
      shell: sudo service apache2 stop
      shell: sudo apt-get purge apache2 -y

      Delete
  7. Pong

    1.

    vi installapache.yaml
    ---
    - name: install
    hosts: webservers
    tasks:
    - name: Executing command module
    command: apt update
    command: apt install apache2 -y
    ~
    2.
    vi startapache.yaml
    ---
    - name: start
    hosts: webservers
    tasks:
    - name: apache2
    state: started


    3.

    ---
    - name: dynamic content
    hosts: dbservers
    vars_prompt:
    name: data
    prompt: Enter the value
    tasks:
    # Task1
    - name: create a file
    command: touch /tmp/status.txt
    4.

    ---
    - name: copy to webserver
    hosts: webservers
    vars_prompt:
    name: data
    prompt: Enter the value
    tasks:
    # Task2
    - name: copy
    src=/tmp/status.txt
    dest=/tmp/status.txt
    5.

    ---
    - name: stop
    hosts: webservers
    tasks:
    - name: Executing command module
    name: apache2
    state: stopped
    command: rm /tmp/app.java

    - name: uninstall
    hosts: webservers
    tasks:
    - name: Execute
    name: apache2
    state: absent
    purge: yes














    ReplyDelete
    Replies
    1. Pong


      6.
      ---
      - name: test
      hosts: webservers
      vars:
      data: testvar
      value:
      tasks:
      - name: if testing
      command: touch /tmp/testfile.txt
      when: vars=="Testing"
      - name: show msg
      debug:
      msg: "{{ 'Testing is in progress' }}"
      ~
      7.
      ---
      - name: play for conditonal statements
      hosts: webservers
      tasks:
      - name: installation
      apt:
      name: httpd
      state: present
      when: ansible_facts['os_family']=="CentOS"
      - name: installation
      yum:
      name: apache2
      state: present
      when: ansible_facts['os_family']=="Debian"

      8.

      ---
      - name: play for conditonal statements
      hosts: webservers
      tasks:
      - name: httpd
      state: started
      when: ansible_facts['os_family']=="CentOS"
      9.

      ---
      - name: play for conditonal statements
      hosts: webservers
      tasks:

      - name: purge apache2
      apt:
      name: apache2
      state: absent

      when: ansible_facts['os_family']=="CentOS"

      10.

      hosts: webservers
      tasks:
      - name: John
      when: ansible_facts['os_family']=="Debian"
      - name: verify
      shell: cat /etc/passwd | grep John| wc -l
      register: output
      - name: printing output
      debug:
      msg: "user has been added"
      when: output.stdout == "1"




      Delete
  8. 1. Create a playbook to install apache on the webservers

    ---
    - name: install apache2
    hosts: webservers
    tasks:
    - name: installing apache2
    apt: name=apache2 state=present

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

    - name: start apache2
    hosts: webservers
    tasks:
    - name: starting apache2
    command: sudo systemctl start apache2

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

    - name: create file
    hosts: dbservers
    vars_prompt:
    name: random
    prompt: Enter the content
    tasks:
    - name: creating file
    command: touch /tmp/status.txt
    - name: adding content
    copy:
    content: "{{ hello world }}"
    dest: /tmp/status.txt

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

    - name: copy file to webserver
    hosts: webservers
    tasks:
    - name: copying files to webservers
    copy:
    src: /tmp/status.txt
    dest: /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).

    ---
    - name: play for stopping apache2 and deleting status.txt
    hosts: webservers
    tasks:
    - name: stopping apache2
    command: sudo systemctl stop apache2
    name: deleting status.txt
    command: rm /tmp/status.txt
    name: uninstalling apache2
    hosts: webservers
    tasks:
    - name: uninstall apache2
    apt: name=apache2 state=absent

    ReplyDelete
    Replies
    1. Good work...just on question 3 use {{ random}}}

      Delete
  9. name: one two
    hosts: webservers
    tasks:
    - name: install apache
    apt:
    name: apache2
    state: present
    - name: start check apache
    service:
    name: apache2
    state: started
    - name: three
    hosts: dbservers
    vars_prompt:
    name: data
    prompt: Enter the value
    tasks:
    - name: create file
    command: touch /tmp/status.txt
    - name: add content
    copy:
    content: "{{data}}"
    dest: /tmp/status.txt
    - name: four
    hosts: webservers
    tasks:
    - name: transfer file
    copy:
    src: /tmp/status.txt
    dest: /tmp
    - name: read file
    command: cat /tmp/status.txt
    register: output
    - name: print output
    debug:
    var: output.stdout
    - name: five
    hosts: webservers
    tasks:
    - name: stop apache
    service:
    name: apache2
    state: stopped
    - name: del file
    command: rm /tmp/status.txt
    - name: uninstall apache
    apt:
    name: apache2
    state: absent
    purge: true

    ReplyDelete
  10. Q6:
    --
    - 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'

    Q7:
    ---
    - 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"

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,Jira,1,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SonarQube,3,Splunk,2,vagrant kubernetes,1,YAML Basics,1,
ltr
item
DevOpsWorld: Ansible Playbook Assignment
Ansible Playbook Assignment
DevOpsWorld
https://www.devopsworld.co.in/2022/02/ansible-playbook-assignment.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/2022/02/ansible-playbook-assignment.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content