Ansible Variable Lab & Excercise – Part 2

Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

Assigment 1 – Create a playbook and setup a webserver(httpd) and use httpd as a variable decalared in role var file

---
-name: webserver
 host:web
 vars_files:
   - var-file.yml

 tasks:
 -name: Install latest httpd
  yum:
     name: "{{packname}}"
     state: latest
 - name: Start service httpd, if not started
   ansible.builtin.service:
      name: "{{packname}}"
      state: started

Assigment 2 – Create a playbook and setup a webserver(httpd) and use httpd as a variable declared using prompt.

---
-name: webserver
 host: web
 vars_prompt:
   - name: "webserver"
      prompt: "Which webserver do you want to install?"
      private: false
 tasks:
 - yum:
    name: "{{ webserver }}"
    state: latest
 - name: Start service httpd, if not started
    ansible.builtin.service:
      name: "{{ webserver }}"
      state: started

Assigment 3 – Create a playbook and setup a webserver(httpd) and find a list of .txt file in some directory and output using register variables

---
-name: webserver
 host:web
 vars:
   webserver: httpd

 tasks:
 -name: Install latest httpd
  yum:
    name: "{{ webserver }}"
    state: latest
 - name: Start service httpd, if not started
    ansible.builtin.service:
      name: "{{ webserver }}"
      state: started
 -name: Listing .txt files
    shell: "find *.txt"
    args:
      chdir: "/home/centos/ansible/sanchita"
    register: find_output
 - name: Print the vars
   ansible.builtin.debug:
     var: find_output.stdout_lines
    

Assigment 4 – Create a playbook and setup a webserver(httpd), copy a index.html and interpolate variables in template file decalared in playbook.

---
-name: webserver
 host:web
 vars:
   webserver: httpd
 
 tasks:
 -name: Install latest httpd
  yum:
    name: "{{ webserver }}"
    state: latest
 - name: copy index.html
   ansible.builtin.template:
     src: index.html.j2
     dest: /var/www/html/
 - name: Start service httpd, if not started
   ansible.builtin.service:
     name: "{{ webserver }}"
     state: started