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: Update web servers
  hosts: web
  vars_files:
    - ext-vars.yaml
   
  tasks:
  - name: Install the latest version of Apache
    yum:
      name={{ packagename }}
      state=latest
  - name: Copy file with owner and permissions
    ansible.builtin.copy:
      src: index.html
      dest: /var/www/html/index.html
  - name: Start service httpd, if not started
    ansible.builtin.service:
      name={{ servicename }}
      state=started

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

---
- name: Update web servers
  hosts: web
  vars_prompt:
    - name: "packagename"
      prompt: "package name"
      private: false
   
  tasks:
  - name: Install the latest version of Apache
    yum:
      name={{ packagename }}
      state=latest
  - name: Copy file with owner and permissions
    ansible.builtin.copy:
      src: index.html
      dest: /var/www/html/index.html
  - name: Start service httpd, if not started
    ansible.builtin.service:
      name={{ packagename }}
      state=started
  - name: Print the vars
    ansible.builtin.debug:
      msg: package {{ packagename }} is installed

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: Update web servers
  hosts: web
  vars:
    packagename: httpd
    servicename: httpd
	
  tasks:
  - name: Install the latest version of Apache
    yum:
      name={{ packagename }}
      state=latest
  - name: Copy file with owner and permissions
    ansible.builtin.copy:
      src: index.html
      dest: /var/www/html/index.html
  - name: Start service httpd, if not started
    ansible.builtin.service:
      name={{ servicename }}
      state=started
  - name: Ansible register variable
    shell: "find *.txt"
    args:
      chdir: "/home/centos/ansible/sandhya"
    register: find_output
  - debug:
      var: find_output.stdout_lines[0]

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

---
- name: Update web servers
  hosts: web
  vars:
    myname: sandhya
	
  tasks:
  - name: Install the latest version of Apache
    yum:
      name=httpd  
      state=latest	  
  - name: Start service httpd, if not started
    ansible.builtin.service:
      name=httpd
      state=started
  - name: Template a file to /var/www/html/
    ansible.builtin.template:
      src: index.html.j2
      dest: /var/www/html/index.html