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
<webserver.yaml>

- name: This sets up an httpd webserver
  hosts: localhost
  vars_files:
    - vars-file.yaml

  tasks:
  - name: Install the httpd apps
    yum:
      name={{ servicename }}
      state=latest
  - name: 
    ansible.builtin.service:
      name={{ servicename }}
      state=started

<vars-file.yaml>

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

<webserver.yaml>

- name: This sets up an httpd webserver
  hosts: localhost
  vars_prompt:
    - name: "servicename"
      prompt: "Enter service name"
      private: false

  tasks:
  - name: Install the httpd apps
    yum:
      name={{ servicename }}
      state=latest
  - name: 
    ansible.builtin.service:
      name={{ servicename }}
      state=started
  - name: Print the vars
    ansible.builtin.debug:
      msg: Service name {{ servicename }}



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

<webserver.yaml>

- name: This sets up an httpd webserver
  hosts: localhost
  vars_prompt:
    - name: "servicename"
      prompt: "Enter service name"
      private: false

  tasks:
  - name: Install the httpd apps
    yum:
      name={{ servicename }}
      state=latest
  - name: 
    ansible.builtin.service:
      name={{ servicename }}
      state=started
  - name: Print the vars
    ansible.builtin.debug:
      msg: Service name {{ servicename }}
  - name: 
    shell: "find *.txt"
    args:
      chdir: "/home/centos/ansible/monika"
      register: find_output
Assigment 4 – Create a playbook and setup a webserver(httpd), copy a index.html and interpolate variables in template file decalared in playbook.


<webserver.yaml>

- name: This sets up an httpd webserver
  hosts: localhost
  vars_prompt:
    - name: "servicename"
      prompt: "Enter service name"
      private: false

  tasks:
  - name: Install the httpd apps
    yum:
      name={{ servicename }}
      state=latest
  - name: 
    ansible.builtin.service:
      name={{ servicename }}
      state=started
  - name: Print the vars
    ansible.builtin.debug:
      msg: Service name {{ servicename }}
  - 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: "{{servicenamename}}"
     state: started